001 /* 002 * Copyright 2011 The Kuali Foundation. 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.ole.docstore.discovery.util; 017 018 import org.slf4j.Logger; 019 import org.slf4j.LoggerFactory; 020 021 import java.io.BufferedReader; 022 import java.io.InputStreamReader; 023 import java.io.OutputStreamWriter; 024 import java.io.Writer; 025 import java.net.URL; 026 import java.net.URLConnection; 027 028 029 /** 030 * Utility class for simulating an HTTP POST request. 031 * 032 */ 033 public class HttpPostUtil { 034 035 private static final Logger LOG = LoggerFactory.getLogger(HttpPostUtil.class); 036 037 /** 038 * Submits an HTTP POST request to the given target with the given request 039 * parameters. 040 * 041 * @param target 042 * url 043 * @param content 044 * of request parameters 045 * @return the response 046 * @throws Exception 047 * 048 * Usage: 049 * String target = "http://localhost:8080/OLE-DocSearch/select/"; 050 * 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"; 051 * String solrResponse = HttpPostUtil.postData(target, content); 052 */ 053 public static String postData(String target, String content) throws Exception { 054 LOG.debug("About Post URL:" + target + "\nCONTENT length:" + content.length()); 055 String response = ""; 056 URL url = new URL(target); 057 URLConnection conn = url.openConnection(); 058 conn.setDoInput(true); 059 conn.setDoOutput(true); 060 conn.setUseCaches(false); 061 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 062 063 Writer w = new OutputStreamWriter(conn.getOutputStream(), "UTF-8"); 064 w.write(content); 065 w.close(); 066 BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 067 String temp; 068 while ((temp = in.readLine()) != null) { 069 response += temp + "\n"; 070 } 071 in.close(); 072 LOG.debug("Server response: " + response); 073 return response; 074 } 075 076 }