01: /*
02: * Copyright 2004 Outerthought bvba and Schaubroeck nv
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.outerj.daisy.httpconnector.handlers;
17:
18: import org.outerj.daisy.repository.*;
19: import org.outerj.daisy.httpconnector.spi.HttpUtil;
20: import org.outerj.daisy.httpconnector.spi.RequestHandlerSupport;
21:
22: import javax.servlet.http.HttpServletRequest;
23: import javax.servlet.http.HttpServletResponse;
24: import java.util.Map;
25: import java.io.OutputStream;
26: import java.io.InputStream;
27:
28: public class PartDataHandler extends AbstractRepositoryRequestHandler {
29: public String getPathPattern() {
30: return "/document/*/version/*/part/*/data";
31: }
32:
33: public void handleRequest(Map matchMap, HttpServletRequest request,
34: HttpServletResponse response, Repository repository,
35: RequestHandlerSupport support) throws Exception {
36: String documentId = (String) matchMap.get("1");
37: long branchId = HttpUtil.getBranchId(request, repository);
38: long languageId = HttpUtil.getLanguageId(request, repository);
39:
40: String versionString = (String) matchMap.get("2");
41: Document document = repository.getDocument(documentId,
42: branchId, languageId, false);
43: Version version;
44: if (versionString.equalsIgnoreCase("last")) {
45: version = document.getLastVersion();
46: } else if (versionString.equalsIgnoreCase("live")) {
47: version = document.getLiveVersion();
48: if (version == null)
49: throw new RepositoryException("Document "
50: + document.getVariantKey()
51: + " does not have a live version.");
52: } else {
53: long versionId = HttpUtil.parseId("version", versionString);
54: version = document.getVersion(versionId);
55: }
56:
57: String partType = (String) matchMap.get("3");
58: Part part;
59: if (!(partType.charAt(0) >= '0' && partType.charAt(0) <= '9')) {
60: part = version.getPart(partType);
61: } else {
62: part = version.getPart(Long.parseLong(partType));
63: }
64:
65: response.setDateHeader("Last-Modified", version.getCreated()
66: .getTime());
67: response.setContentType(part.getMimeType());
68: response.setHeader("Content-Length", String.valueOf(part
69: .getSize()));
70:
71: OutputStream os = response.getOutputStream();
72: byte[] buffer = new byte[BUFFER_SIZE];
73: InputStream is = part.getDataStream();
74: try {
75: int read;
76: while ((read = is.read(buffer)) != -1) {
77: os.write(buffer, 0, read);
78: }
79: } finally {
80: is.close();
81: }
82: }
83:
84: private static final int BUFFER_SIZE = 32768;
85: }
|