KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > httpconnector > handlers > PartDataHandler


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.mortbay.http.HttpRequest;
19 import org.mortbay.http.HttpResponse;
20 import org.outerj.daisy.repository.Repository;
21 import org.outerj.daisy.repository.Document;
22 import org.outerj.daisy.httpconnector.RequestHandler;
23 import org.outerj.daisy.httpconnector.HttpUtil;
24
25 import java.util.Map JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.io.InputStream JavaDoc;
28
29 public class PartDataHandler implements RequestHandler {
30     public String JavaDoc getPathPattern() {
31         return "/document/*/version/*/part/*/data";
32     }
33
34     public void handleRequest(Map JavaDoc matchMap, HttpRequest request, HttpResponse response, Repository repository) throws Exception JavaDoc {
35         long documentId = HttpUtil.parseId("document", (String JavaDoc)matchMap.get("1"));
36         long branchId = HttpUtil.getBranchId(request, repository);
37         long languageId = HttpUtil.getLanguageId(request, repository);
38         long versionId;
39         long partTypeId;
40
41         String JavaDoc versionString = (String JavaDoc)matchMap.get("2");
42         if (versionString.equals("last")) {
43             Document document = repository.getDocument(documentId, branchId, languageId, false);
44             versionId = document.getLastVersionId();
45         } else {
46             versionId = HttpUtil.parseId("version", versionString);
47         }
48
49         String JavaDoc partType = (String JavaDoc)matchMap.get("3");
50         if (partType.length() > 0 && Character.isLetter(partType.charAt(0))) {
51             partTypeId = repository.getRepositorySchema().getPartTypeByName(partType, false).getId();
52         } else {
53             partTypeId = HttpUtil.parseId("part type", partType);
54         }
55
56         OutputStream JavaDoc os = response.getOutputStream();
57         byte[] buffer = new byte[BUFFER_SIZE];
58         InputStream JavaDoc is = repository.getPartData(documentId, branchId, languageId, versionId, partTypeId);
59         try {
60             int read;
61             while ((read = is.read(buffer)) != -1) {
62                 os.write(buffer, 0, read);
63             }
64         } finally {
65             is.close();
66         }
67
68         response.commit();
69     }
70
71     private static final int BUFFER_SIZE = 32768;
72 }
73
Popular Tags