📄 dwservlet.java
字号:
package org.apache.dwexample.servlet;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import java.io.IOException;import java.io.StringReader;import java.net.URLEncoder;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Collections;import java.util.Date;import java.util.HashMap;import java.util.List;import java.util.Locale;import java.util.Map;import java.util.TimeZone;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.HttpStatus;import org.apache.commons.httpclient.methods.GetMethod;import org.apache.commons.httpclient.methods.PostMethod;import org.apache.commons.httpclient.methods.RequestEntity;import org.apache.commons.httpclient.methods.StringRequestEntity;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.InputSource;import org.xml.sax.SAXException;/** * Copyright 2005 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. *//** * The DWServlet is responsible for taking in commands from the sample developerWorks application and converting them to * well-formed Solr commands, which can be viewed and then submitted to Solr for processing. * <p/> * This code is for demonstration purposes only and is not intended for production use. */public class DWServlet extends HttpServlet{ private static final String TYPE = "type"; private static final String URL = "url"; private static final String PUBLISHED = "published"; private static final String CONTENT = "content"; private static final String RATING = "rating"; private static final String CREATION_DATE = "creationDate"; private static final String KEYWORDS = "keywords"; private static final String TITLE = "title"; public static final String LINE_SEP = System.getProperty("line.separator"); private static SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy"); private static SimpleDateFormat solrFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS", Locale.US); public static TimeZone UTC = TimeZone.getTimeZone("UTC"); private int portNumber = 8080; private static final String SHOW_INDEX_XML_COMMAND_JSP = "/showIndexXMLCommand.jsp"; private static final String SHOW_SOLR_RESULTS_JSP = "/showSolrResults.jsp"; private String solrLocation; private static final String SHOW_QUERY_COMMAND_JSP = "/showQueryCommand.jsp"; private static final String FACET_RESULTS_JSP = "facetResults.jsp"; private DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); /** * Process the incoming requests, based on the "type" passed in as a hidden field on most pages. * @param request * @param response * @throws ServletException * @throws IOException */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String type = request.getParameter(TYPE); if ("add".equals(type)) { //Create the Solr Document. Real apps should probably use a DocumentBuilder processAdd(request, response); } else if ("commit".equals(type)) { processCommit(request, response); } else if ("optimize".equals(type)) { processOptimizeCommand(request, response); } else if ("search".equals(type)) { processSearch(request, response); } else if ("delete".equals(type)) { processDelete(request, response); } else if ("submitCommand".equals(type)) { processSubmitCommand(request, response); } else if ("submitQuery".equals(type)) { processSubmitQuery(request, response); } else if ("facet".equals(type)) { try { processFacetRequest(request, response); } catch (ParserConfigurationException e) { throw new ServletException(e); } catch (SAXException e) { throw new ServletException(e); } } else { throw new ServletException("Invalid Request Type: " + type); } } //Indexing Related commands /** * Create an add command, made up of fields in a document. * * @param request * @param response * @throws ServletException * @throws IOException */ private void processAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { StringBuilder command = new StringBuilder(); //Probably should use a DOM based construction, but this is simpler for now String url = request.getParameter(URL); command.append("<add>").append(LINE_SEP).append("<doc>") .append("<field name=\"url\">").append(url).append("</field>").append(LINE_SEP); String title = request.getParameter(TITLE); if (title != null && title.equals("") == false) { command.append("<field name=\"title\">").append(title).append("</field>").append(LINE_SEP); } String keywordsStr = request.getParameter(KEYWORDS); if (keywordsStr != null && keywordsStr.equals("") == false) { command.append("<field name=\"keywords\">").append(keywordsStr).append("</field>").append(LINE_SEP); } String dateStr = request.getParameter(CREATION_DATE); if (dateStr != null && dateStr.equals("") == false) { Date date; try { date = formatter.parse(dateStr); } catch (ParseException e) { throw new ServletException(e); } //The Solr DateField wants dates formatted in a particular way. //convert our date format to Solr DateField format: yyyy-MM-dd'T'HH:mm:ss.SSSZ dateStr = solrFormatter.format(date); //dateStr = dateStr.replace("T", "'T'"); command.append("<field name=\"creationDate\">").append(dateStr).append("Z").append("</field>").append(LINE_SEP); } String ratingStr = request.getParameter(RATING); if (ratingStr != null && ratingStr.equals("") == false) { command.append("<field name=\"rating\">").append(ratingStr).append("</field>").append(LINE_SEP); } String content = request.getParameter(CONTENT); if (content != null && content.equals("") == false) { command.append("<field name=\"content\">").append(content).append("</field>").append(LINE_SEP); } String publishedStr = request.getParameter(PUBLISHED); if (publishedStr != null && publishedStr.equals("") == false) { command.append("<field name=\"published\">").append(publishedStr).append("</field>").append(LINE_SEP); } command.append("</doc>").append(LINE_SEP); command.append("</add>").append(LINE_SEP); processIndexCommand(request, response, command.toString()); } /** * Create a commit command * * @param request * @param response * @throws IOException * @throws ServletException */ private void processCommit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String command = "<commit/>"; processIndexCommand(request, response, command); } /** * Create an optimize command to send to Solr and display it on the SHOW_XMLCOMMAND_JSP page * * @param request * @param response * @throws IOException * @throws ServletException */ private void processOptimizeCommand(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String command = "<optimize/>"; processIndexCommand(request, response, command); } /** * Delete by ID. This app does not support delete by query at this point, even though Solr does. * @param request * @param response * @throws IOException * @throws ServletException */ private void processDelete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String urlToDelete = request.getParameter("deleteURL"); String command = "<delete>" + LINE_SEP + "<id>" + urlToDelete + LINE_SEP + "</id></delete>"; processIndexCommand(request, response, command); } //SEARCH Related Commands /** * Build a Search request for Solr out of the fields entered by the user. *<p/> * See http://wiki.apache.org/solr/CoreQueryParameters, http://wiki.apache.org/solr/CommonQueryParameters, http://wiki.apache.org/solr/HighlightingParameters * and http://wiki.apache.org/solr/SolrRequestHandler * * @param request * @param response * @throws ServletException * @throws IOException */ private void processSearch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { StringBuilder query = new StringBuilder(); String url = request.getParameter(URL); if (url != null && url.equals("") == false) { //Escape the colon in the URL, as it is a reserved token in Lucene url = url.replace(":", "\\:"); query.append(URL).append(":").append(URLEncoder.encode(url, "UTF-8")); } String title = request.getParameter(TITLE); if (title != null && title.equals("") == false) { addSpaceToQuery(query); query.append(TITLE).append(":").append(URLEncoder.encode(title, "UTF-8")); } String keywords = request.getParameter(KEYWORDS); if (keywords != null && keywords.equals("") == false) { addSpaceToQuery(query); query.append(KEYWORDS).append(":").append(URLEncoder.encode(keywords, "UTF-8")); } String dateStr = request.getParameter(CREATION_DATE); if (dateStr != null && dateStr.equals("") == false) { Date date; try { date = formatter.parse(dateStr); } catch (ParseException e) { throw new ServletException(e); } //convert our date format to Solr DateField format: yyyy-MM-dd'T'HH:mm:ss.SSSZ dateStr = solrFormatter.format(date); //dateStr = dateStr.replace("T", "'T'"); addSpaceToQuery(query); query.append(CREATION_DATE).append(":").append(URLEncoder.encode(dateStr, "UTF-8")); } String ratingStr = request.getParameter(RATING); if (ratingStr != null && ratingStr.equals("") == false) { addSpaceToQuery(query); query.append(RATING).append(":").append(URLEncoder.encode(ratingStr, "UTF-8")); } String content = request.getParameter(CONTENT); if (content != null && content.equals("") == false) { addSpaceToQuery(query); query.append(CONTENT).append(":").append(URLEncoder.encode(content, "UTF-8")); } String published = request.getParameter(PUBLISHED); if (published != null && published.equals("") == false) { addSpaceToQuery(query); query.append(PUBLISHED).append(":").append(published); } //Handle any sorting as part of the "q" parameter String sort = request.getParameter("sort"); //only adding sort if it is not score, since score is the default if (sort != null && sort.equals("") == false && sort.equals("score") == false) { query.append("; ").append(sort); //only add direction if we are sorting String direction = request.getParameter("direction"); if (direction != null && direction.equals("") == false) { query.append(" ").append(direction); } else { query.append(" desc"); } } //Set the default operator to use for boolean queries String operator = request.getParameter("operator"); if (operator != null && operator.equals("AND")) { query.append("&q.op=").append("AND"); } /*else { //DEFAULT IS OR, DO NOTHING }*/ String start = request.getParameter("start"); if (start != null && start.equals("") == false) { query.append("&start=").append(start); } String numResults = request.getParameter("numResults"); if (numResults != null && numResults.equals("") == false) { query.append("&rows=").append(numResults); } String highlight = request.getParameter("highlight"); if (highlight != null && highlight.equals("") == false) { query.append("&hl=true"); } //Add this on to the beginning now so that addSpaceToQuery works properly query.insert(0, "fl=*,score&q="); processQueryCommand(request, response, query.toString(), SHOW_QUERY_COMMAND_JSP); } /** * Create a Facet Query request. A facet request is similar to a regular search query, plus it has parameters related to where * to get the facets from, etc. * <p/> * See http://wiki.apache.org/solr/SolrFacetingOverview and http://wiki.apache.org/solr/SimpleFacetParameters * @param request * @param response * @throws ServletException * @throws IOException * @throws ParserConfigurationException * @throws SAXException */ private void processFacetRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ParserConfigurationException, SAXException { String all = request.getParameter("all"); if (all != null && all.equals("") == false) { StringBuilder finalQuery = new StringBuilder("fl=*,score&facet=true&q="); String facetField = request.getParameter("facet.field"); finalQuery.append(URLEncoder.encode(all, "UTF-8")); if (facetField != null && facetField.equals("") == false) { finalQuery.append("&facet.field=").append(URLEncoder.encode(facetField, "UTF-8"));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -