Glassfish bietet die Möglichkeit einfach und schnell neue Ordner ins System einzubinden und diese per URL- Pattern anzusprechen.
Die Parameter werden in der: glassfish-web.xml angegeben. Für mehr Details können sie gerne bei Oracle in die Details gehen.
<property name="alternatedocroot_1" value="from=/my.jpg dir=/srv/images/jpg"/>
<property name="alternatedocroot_2" value="from=*.jpg dir=/srv/images/jpg"/>
<property name="alternatedocroot_3" value="from=/jpg/* dir=/src/images"/>
Leider unterstützt jboss seit einigen Versionen eine ähnliche Funktion nicht mehr. Dafür kann per HttpServlet ein Ordner eingebunden werden der ebenfalls per Pattern angesprochen wird.
package de.kuw.jee.kuwbid.servlet;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLDecoder;
import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import de.kuw.jee.core.ejb.ParameterBean;
import de.kuw.jee.kuwbid.mb.ApplicationBean;
@WebServlet(urlPatterns = "/images/*", initParams = {
@WebInitParam(name = "basePath", value = "/home/jskoeber/server/wildfly/uploads/documents") }
)
public class ImageServlet extends HttpServlet {
private static final long serialVersionUID = -3522881745170031638L;
private String basePath = "";
@Inject
private ApplicationBean app;
@Inject
private ParameterBean parameter;
public void init() throws ServletException {
this.basePath = getInitParameter("basePath");
if(!parameter.getParameter("uri.images", app.getMandant()).getWert().isEmpty()) {
basePath = parameter.getParameter("uri.images", app.getMandant()).getWert();
}
// Validate base path.
if (this.basePath == null) {
throw new ServletException("FileServlet init param 'basePath' is required.");
} else {
File path = new File(this.basePath);
if (!path.exists()) {
throw new ServletException("FileServlet init param 'basePath' value '" + this.basePath + "' does actually not exist in file system.");
} else if (!path.isDirectory()) {
throw new ServletException("FileServlet init param 'basePath' value '" + this.basePath + "' is actually not a directory in file system.");
} else if (!path.canRead()) {
throw new ServletException("FileServlet init param 'basePath' value '" + this.basePath + "' is actually not readable in file system.");
}
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String requestedFile = request.getPathInfo();
File file = new File(basePath, URLDecoder.decode(requestedFile, "UTF-8"));
if (!file.exists()) {
// Throw 404, redirect to error page may is another selection
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
// write via response's OutputStream
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int bytesRead = 0;
do {
bytesRead = inputStream.read(buffer, 0, buffer.length);
response.getOutputStream().write(buffer, 0, bytesRead);
} while (bytesRead == buffer.length);
response.getOutputStream().flush();
} finally {
if (inputStream != null)
inputStream.close();
}
}
}