View Javadoc
1   /*
2    * Copyright 2011 The Kuali Foundation.
3    * 
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.ole.docstore.discovery.util;
17  
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  
21  import java.io.BufferedReader;
22  import java.io.InputStreamReader;
23  import java.io.OutputStreamWriter;
24  import java.io.Writer;
25  import java.net.URL;
26  import java.net.URLConnection;
27  
28  
29  /**
30   * Utility class for simulating an HTTP POST request.
31   */
32  public class HttpPostUtil {
33  
34      private static final Logger LOG = LoggerFactory.getLogger(HttpPostUtil.class);
35  
36      /**
37       * Submits an HTTP POST request to the given target with the given request
38       * parameters.
39       *
40       * @param target  url
41       * @param content of request parameters
42       * @return the response
43       * @throws Exception Usage:
44       *                   String target = "http://localhost:8080/OLE-DocSearch/select/";
45       *                   String content = "q=(ModifyingAgency:iul)&facet=true&facet.field=Price_f&facet.query=Price_f:[1 TO 50]&facet.query=Price_f:[51 TO 100]&facet.query=Price_f:[101 TO 200]&facet.query=Price_f:[201 TO 500]&facet.field=r_name_facetLetter&facet.query=r_name_facetLetter:[A TO C]&facet.query=r_name_facetLetter:[D TO F]&facet.query=r_name_facetLetter:[G TO I]&facet.query=r_name_facetLetter:[J TO L]&facet.query=r_name_facetLetter:[M TO O]&facet.query=r_name_facetLetter:[P TO R]&facet.query=r_name_facetLetter:[S TO U]&facet.query=r_name_facetLetter:[V TO Z]&facet.field=YearOfPublication&facet.query=YearOfPublication:[1900 TO 1950]&facet.query=YearOfPublication:[1951 TO 2000]&facet.query=YearOfPublication:[2000 TO 2011]&facet.field=r_NameOfPublisher_facetLetter&facet.query=r_NameOfPublisher_facetLetter:[A TO C]&facet.query=r_NameOfPublisher_facetLetter:[D TO F]&facet.query=r_NameOfPublisher_facetLetter:[G TO I]&facet.query=r_NameOfPublisher_facetLetter:[J TO L]&facet.query=r_NameOfPublisher_facetLetter:[M TO O]&facet.query=r_NameOfPublisher_facetLetter:[P TO R]&facet.query=r_NameOfPublisher_facetLetter:[S TO U]&facet.query=r_NameOfPublisher_facetLetter:[V TO Z]&facet.field=r_AddedEntryPersonalName_facetLetter&facet.query=r_AddedEntryPersonalName_facetLetter:[A TO C]&facet.query=r_AddedEntryPersonalName_facetLetter:[D TO F]&facet.query=r_AddedEntryPersonalName_facetLetter:[G TO I]&facet.query=r_AddedEntryPersonalName_facetLetter:[J TO L]&facet.query=r_AddedEntryPersonalName_facetLetter:[M TO O]&facet.query=r_AddedEntryPersonalName_facetLetter:[P TO R]&facet.query=r_AddedEntryPersonalName_facetLetter:[S TO U]&facet.query=r_AddedEntryPersonalName_facetLetter:[V TO Z]&wt=xslt&tr=response.xsl&hl.fl=ModifyingAgency,MainEntryPersonalName,FullerFormOfName,DatesAssociatedWithName,Title,RemainderOfTitle,StatementOfResponsibility,PlaceOfPublication,NameOfPublisher,DateOfPublication,Extent,Dimentions,GeneralNote,TopicalTermorgeographicnameElement,GeneralSubdivision,CorporateNameJurisdictionNameEntryElement,SubordinateUnit,AddedEntryPersonalName,999_i,999_p,999_u,ISBN,ISSN&&hl=true";
46       *                   String solrResponse = HttpPostUtil.postData(target, content);
47       */
48      public static String postData(String target, String content) throws Exception {
49          LOG.debug("About Post URL:" + target + "\nCONTENT length:" + content.length());
50          String response = "";
51          URL url = new URL(target);
52          URLConnection conn = url.openConnection();
53          conn.setDoInput(true);
54          conn.setDoOutput(true);
55          conn.setUseCaches(false);
56          conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
57  
58          Writer w = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
59          w.write(content);
60          w.close();
61          BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
62          String temp;
63          while ((temp = in.readLine()) != null) {
64              response += temp + "\n";
65          }
66          in.close();
67          LOG.debug("Server response: " + response);
68          return response;
69      }
70  
71  }