Mercurial > hg > mpdl-group
comparison software/mpdl-services-new/mpiwg-mpdl-cms-web/src/de/mpg/mpiwg/berlin/mpdl/servlets/cms/GetToken.java @ 25:e9fe3186670c default tip
letzter Stand eingecheckt
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Tue, 21 May 2013 10:19:32 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
23:e845310098ba | 25:e9fe3186670c |
---|---|
1 package de.mpg.mpiwg.berlin.mpdl.servlets.cms; | |
2 | |
3 import java.io.IOException; | |
4 import java.io.PrintWriter; | |
5 import java.util.ArrayList; | |
6 | |
7 import javax.servlet.ServletConfig; | |
8 import javax.servlet.ServletException; | |
9 import javax.servlet.http.HttpServlet; | |
10 import javax.servlet.http.HttpServletRequest; | |
11 import javax.servlet.http.HttpServletResponse; | |
12 | |
13 import org.apache.lucene.index.Term; | |
14 | |
15 import de.mpg.mpiwg.berlin.mpdl.cms.document.Token; | |
16 import de.mpg.mpiwg.berlin.mpdl.cms.lucene.IndexHandler; | |
17 import de.mpg.mpiwg.berlin.mpdl.exception.ApplicationException; | |
18 | |
19 public class GetToken extends HttpServlet { | |
20 private static final long serialVersionUID = 1L; | |
21 public GetToken() { | |
22 super(); | |
23 } | |
24 | |
25 public void init(ServletConfig config) throws ServletException { | |
26 super.init(config); | |
27 } | |
28 | |
29 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
30 request.setCharacterEncoding("utf-8"); | |
31 response.setCharacterEncoding("utf-8"); | |
32 String docId = request.getParameter("docId"); | |
33 String attribute = request.getParameter("attribute"); | |
34 if (attribute == null) | |
35 attribute = "tokenOrig"; | |
36 String query = request.getParameter("query"); | |
37 String countStr = request.getParameter("count"); | |
38 if (countStr == null) | |
39 countStr = "100"; | |
40 int count = Integer.parseInt(countStr); | |
41 String outputFormat = request.getParameter("outputFormat"); | |
42 if (outputFormat == null) | |
43 outputFormat = "xml"; | |
44 try { | |
45 IndexHandler indexHandler = IndexHandler.getInstance(); | |
46 ArrayList<Token> token = null; | |
47 if (docId == null) | |
48 token = indexHandler.getToken(attribute, query, count); | |
49 else | |
50 token = indexHandler.getToken(docId, attribute, query, count); | |
51 if (outputFormat.equals("xml")) | |
52 response.setContentType("text/xml"); | |
53 else if (outputFormat.equals("html")) | |
54 response.setContentType("text/html"); | |
55 else | |
56 response.setContentType("text/xml"); | |
57 PrintWriter out = response.getWriter(); | |
58 out.print("<result>"); | |
59 out.print("<attribute>" + attribute + "</attribute>"); | |
60 out.print("<query>" + query + "</query>"); | |
61 out.print("<count>" + count + "</count>"); | |
62 out.print("<result>"); | |
63 if (token != null) { | |
64 for (int i=0; i<token.size(); i++) { | |
65 Token t = token.get(i); | |
66 Term term = t.getTerm(); | |
67 int freq = t.getFreq(); | |
68 out.print("<token>"); | |
69 out.print("<text>" + term.text() + "</text>"); | |
70 if (freq != -1) | |
71 out.print("<freq>" + freq + "</freq>"); | |
72 out.print("</token>"); | |
73 } | |
74 } | |
75 out.print("</result>"); | |
76 out.print("</result>"); | |
77 out.close(); | |
78 } catch (ApplicationException e) { | |
79 throw new ServletException(e); | |
80 } | |
81 } | |
82 | |
83 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { | |
84 doGet(request, response); | |
85 } | |
86 | |
87 } |