01: package org.andromda.cartridges.jsf;
02:
03: import java.io.BufferedInputStream;
04: import java.io.IOException;
05: import java.io.InputStream;
06: import java.io.OutputStream;
07:
08: import javax.servlet.ServletException;
09: import javax.servlet.http.HttpServlet;
10: import javax.servlet.http.HttpServletRequest;
11: import javax.servlet.http.HttpServletResponse;
12:
13: /**
14: * A servlet allowing us to load resources from a jar and make them available to
15: * the web application.
16: */
17: public class JSFServlet extends HttpServlet {
18: /**
19: * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
20: * javax.servlet.http.HttpServletResponse)
21: */
22: public void doGet(HttpServletRequest request,
23: HttpServletResponse response) throws ServletException,
24: IOException {
25: final String uri = request.getRequestURI();
26: final String path = '/' + uri.substring(uri
27: .indexOf(Constants.RESOURCE_CONTEXT)
28: + Constants.RESOURCE_CONTEXT.length() + 1);
29: final InputStream resource = JSFServlet.class
30: .getResourceAsStream(path);
31: if (resource == null) {
32: throw new ServletException(
33: "Could not load resource from path '" + path + "'");
34: }
35:
36: // - write resource to the output stream
37: final OutputStream out = response.getOutputStream();
38: final byte[] buffer = new byte[2048];
39: BufferedInputStream inputStream = new BufferedInputStream(
40: resource);
41: int read = 0;
42: read = inputStream.read(buffer);
43: while (read != -1) {
44: out.write(buffer, 0, read);
45: read = inputStream.read(buffer);
46: }
47: inputStream.close();
48: out.flush();
49: out.close();
50: }
51: }
|