1

This is very easy using Glassfish:

Consider my absolute path on unix /apps/static_content/. Using Glassfish, I will simply define alternate doc root as:

<property name="alternatedocroot_1"
  value="from=/static/* dir=/apps/static_content/"/>

When I upload my images and other data files, I can save them to the /apps/static_content directory, and within my JSF page I can display my static content normally as:

<p:graphicsimage value="/static/external_web_app.png"/>

I really need to achieve the same functionality in JBoss AS7

How can I do this?

Beryllium
  • 12,808
  • 10
  • 56
  • 86
maress
  • 3,533
  • 1
  • 19
  • 37

3 Answers3

1

This question is a little bit old, but answering if someone need to do this with newer versions of JBoss/Wildfly.

JBoss AS was renamed to Wildfly from version 8 (i.e. Wildfly 8 is JBoss AS 8) and Red Hat supported version of JBoss was renamed to JBoss EAP. So this applies to:

  • Wildfly (any version)
  • JBoss EAP (from version 7)

First thing to note is that "Alternate doc root" feature in glassfish doesn't work like that. Please take a look at this question for an explanation of the behavior of this feature: Alternate docroot not working on glassfish 4

Now, to answer this question, JBoss/Wildfly is build upon Undertow, that let you do exactly what you need. Refer at this question on how to configure undertow as a web server for static content: How to configure Wildfly to serve static content (like images)?

Gabriel Molina
  • 539
  • 3
  • 14
0
  • Option 1: You could try to deploy a separate exploded .war file, and use that for your static content

In your case: In .../jboss-7/standalone/deployments/ there must be a static.war/.

So the uploads go into this directory, and the content is served back the normal way.

As for details, see Is it possible to deploy an exploded war file (unzipped war) in JBoss AS 7

As pointed out by BalusC: You must not redeploy/remove this directory, as soon as data has been uploaded. You should have a regular backup of this directory.

As far as I know this is the only possibility to do it by configuration/setup only.


  • Option 2: Create separate webapp with name static.war. Add a servlet to stream the static content

This way there is no need to upload/store the files into the file system below ../deployments/, it could be any directory, but you need an additional servlet, so it's solved programatically.

A simple streaming servlet could look like this (just streaming - no authentication etc.):

public class DownloadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {

        final File dir = new File("/var/cms/storage/");
        final String start = "/static/";
        final String relativePath = request.getRequestURI().substring(
                request.getRequestURI().indexOf(start) + start.length());
        final File file = new File(dir, relativePath);
        final String ct = URLConnection.guessContentTypeFromName(file.getName());
        response.setContentType(ct);

        final InputStream is = 
            new BufferedInputStream(new FileInputStream(file));
        try {
            final boolean closeOs = true;
            org.apache.commons.fileupload.util.Streams.copy
                    (is, response.getOutputStream(), closeOs);
        } finally {
            is.close();
        }
    }

Map all URLs to this servlet:

<servlet-mapping>
    <servlet-name>DownloadServlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

The name static.war provides the /static/ web context, so that should make it compatible with the URLs in your code.

Community
  • 1
  • 1
Beryllium
  • 12,808
  • 10
  • 56
  • 86
  • OP intented to save uploaded files and serve them back. This doesn't go together with a WAR. – BalusC Aug 07 '13 at 12:15
  • @BalusC It might not be specified/supported, and probably the data center won't like it. I've tested it, it works using an exploded .war using JBoss 7.1.1: I could upload files into that directory, and serving back works the normal way. – Beryllium Aug 07 '13 at 12:49
  • Until you redeploy the WAR. – BalusC Aug 07 '13 at 12:50
  • @BalusC Thanks, I've added this hint to the answer. – Beryllium Aug 07 '13 at 12:59
  • No, just do not save uploaded files in deploy folder at all. Related answers: http://stackoverflow.com/questions/8885201/uploaded-image-only-available-after-refreshing-the-page/8889096#8889096 and http://stackoverflow.com/questions/4543936/load-the-image-from-outside-of-webcontext-in-jsf/4543951#4543951 – BalusC Aug 07 '13 at 17:58
  • That leaves the area of "configuration only", but yes, a valid option. I have added it to the answer. – Beryllium Aug 07 '13 at 19:44
  • Servlet gets complicated if you'd like to take caching and range requests into account as well. By the way, servlet doesn't need to be deployed in a different WAR. – BalusC Aug 07 '13 at 19:49
  • I have looked at the @BalusC servlet implementation and simply modified it a little to suit my needs. So i will mark this servlet option as the answer. And since i am using the jsf2.0 implementation. i dont need to have servlet mapping in the configurations, but simply annotate my servlet class using WebServlet annotation. – maress Aug 10 '13 at 13:54
0

If you explore jboss directory you will find that there are many you can use to store different type of data like jboss.serer.data.dir .

You can try asking this directory path via system properties and store in a folder the data your services are using, under such directory.

String path = System.getProperty("jboss.server.data.dir");  

Then you can use path as you want, if is just static as shown in your example you set directly the name of the directory.

This should work, I hope :p

ps: as the previous answer suggest the saved data will keep in the directory, and you must not redeploy/remove this directory.. It will keep your data there..

LMG
  • 966
  • 1
  • 12
  • 28