001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.servlet;
017:
018: import java.io.IOException;
019: import java.io.PrintWriter;
020: import java.util.logging.Logger;
021:
022: import javax.servlet.ServletException;
023: import javax.servlet.http.HttpServlet;
024: import javax.servlet.http.HttpServletRequest;
025: import javax.servlet.http.HttpServletResponse;
026:
027: import org.apache.solr.core.SolrCore;
028: import org.apache.solr.core.SolrException;
029: import org.apache.solr.request.QueryResponseWriter;
030: import org.apache.solr.request.SolrQueryResponse;
031: import org.apache.solr.request.SolrRequestHandler;
032:
033: /**
034: * @author yonik
035: * @author <a href='mailto:mbaranczak@epublishing.com'> Mike Baranczak </a>
036: */
037:
038: @Deprecated
039: public class SolrServlet extends HttpServlet {
040:
041: final Logger log = Logger.getLogger(SolrServlet.class.getName());
042: SolrCore core;
043:
044: public void init() throws ServletException {
045: log.info("SolrServlet.init()");
046: core = SolrCore.getSolrCore();
047: log.info("SolrServlet.init() done");
048: }
049:
050: public void doPost(HttpServletRequest request,
051: HttpServletResponse response) throws ServletException,
052: IOException {
053: doGet(request, response);
054: }
055:
056: public void doGet(HttpServletRequest request,
057: HttpServletResponse response) throws ServletException,
058: IOException {
059: SolrServletRequest solrReq = new SolrServletRequest(core,
060: request);
061: ;
062: SolrQueryResponse solrRsp = new SolrQueryResponse();
063: try {
064:
065: SolrRequestHandler handler = core.getRequestHandler(solrReq
066: .getQueryType());
067: if (handler == null) {
068: log.warning("Unknown Request Handler '"
069: + solrReq.getQueryType() + "' :" + solrReq);
070: throw new SolrException(
071: SolrException.ErrorCode.BAD_REQUEST,
072: "Unknown Request Handler '"
073: + solrReq.getQueryType() + "'", true);
074: }
075: core.execute(handler, solrReq, solrRsp);
076: if (solrRsp.getException() == null) {
077: QueryResponseWriter responseWriter = core
078: .getQueryResponseWriter(solrReq);
079: response.setContentType(responseWriter.getContentType(
080: solrReq, solrRsp));
081: PrintWriter out = response.getWriter();
082: responseWriter.write(out, solrReq, solrRsp);
083: } else {
084: Exception e = solrRsp.getException();
085: int rc = 500;
086: if (e instanceof SolrException) {
087: rc = ((SolrException) e).code();
088: }
089: sendErr(rc, SolrException.toStr(e), request, response);
090: }
091: } catch (SolrException e) {
092: if (!e.logged)
093: SolrException.log(log, e);
094: sendErr(e.code(), SolrException.toStr(e), request, response);
095: } catch (Throwable e) {
096: SolrException.log(log, e);
097: sendErr(500, SolrException.toStr(e), request, response);
098: } finally {
099: // This releases the IndexReader associated with the request
100: solrReq.close();
101: }
102: }
103:
104: final void sendErr(int rc, String msg, HttpServletRequest request,
105: HttpServletResponse response) {
106: try {
107: // hmmm, what if this was already set to text/xml?
108: try {
109: response
110: .setContentType(QueryResponseWriter.CONTENT_TYPE_TEXT_UTF8);
111: // response.setCharacterEncoding("UTF-8");
112: } catch (Exception e) {
113: }
114: try {
115: response.setStatus(rc);
116: } catch (Exception e) {
117: }
118: PrintWriter writer = response.getWriter();
119: writer.write(msg);
120: } catch (IOException e) {
121: SolrException.log(log, e);
122: }
123: }
124: }
|