001: /**
002: *
003: */package org.enhydra.dm.handler;
004:
005: import java.io.IOException;
006: import java.util.Map;
007:
008: import javax.servlet.ServletException;
009: import javax.servlet.ServletOutputStream;
010: import javax.servlet.http.HttpServletRequest;
011: import javax.servlet.http.HttpServletResponse;
012:
013: import org.enhydra.dm.api.Document;
014: import org.enhydra.dm.api.DocumentVersion;
015: import org.enhydra.dm.api.exceptions.BaseException;
016: import org.enhydra.dm.api.handler.AbstractHandler;
017: import org.enhydra.dm.api.loggers.Log;
018: import org.enhydra.dm.util.EnhydraDMConstants;
019:
020: /**
021: * Default implementation of a handler for requests using the HTTP GET method.
022: *
023: * @author Slobodan Vujasinovic
024: */
025: public class DefaultGetHandler extends AbstractHandler {
026:
027: /**
028: * Services requests which use the HTTP GET method. This implementation retrieves the
029: * content for non-collection resources, using the content type information. If the
030: * specified file does not exist, a 404 (Not Found) error is sent to the client.
031: *
032: * @param request The request being serviced.
033: * @param response The servlet response.
034: * @throws ServletException If an application error occurs.
035: * @throws IOException If an IO error occurs while handling the request.
036: */
037:
038: public void service(HttpServletRequest request,
039: HttpServletResponse response) throws ServletException,
040: IOException {
041:
042: long modified = 0;
043: Document document = null;
044: byte[] buf;
045: int contentLength;
046: try {
047:
048: Map map = this .readParameters(request);
049:
050: String mod = (String) map.get(EnhydraDMConstants.ACTION);
051:
052: if (null == mod) {
053: if (null != getLogger()) {
054: getLogger()
055: .log(Log.DEBUG,
056: "(GET)Action parameter isn't defined parameter does not exist.");
057: }
058:
059: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
060: response.flushBuffer();
061: return;
062: }
063: if (mod.equals(EnhydraDMConstants.ACTION_READ_VERSION)) {
064:
065: String vdID = (String) map
066: .get(EnhydraDMConstants.VERSION_ID);
067:
068: if (null == vdID) {
069: if (null != getLogger()) {
070: getLogger()
071: .log(Log.DEBUG,
072: "(GET)DocumentVersion parameter id does not exist.");
073: }
074:
075: response
076: .setStatus(HttpServletResponse.SC_BAD_REQUEST);
077: response.flushBuffer();
078: return;
079: }
080:
081: if (null != getLogger()) {
082: getLogger()
083: .log(
084: Log.DEBUG,
085: "GET Request for document version (id)\"{0}\".",
086: vdID);
087: }
088:
089: DocumentVersion dv = getDocumentManager()
090: .getDocumentVersion(vdID);
091:
092: if (null == dv) {
093: if (null != getLogger()) {
094: getLogger()
095: .log(
096: Log.DEBUG,
097: "(GET)DocumentVersion does not exist for id:",
098: vdID);
099: }
100:
101: response
102: .sendError(HttpServletResponse.SC_NOT_FOUND);
103: return;
104: }
105: document = dv.getDocument();
106: modified = dv.getLastModifiedDate();
107: buf = getDocumentStore().loadDocumentContent(
108: dv.getFilepath());
109: contentLength = (int) dv.getSize();
110:
111: } else {
112:
113: String id = (String) map
114: .get(EnhydraDMConstants.DOCUMENT_ID);
115:
116: if (null == id) {
117: if (null != getLogger()) {
118: getLogger()
119: .log(Log.DEBUG,
120: "(GET)Document id parameter does not exist.");
121: }
122: response
123: .setStatus(HttpServletResponse.SC_BAD_REQUEST);
124: response.flushBuffer();
125: return;
126: }
127:
128: if (null != getLogger()) {
129: getLogger()
130: .log(
131: Log.DEBUG,
132: "GET Request for document (id)\"{0}\".",
133: id);
134: }
135:
136: document = getDocumentManager().getDocument(id);
137:
138: if (null == document) {
139:
140: if (null != getLogger()) {
141: getLogger().log(Log.DEBUG,
142: "(GET)Document does not exist.");
143: }
144:
145: response
146: .sendError(HttpServletResponse.SC_NOT_FOUND);
147: return;
148: }
149:
150: modified = document.getLastModifiedDate();
151: buf = getDocumentStore().loadDocumentContent(
152: document.getFilepath());
153: contentLength = (int) document.getSize();
154:
155: }
156:
157: response
158: .setHeader(EnhydraDMConstants.HEAD_PRAGMA, "public");
159: response.setHeader(EnhydraDMConstants.HEAD_CACHE_CONTROL,
160: "max-age=0");
161:
162: if (modified != 0) {
163: response.setHeader(
164: EnhydraDMConstants.HEAD_LAST_MODIFIED, document
165: .getLastModifiedFormated());
166: }
167:
168: String etag = document.getETag();
169: if (etag != null)
170: response.setHeader(EnhydraDMConstants.HEAD_ETAG, etag);
171:
172: int result = checkConditionalRequest(request, document);
173: if (result != HttpServletResponse.SC_OK) {
174: response.setStatus(result);
175: response.flushBuffer();
176: return;
177: }
178: String contentType = document.getMimeType();
179:
180: response.setContentType((contentType != null) ? contentType
181: : "application/octet-stream");
182:
183: response.setContentLength(contentLength);
184:
185: response.setHeader(EnhydraDMConstants.HEAD_ALLOW,
186: getAllowedMethods());
187:
188: ServletOutputStream output = response.getOutputStream();
189:
190: output.write(buf, 0, buf.length);
191:
192: output.flush();
193:
194: } catch (BaseException e) {
195:
196: throw new ServletException(e.getMessage());
197: }
198: }
199: }
|