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.http.HttpServletRequest;
010: import javax.servlet.http.HttpServletResponse;
011:
012: import org.enhydra.dm.api.Document;
013: import org.enhydra.dm.api.DocumentVersion;
014: import org.enhydra.dm.api.exceptions.BaseException;
015: import org.enhydra.dm.api.handler.AbstractHandler;
016: import org.enhydra.dm.api.loggers.Log;
017: import org.enhydra.dm.util.EnhydraDMConstants;
018:
019: /**
020: * Default implementation of a handler for requests using the HTTP HEAD method.
021: *
022: * @author Slobodan Vujasinovic
023: */
024: public class DefaultHeadHandler extends AbstractHandler {
025:
026: /**
027: * Services requests which use the HTTP HEAD method. This implementation returns basic
028: * information regarding the specified resource. <br>
029: * If the specified file does not exist, a 404 (Not Found) error is sent to the client.
030: *
031: * @param request The request being serviced.
032: * @param response The servlet response.
033: * @throws ServletException If an application error occurs.
034: * @throws IOException If an IO error occurs while handling the request.
035: */
036:
037: public void service(HttpServletRequest request,
038: HttpServletResponse response) throws ServletException,
039: IOException {
040:
041: Map map = this .readParameters(request);
042:
043: String mod = (String) map.get(EnhydraDMConstants.ACTION);
044:
045: if (null == mod) {
046: if (null != getLogger()) {
047: getLogger()
048: .log(Log.DEBUG,
049: "(HEAD)Action parameter isn't defined parameter does not exist.");
050: }
051:
052: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
053: response.flushBuffer();
054: return;
055: }
056:
057: long modified = 0;
058: Document document = null;
059: int contentLength = 0;
060:
061: try {
062:
063: if (mod.equals(EnhydraDMConstants.ACTION_READ_VERSION)) {
064:
065: String vdOID = (String) map
066: .get(EnhydraDMConstants.VERSION_ID);
067:
068: if (null == vdOID) {
069: if (null != getLogger()) {
070: getLogger()
071: .log(Log.DEBUG,
072: "(HEAD)DocumentVersion id parameter 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: "HEAD Request for document version (id)\"{0}\".",
086: vdOID);
087: }
088:
089: DocumentVersion dv = getDocumentManager()
090: .getDocumentVersion(vdOID);
091: if (null == dv) {
092:
093: if (null != getLogger()) {
094: getLogger()
095: .log(
096: Log.DEBUG,
097: "(HEAD)DocumentVersion does not exist for id.",
098: vdOID);
099: }
100:
101: response
102: .sendError(HttpServletResponse.SC_NOT_FOUND);
103: return;
104: }
105: document = dv.getDocument();
106: modified = dv.getLastModifiedDate();
107: contentLength = (int) dv.getSize();
108:
109: } else {
110: String id = (String) map
111: .get(EnhydraDMConstants.DOCUMENT_ID);
112:
113: if (null == id) {
114: if (null != getLogger()) {
115: getLogger()
116: .log(Log.DEBUG,
117: "(HEAD)Document id parameter does not exist.");
118: }
119: response
120: .setStatus(HttpServletResponse.SC_BAD_REQUEST);
121: response.flushBuffer();
122: return;
123: }
124:
125: if (null != getLogger()) {
126: getLogger().log(Log.DEBUG,
127: "HEAD Request for document (id)\"{0}\".",
128: id);
129: }
130: document = getDocumentManager().getDocument(id);
131:
132: if (null == document) {
133:
134: if (null != getLogger()) {
135: getLogger()
136: .log(
137: Log.DEBUG,
138: "(HEAD)Document does not exist for id.",
139: id);
140: }
141:
142: response
143: .sendError(HttpServletResponse.SC_NOT_FOUND);
144: return;
145: }
146: modified = document.getLastModifiedDate();
147: contentLength = (int) document.getSize();
148:
149: }
150:
151: String contentType = document.getMimeType();
152:
153: response.setContentType((contentType != null) ? contentType
154: : "application/octet-stream");
155:
156: if (modified != 0) {
157: response.setHeader(
158: EnhydraDMConstants.HEAD_LAST_MODIFIED, document
159: .getLastModifiedFormated());
160: }
161: String etag = document.getETag();
162: if (etag != null)
163: response.setHeader(EnhydraDMConstants.HEAD_ETAG, etag);
164:
165: response.setHeader(EnhydraDMConstants.HEAD_ALLOW,
166: getAllowedMethods());
167: response.setContentLength(contentLength);
168: response.flushBuffer();
169:
170: } catch (BaseException e) {
171:
172: throw new ServletException(e.getMessage());
173: }
174: }
175: }
|