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 */ 33 public class HttpPostUtil { 34 35 private static final Logger LOG = LoggerFactory.getLogger(HttpPostUtil.class); 36 37 /** 38 * Submits an HTTP POST request to the given target with the given request 39 * parameters. 40 * 41 * @param target 42 * url 43 * @param content 44 * of request parameters 45 * @return the response 46 * @throws Exception 47 * 48 * Usage: 49 * String target = "http://localhost:8080/OLE-DocSearch/select/"; 50 * 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"; 51 * String solrResponse = HttpPostUtil.postData(target, content); 52 */ 53 public static String postData(String target, String content) throws Exception { 54 LOG.debug("About Post URL:" + target + "\nCONTENT length:" + content.length()); 55 String response = ""; 56 URL url = new URL(target); 57 URLConnection conn = url.openConnection(); 58 conn.setDoInput(true); 59 conn.setDoOutput(true); 60 conn.setUseCaches(false); 61 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 62 63 Writer w = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); 64 w.write(content); 65 w.close(); 66 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 67 String temp; 68 while ((temp = in.readLine()) != null) { 69 response += temp + "\n"; 70 } 71 in.close(); 72 LOG.debug("Server response: " + response); 73 return response; 74 } 75 76 }