KickJava   Java API By Example, From Geeks To Geeks.

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


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.LocaleHelper;
22 import org.outerj.daisy.repository.VariantKey;
23 import org.outerj.daisy.repository.VariantKeys;
24 import org.outerj.daisy.repository.query.QueryManager;
25 import org.outerj.daisy.httpconnector.RequestHandler;
26 import org.outerj.daisy.httpconnector.HttpUtil;
27 import org.outerx.daisy.x10.SearchResultDocument;
28
29 import java.util.Map JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.HashMap JavaDoc;
33
34 public class QueryHandler implements RequestHandler {
35     public String JavaDoc getPathPattern() {
36         return "/query";
37     }
38
39     public void handleRequest(Map JavaDoc matchMap, HttpRequest request, HttpResponse response, Repository repository) throws Exception JavaDoc {
40         if (request.getMethod().equals(HttpRequest.__GET)) {
41             String JavaDoc query = HttpUtil.getStringParam(request, "q");
42             String JavaDoc locale = HttpUtil.getStringParam(request, "locale");
43             String JavaDoc returnKeysParam = request.getParameter("returnKeys");
44             boolean returnKeys = returnKeysParam != null && returnKeysParam.equals("true");
45             String JavaDoc extraCond = request.getParameter("extraCondition");
46
47             QueryManager queryManager = repository.getQueryManager();
48
49             Map JavaDoc queryOptions = null;
50             List JavaDoc queryOptionsList = request.getParameterValues("queryOption");
51             if (queryOptionsList != null && queryOptionsList.size() > 0) {
52                 queryOptions = new HashMap JavaDoc();
53                 Iterator JavaDoc queryOptionsIt = queryOptionsList.iterator();
54                 while (queryOptionsIt.hasNext()) {
55                     String JavaDoc option = (String JavaDoc)queryOptionsIt.next();
56                     int eqPos = option.indexOf('=');
57                     if (eqPos == -1)
58                         throw new RuntimeException JavaDoc("Missing equal (=) sign in query option: " + option);
59                     queryOptions.put(option.substring(0, eqPos), option.substring(eqPos + 1));
60                 }
61             }
62
63             if (returnKeys) {
64                 VariantKey[] keys = queryManager.performQueryReturnKeys(query, extraCond, queryOptions, LocaleHelper.parseLocale(locale));
65                 new VariantKeys(keys).getXml().save(response.getOutputStream());
66                 response.commit();
67             } else {
68                 SearchResultDocument searchResultDocument = queryManager.performQuery(query, extraCond, queryOptions, LocaleHelper.parseLocale(locale));
69                 searchResultDocument.save(response.getOutputStream());
70                 response.commit();
71             }
72         } else {
73             response.sendError(HttpResponse.__405_Method_Not_Allowed);
74         }
75     }
76 }
77
Popular Tags