1   /***
2    * Copyright 2005 The Apache Software Foundation
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  
17  // Changed package name. St.Ack
18  package org.archive.access.nutch;
19  
20  // Added by St.Ack.
21  import java.io.BufferedReader;
22  import java.io.IOException;
23  import java.io.UnsupportedEncodingException;
24  import java.security.Principal;
25  import java.util.Enumeration;
26  import java.util.Locale;
27  import java.util.Map;
28  
29  import javax.servlet.RequestDispatcher;
30  import javax.servlet.ServletConfig;
31  import javax.servlet.ServletException;
32  import javax.servlet.ServletInputStream;
33  import javax.servlet.http.Cookie;
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  import javax.servlet.http.HttpSession;
37  
38  import org.apache.hadoop.conf.Configuration;
39  import org.apache.nutch.searcher.OpenSearchServlet;
40  import org.apache.nutch.util.NutchConfiguration;
41   
42  
43  /***
44   * Subclass of OpenSearchServlet from nutch.
45   * Adds in the Nutchwax plugins and does special encoding if
46   * there is a 'exacturl' in the query string parameter.
47   * @author stack
48   */
49  public class NutchwaxOpenSearchServlet extends OpenSearchServlet {
50  	private static final long serialVersionUID = -6009645870609220838L;
51  
52  	public void init(ServletConfig config) throws ServletException {
53          // Put our configuration into place in the servlet context ahead
54          // of the query done by OpenSearchServlet#init so it finds our
55          // Configuration and doesn't create its own -- we're faking it out.
56  		// Do same for nutchbean.  Add our NutchwaxBean into place instead.
57          Configuration conf = NutchwaxConfiguration.
58          	getConfiguration(config.getServletContext());
59          config.getServletContext().
60          	setAttribute(NutchConfiguration.class.getName(), conf);
61          try {
62          	NutchwaxBean.get(config.getServletContext(), conf);
63          } catch (IOException e) {
64              throw new ServletException(e);
65          }
66          super.init(config);
67      }
68  
69      public void doGet(final HttpServletRequest request,
70              final HttpServletResponse response)
71      throws ServletException, IOException {
72          // Call super method passing an overridden version of
73      	// HttpServletRequest so we can preprocess the query string whenever
74      	// a call to #getParameter adding our exacturl encoding.  See
75      	// NutchwaxQuery for why we have to do this.
76          super.doGet(new HttpServletRequest() {
77              public String getParameter(String s) {
78                  if (s == null || !s.equals("query")) {
79                      return request.getParameter(s);
80                  }
81                  String queryString = request.getParameter(s);    
82                  if (queryString != null) {
83                      queryString = NutchwaxQuery.encodeExacturl(queryString);
84                  }
85                  return queryString;
86              }
87  
88              public String getAuthType() {
89                  return request.getAuthType();
90              }
91  
92              public Cookie[] getCookies() {
93                  return request.getCookies();
94              }
95  
96              public long getDateHeader(String s) {
97                  return request.getDateHeader(s);
98              }
99  
100             public String getHeader(String s) {
101                 return request.getHeader(s);
102             }
103 
104             public Enumeration getHeaders(String s) {
105                 return request.getHeaders(s);
106             }
107 
108             public Enumeration getHeaderNames() {
109                 return request.getHeaderNames();
110             }
111 
112             public int getIntHeader(String s) {
113                 return request.getIntHeader(s);
114             }
115 
116             public String getMethod() {
117                 return request.getMethod();
118             }
119 
120             public String getPathInfo() {
121                 return request.getPathInfo();
122             }
123 
124             public String getPathTranslated() {
125                 return request.getPathTranslated();
126             }
127 
128             public String getContextPath() {
129                 return request.getContextPath();
130             }
131 
132             public String getQueryString() {
133                 return request.getQueryString();
134             }
135 
136             public String getRemoteUser() {
137                 return request.getRemoteUser();
138             }
139 
140             public boolean isUserInRole(String s) {
141                 return request.isUserInRole(s);
142             }
143 
144             public Principal getUserPrincipal() {
145                 return request.getUserPrincipal();
146             }
147 
148             public String getRequestedSessionId() {
149                 return request.getRequestedSessionId();
150             }
151 
152             public String getRequestURI() {
153                 return request.getRequestURI();
154             }
155 
156             public StringBuffer getRequestURL() {
157                 return request.getRequestURL();
158             }
159 
160             public String getServletPath() {
161                 return request.getServletPath();
162             }
163 
164             public HttpSession getSession(boolean s) {
165                 return request.getSession(s);
166             }
167 
168             public HttpSession getSession() {
169                 return request.getSession();
170             }
171 
172             public boolean isRequestedSessionIdValid() {
173                 return request.isRequestedSessionIdValid();
174             }
175 
176             public boolean isRequestedSessionIdFromCookie() {
177                 return request.isRequestedSessionIdFromCookie();
178             }
179 
180             public boolean isRequestedSessionIdFromURL() {
181                 return request.isRequestedSessionIdFromURL();
182             }
183 
184             public boolean isRequestedSessionIdFromUrl() {
185             	throw new RuntimeException("Unimplemented");
186             }
187 
188             public Object getAttribute(String s) {
189                 return request.getAttribute(s);
190             }
191 
192             public Enumeration getAttributeNames() {
193                 return request.getAttributeNames();
194             }
195 
196             public String getCharacterEncoding() {
197                 return request.getCharacterEncoding();
198             }
199 
200             public void setCharacterEncoding(String s)
201                     throws UnsupportedEncodingException {
202                 request.setCharacterEncoding(s);
203             }
204 
205             public int getContentLength() {
206                 return request.getContentLength();
207             }
208 
209             public String getContentType() {
210                 return request.getContentType();
211             }
212 
213             public ServletInputStream getInputStream() throws IOException {
214                 return request.getInputStream();
215             }
216 
217             public Enumeration getParameterNames() {
218                 return request.getParameterNames();
219             }
220 
221             public String[] getParameterValues(String s) {
222                 return request.getParameterValues(s);
223             }
224 
225             public Map getParameterMap() {
226                 return request.getParameterMap();
227             }
228 
229             public String getProtocol() {
230                 return request.getProtocol();
231             }
232 
233             public String getScheme() {
234                 return request.getScheme();
235             }
236 
237             public String getServerName() {
238                 return request.getServerName();
239             }
240 
241             public int getServerPort() {
242                 return request.getServerPort();
243             }
244 
245             public BufferedReader getReader() throws IOException {
246                 return request.getReader();
247             }
248 
249             public String getRemoteAddr() {
250                 return request.getRemoteAddr();
251             }
252 
253             public String getRemoteHost() {
254                 return request.getRemoteHost();
255             }
256 
257             public void setAttribute(String a, Object b) {
258                 request.setAttribute(a, b);
259             }
260 
261             public void removeAttribute(String a) {
262                 request.removeAttribute(a);
263             }
264 
265             public Locale getLocale() {
266                 return request.getLocale();
267             }
268 
269             public Enumeration getLocales() {
270                 return request.getLocales();
271             }
272 
273             public boolean isSecure() {
274                 return request.isSecure();
275             }
276 
277             public RequestDispatcher getRequestDispatcher(String a) {
278                 return request.getRequestDispatcher(a);
279             }
280 
281             public String getRealPath(String a) {
282                 throw new RuntimeException("Unimplemented");
283             }
284 
285             public int getRemotePort() {
286                 return request.getRemotePort();
287             }
288 
289             public String getLocalName() {
290                 return request.getLocalName();
291             }
292 
293             public String getLocalAddr() {
294                 return request.getLocalAddr();
295             }
296 
297             public int getLocalPort() {
298                 return request.getLocalPort();
299             }
300         }, response);
301     }
302 }