01: /*
02: * Copyright 2007 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.navigation.impl.httphandlers;
17:
18: import org.outerj.daisy.httpconnector.spi.BadRequestException;
19: import org.outerj.daisy.httpconnector.spi.RequestHandlerSupport;
20: import org.outerj.daisy.repository.Repository;
21: import org.outerj.daisy.repository.VariantKey;
22: import org.outerj.daisy.xmlutil.LocalSAXParserFactory;
23: import org.outerj.daisy.navigation.LookupAlternative;
24: import org.outerj.daisy.navigation.NavigationVersionMode;
25: import org.outerj.daisy.navigation.NavigationManager;
26: import org.outerj.daisy.navigation.NavigationLookupResult;
27: import org.outerj.daisy.util.HttpConstants;
28: import org.apache.xmlbeans.XmlOptions;
29: import org.outerx.daisy.x10Navigationspec.NavigationLookupDocument;
30:
31: import javax.servlet.http.HttpServletRequest;
32: import javax.servlet.http.HttpServletResponse;
33: import java.util.Map;
34: import java.util.List;
35:
36: public class NavigationLookupHandler extends
37: AbstractNavigationRequestHandler {
38: public void handleRequest(Map matchMap, HttpServletRequest request,
39: HttpServletResponse response, Repository repository,
40: RequestHandlerSupport support) throws Exception {
41: if (request.getMethod().equals("POST")) {
42: XmlOptions xmlOptions = new XmlOptions()
43: .setLoadUseXMLReader(LocalSAXParserFactory
44: .newXmlReader());
45: NavigationLookupDocument navigationLookupDocument = NavigationLookupDocument.Factory
46: .parse(request.getInputStream(), xmlOptions);
47: if (!navigationLookupDocument.validate()) {
48: throw new BadRequestException(
49: "Invalid XML posted to navigationLookup.");
50: }
51: NavigationLookupDocument.NavigationLookup navigationLookupXml = navigationLookupDocument
52: .getNavigationLookup();
53: List<NavigationLookupDocument.NavigationLookup.LookupAlternative> lookupAlternativesXml = navigationLookupXml
54: .getLookupAlternativeList();
55: LookupAlternative[] lookupAlternatives = new LookupAlternative[lookupAlternativesXml
56: .size()];
57: for (int i = 0; i < lookupAlternativesXml.size(); i++) {
58: NavigationLookupDocument.NavigationLookup.LookupAlternative lookupAlternativeXml = lookupAlternativesXml
59: .get(i);
60: lookupAlternatives[i] = new LookupAlternative(
61: lookupAlternativeXml.getName(),
62: lookupAlternativeXml.getCollectionId(),
63: new VariantKey(lookupAlternativeXml
64: .getNavDocId(), lookupAlternativeXml
65: .getNavBranchId(), lookupAlternativeXml
66: .getNavLangId()), NavigationVersionMode
67: .fromString(lookupAlternativeXml
68: .getNavVersionMode()));
69: }
70:
71: String navigationPath = navigationLookupXml
72: .getNavigationPath();
73: long requestedBranchId = navigationLookupXml
74: .isSetRequestedBranchId() ? navigationLookupXml
75: .getRequestedBranchId() : -1;
76: long requestedLangId = navigationLookupXml
77: .isSetRequestedLanguageId() ? navigationLookupXml
78: .getRequestedLanguageId() : -1;
79:
80: NavigationManager navigationManager = (NavigationManager) repository
81: .getExtension("NavigationManager");
82: NavigationLookupResult lookupResult = navigationManager
83: .lookup(navigationPath, requestedBranchId,
84: requestedLangId, lookupAlternatives);
85: lookupResult.getXml().save(response.getOutputStream());
86: } else if (request.getMethod().equals("GET")) {
87: throw new BadRequestException(
88: "Using GET on /navigation/lookup is no longer supported.");
89: } else {
90: response.sendError(HttpConstants._405_Method_Not_Allowed);
91: }
92: }
93:
94: public String getPathPattern() {
95: return "/lookup";
96: }
97: }
|