001: /**
002: *
003: */package org.enhydra.dm.handler;
004:
005: import java.io.ByteArrayOutputStream;
006: import java.io.IOException;
007: import java.io.InputStream;
008: import java.util.Map;
009:
010: import javax.servlet.ServletException;
011: import javax.servlet.http.HttpServletRequest;
012: import javax.servlet.http.HttpServletResponse;
013:
014: import org.enhydra.dm.api.Document;
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 PUT method.
022: *
023: * @author Slobodan Vujasinovic
024: */
025: public class DefaultPutHandler extends AbstractHandler {
026:
027: /**
028: * Services requests which use the HTTP PUT method. This implementation uploads the
029: * content to the specified location. <br>
030: * If the content length is not specified, a 411 (Length Required) error is sent to the
031: * client. <br>
032: * If the resource exists and is a collection, a 405 (Method Not Allowed) error is sent
033: * to the client. <br>
034: * If the parent collection does not exist, a 409 (Conflict) error is sent to the
035: * client.
036: *
037: * @param request The request being serviced.
038: * @param response The servlet response.
039: * @throws ServletException If an application error occurs.
040: * @throws IOException If an IO error occurs while handling the request.
041: */
042:
043: public void service(HttpServletRequest request,
044: HttpServletResponse response) throws ServletException,
045: IOException {
046:
047: try {
048: int length = request.getContentLength();
049: if (length < 0) {
050: response
051: .sendError(HttpServletResponse.SC_LENGTH_REQUIRED);
052: return;
053: }
054:
055: Map map = this .readParameters(request);
056:
057: String id = (String) map
058: .get(EnhydraDMConstants.DOCUMENT_ID);
059:
060: if (null == id) {
061: if (null != getLogger()) {
062: getLogger()
063: .log(Log.DEBUG,
064: "(PUT)Document id parameter does not exist.");
065: }
066: response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
067: response.flushBuffer();
068: return;
069: }
070:
071: if (null == request.getSession(true).getAttribute(
072: SESSION_ATT_LOCKED)) {
073: if (null != getLogger()) {
074: getLogger().log(Log.WARNING,
075: "(PUT) Conflict - Session Expired.");
076: }
077: response.setStatus(HttpServletResponse.SC_CONFLICT);
078: response.flushBuffer();
079: return;
080: }
081:
082: if (null != getLogger()) {
083: getLogger().log(Log.DEBUG,
084: "PUT Request for document (id)\"{0}\".", id);
085: }
086: Document doc = getDocumentManager().getDocument(id);
087:
088: if (null == doc) {
089:
090: if (null != getLogger()) {
091: getLogger().log(Log.DEBUG,
092: "(PUT)Document does not exist for id.", id);
093: }
094:
095: response.sendError(HttpServletResponse.SC_NOT_FOUND);
096: return;
097: }
098:
099: InputStream input = request.getInputStream();
100: ByteArrayOutputStream output = new ByteArrayOutputStream(
101: request.getContentLength());
102: byte[] buf = new byte[8192];
103: int count;
104: while ((count = input.read(buf)) != -1) {
105: output.write(buf, 0, count);
106: }
107: byte[] documentContent = output.toByteArray();
108: output.flush();
109: output.close();
110:
111: String path = (String) request.getSession()
112: .getAttribute(id);
113:
114: if (path == null || "".equals(path)) {
115: path = getDocumentStore().saveDocumentContent(
116: doc.getName(), documentContent);
117: request.getSession().setAttribute(id, path);
118: } else {
119: String result = getDocumentStore().saveDocumentContent(
120: doc.getName(), documentContent, path);
121: if (null == result) {
122: if (null != getLogger()) {
123: getLogger().log(Log.ERROR,
124: "PUT Request failure for document.");
125: }
126:
127: response.setStatus(HttpServletResponse.SC_CONFLICT);
128: return;
129: }
130: }
131:
132: response.setStatus(HttpServletResponse.SC_CREATED);
133: response.setHeader(EnhydraDMConstants.HEAD_LOCATION,
134: request.getPathTranslated());
135: response.setHeader(EnhydraDMConstants.HEAD_ALLOW,
136: getAllowedMethods());
137: response.flushBuffer();
138: } catch (BaseException e) {
139:
140: response
141: .setStatus(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
142: throw new ServletException(e.getMessage());
143: }
144: }
145: }
|