refactor /update to separate servlet, allow post of query to /select

git-svn-id: https://svn.apache.org/repos/asf/incubator/solr/trunk@392311 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yonik Seeley 2006-04-07 15:20:12 +00:00
parent 0f57b81cb1
commit 64047e0b46
4 changed files with 125 additions and 76 deletions

View File

@ -25,12 +25,22 @@
<servlet-class>org.apache.solr.servlet.SolrServlet</servlet-class> <servlet-class>org.apache.solr.servlet.SolrServlet</servlet-class>
<load-on-startup>0</load-on-startup> <load-on-startup>0</load-on-startup>
</servlet> </servlet>
<servlet>
<servlet-name>SolrUpdate</servlet-name>
<display-name>SolrUpdate</display-name>
<description>Solr Update Handler</description>
<servlet-class>org.apache.solr.servlet.SolrUpdateServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping> <servlet-mapping>
<servlet-name>SolrServer</servlet-name> <servlet-name>SolrServer</servlet-name>
<url-pattern>/select/*</url-pattern> <url-pattern>/select/*</url-pattern>
</servlet-mapping> </servlet-mapping>
<servlet-mapping> <servlet-mapping>
<servlet-name>SolrServer</servlet-name> <servlet-name>SolrUpdate</servlet-name>
<url-pattern>/update/*</url-pattern> <url-pattern>/update/*</url-pattern>
</servlet-mapping> </servlet-mapping>

View File

@ -37,55 +37,30 @@ import java.util.Set;
*/ */
public class SolrServlet extends HttpServlet { public class SolrServlet extends HttpServlet {
public static Logger log = Logger.getLogger(SolrServlet.class.getName()); final Logger log = Logger.getLogger(SolrServlet.class.getName());
public SolrCore core; SolrCore core;
private static String CONTENT_TYPE="text/xml;charset=UTF-8"; private static String CONTENT_TYPE="text/xml;charset=UTF-8";
XMLResponseWriter xmlResponseWriter; XMLResponseWriter xmlResponseWriter;
public void init() throws ServletException public void init() throws ServletException
{ {
String configDir=getServletContext().getInitParameter("solr.configDir");
String dataDir=getServletContext().getInitParameter("solr.dataDir");
log.info("user.dir=" + System.getProperty("user.dir")); log.info("user.dir=" + System.getProperty("user.dir"));
// TODO: find a way to allow configuration of the config and data
// directories other than using CWD. If it is done via servlet
// params, then we must insure that this init() run before any
// of the JSPs.
core = SolrCore.getSolrCore(); core = SolrCore.getSolrCore();
xmlResponseWriter=new XMLResponseWriter(); xmlResponseWriter=new XMLResponseWriter();
getServletContext().setAttribute("SolrServlet",this);
log.info("SolrServlet.init() done"); log.info("SolrServlet.init() done");
} }
public void destroy() { public void destroy() {
core.close(); core.close();
getServletContext().removeAttribute("SolrServlet");
super.destroy(); super.destroy();
} }
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// log.finer("Solr doPost()"); doGet(request,response);
// InputStream is = request.getInputStream();
BufferedReader requestReader = request.getReader();
response.setContentType(CONTENT_TYPE);
PrintWriter responseWriter = response.getWriter();
core.update(requestReader, responseWriter);
} }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// log.finer("Solr doGet: getQueryString:" + request.getQueryString());
SolrServletRequest solrReq =null; SolrServletRequest solrReq =null;
SolrQueryResponse solrRsp =null; SolrQueryResponse solrRsp =null;
try { try {
@ -143,50 +118,4 @@ public class SolrServlet extends HttpServlet {
return request.getParameter(param)!=null ? true : false; return request.getParameter(param)!=null ? true : false;
} }
}
class SolrServletRequest extends SolrQueryRequestBase {
final HttpServletRequest req;
public SolrServletRequest(SolrCore core, HttpServletRequest req) {
super(core);
this.req = req;
}
public String getParam(String name) {
return req.getParameter(name);
}
public String getParamString() {
StringBuilder sb = new StringBuilder(128);
try {
boolean first=true;
for (Map.Entry<String,String[]> entry : (Set<Map.Entry<String,String[]>>)req.getParameterMap().entrySet()) {
String key = entry.getKey();
String[] valarr = entry.getValue();
for (String val : valarr) {
if (!first) sb.append('&');
first=false;
sb.append(key);
sb.append('=');
StrUtils.partialURLEncodeVal(sb, val);
}
}
}
catch (Exception e) {
// should never happen... we only needed this because
// partialURLEncodeVal can throw an IOException, but it
// never will when adding to a StringBuilder.
throw new RuntimeException(e);
}
return sb.toString();
}
} }

View File

@ -0,0 +1,57 @@
package org.apache.solr.servlet;
import org.apache.solr.request.SolrQueryRequestBase;
import org.apache.solr.core.SolrCore;
import org.apache.solr.util.StrUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
import java.util.Set;
/**
* @author yonik
* @version $Id$
*/
class SolrServletRequest extends SolrQueryRequestBase {
final HttpServletRequest req;
public SolrServletRequest(SolrCore core, HttpServletRequest req) {
super(core);
this.req = req;
}
public String getParam(String name) {
return req.getParameter(name);
}
public String getParamString() {
StringBuilder sb = new StringBuilder(128);
try {
boolean first=true;
for (Map.Entry<String,String[]> entry : (Set<Map.Entry<String,String[]>>)req.getParameterMap().entrySet()) {
String key = entry.getKey();
String[] valarr = entry.getValue();
for (String val : valarr) {
if (!first) sb.append('&');
first=false;
sb.append(key);
sb.append('=');
StrUtils.partialURLEncodeVal(sb, val);
}
}
}
catch (Exception e) {
// should never happen... we only needed this because
// partialURLEncodeVal can throw an IOException, but it
// never will when adding to a StringBuilder.
throw new RuntimeException(e);
}
return sb.toString();
}
}

View File

@ -0,0 +1,53 @@
package org.apache.solr.servlet;/**
* Copyright 2006 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.apache.solr.core.SolrCore;
import org.apache.solr.core.SolrException;
import org.apache.solr.request.XMLResponseWriter;
import org.apache.solr.request.SolrQueryResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.util.logging.Logger;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.PrintWriter;
/**
* @author yonik
* @version $Id$
*/
public class SolrUpdateServlet extends HttpServlet {
final Logger log = Logger.getLogger(SolrUpdateServlet.class.getName());
private SolrCore core;
private static String CONTENT_TYPE="text/xml;charset=UTF-8";
XMLResponseWriter xmlResponseWriter;
public void init() throws ServletException
{
core = SolrCore.getSolrCore();
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BufferedReader requestReader = request.getReader();
response.setContentType(CONTENT_TYPE);
PrintWriter responseWriter = response.getWriter();
core.update(requestReader, responseWriter);
}
}