List of usage examples for org.apache.commons.httpclient HttpException getMessage
public String getMessage()
From source file:de.hybris.platform.solrfacetsearch.integration.AbstractSolrTest.java
protected void checkStandaloneSolr(final String url) { try {//from ww w. j a v a2 s . c o m final HttpClient client = new HttpClient(); client.getParams().setParameter("http.protocol.content-charset", "UTF-8"); final GetMethod method = new GetMethod(url); method.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); client.executeMethod(method); } catch (final HttpException e) { Assert.fail("Cannot connect to remote standalone SOLR : " + e.getMessage()); } catch (final IOException e) { Assert.fail("Cannot connect to remote standalone SOLR : " + e.getMessage()); } }
From source file:autohit.call.modules.SimpleHttpModule.java
/** * Post method. It will set the target address for the client, as well as * clearing any state.// w ww.j ava2s . c o m * * @param url * the Url path, not to include protocol, address, and port (ie. * "/goats/index.html"). * @param nv * set of name/value pairs for the post. it can be empty. * @return the data from the page as a String * @throws CallException */ private String post(String url, Hashtable nv) throws CallException { if (started == false) { throw buildException("Tried to post when a session wasn't started.", CallException.CODE_MODULE_FAULT); } String result = null; String name; Object value; // Construct our method. PostMethod method = new PostMethod(url); method.setFollowRedirects(true); method.setStrictMode(false); //build the rest of the method try { // Construct the headers Enumeration eNV = nv.keys(); while (eNV.hasMoreElements()) { name = (String) eNV.nextElement(); value = nv.get(name); if (value instanceof String) { // Only take it if it is a string method.addParameter(name, (String) value); debug("ADD POST - name=" + name + " value=" + (String) value); } } //DEBUG debug("DUMP POST-------------------------------"); debug(method.toString()); debug("DUMP POST-------------------------------"); // Do it debug("(post)post=" + url); httpClient.executeMethod(method); // Process result result = method.getResponseBodyAsString(); log("(post)" + method.getStatusLine().toString() + " size=" + result.length()); } catch (HttpException he) { // Bad but not fatal error("(post)Error on connect to url " + url + ". Error=" + he.getMessage()); } catch (IOException ioe) { // Fatal throw buildException("(post)Unable to connect. Session is invalid.", CallException.CODE_MODULE_FAULT, ioe); } catch (Exception ex) { // Fatal throw buildException("(post)Serious general error.", CallException.CODE_MODULE_FAULT, ex); } finally { try { method.releaseConnection(); method.recycle(); } catch (Exception e) { // Already FUBAR } } return result; }
From source file:de.ingrid.portal.interfaces.impl.WMSInterfaceImpl.java
public void setWMCDocument(String wmc, String sessionId) { String urlStr = config.getString("interface_url", "http://localhost/mapbender/php/mod_portalCommunication_gt.php"); if (urlStr.indexOf("?") > 0) { urlStr = urlStr.concat("&PREQUEST=setWMC").concat("&PHPSESSID=" + sessionId); } else {/* w ww. j av a 2 s . c o m*/ urlStr = urlStr.concat("?PREQUEST=setWMC").concat("&PHPSESSID=" + sessionId); } // Create an instance of HttpClient. HttpClient client = new HttpClient(); // Create a method instance. PostMethod method = new PostMethod(urlStr); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(2, false)); NameValuePair[] data = { new NameValuePair("wmc", wmc) }; method.setRequestBody(data); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { log.error("Sending WMC faild for URL: " + urlStr); } // Read the response body. byte[] responseBody = method.getResponseBody(); if (log.isDebugEnabled()) { log.debug("MapBender Server Response: " + new String(responseBody)); } // Deal with the response. // Use caution: ensure correct character encoding and is not binary data Document document = DocumentHelper.parseText(new String(responseBody)); // check for valid server response String error = document.valueOf("//portal_communication/error"); if (error != null && error.length() > 0) { throw new Exception("WMS Server Error: " + error); } String success = document.valueOf("//portal_communication/success"); if (error == null || success.length() == 0) { throw new Exception("WMS Server Error: Cannot find success message from server. message was: " + new String(responseBody)); } } catch (HttpException e) { log.error("Fatal protocol violation: " + e.getMessage(), e); } catch (IOException e) { log.error("Fatal transport error: " + e.getMessage(), e); } catch (Exception e) { log.error("Fatal error: " + e.getMessage(), e); } finally { // Release the connection. method.releaseConnection(); } }
From source file:gr.upatras.ece.nam.fci.uop.UoPGWClient.java
/** * It makes a GET towards the gateway. The response is retrieved with the {@link PanlabGWClient#getResponse_stream()} ; * @author ctranoris/*from www. java2 s . c o m*/ * @param resourceInstance sets the name of the resource Instance, e.g.: uop.rubis_db-27 * @param ptmAlias sets the name of the resource Instance, e.g.: uop * @see PanlabGWClient#getResponse_stream() */ public void GETexecute(String resourceInstance, String ptmAlias) { HttpClient client = new HttpClient(); // resource instance is like uop.rubis_db-6 so we need to make it like // this /uop/uop.rubis_db-6 String ptm = ptmAlias; String url = uopGWAddress + "/" + ptm + "/" + resourceInstance; log.info("Request: " + url); // Create a method instance. GetMethod get = new GetMethod(url); get.setRequestHeader("User-Agent", userAgent); get.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); try { // execute the GET client.executeMethod(get); // print the status and response InputStream responseBody = get.getResponseBodyAsStream(); CopyInputStream cis = new CopyInputStream(responseBody); response_stream = cis.getCopy(); log.info("Response body=" + "\n" + convertStreamToString(response_stream)); response_stream.reset(); } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); e.printStackTrace(); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { // release any connection resources used by the method get.releaseConnection(); } }
From source file:gr.upatras.ece.nam.fci.uop.UoPGWClient.java
/** * It makes a GET towards the gateway for authorization * @author ctranoris//w w w . j a v a2 s.co m * @param username * @param password * @see PanlabGWClient#getResponse_stream() */ public String GETAuthorizationToken(String username, String password) { HttpClient client = new HttpClient(); String url = this.uopAuthUrl + "?username=" + username + "&password=" + password; log.info("Request: " + url); // Create a method instance. GetMethod get = new GetMethod(url); get.setRequestHeader("User-Agent", userAgent); get.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); try { // execute the GET int rescode = client.executeMethod(get); // print the status and response InputStream responseBody = get.getResponseBodyAsStream(); CopyInputStream cis = new CopyInputStream(responseBody); response_stream = cis.getCopy(); String s = convertStreamToString(response_stream); log.info("Response body=" + "\n" + s); response_stream.reset(); if (rescode == 200) return s; else return ""; } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); e.printStackTrace(); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { // release any connection resources used by the method get.releaseConnection(); } return ""; }
From source file:de.ingrid.portal.interfaces.impl.WMSInterfaceImpl.java
public String getWMCDocument(String sessionId) { String response = null;/*w ww.ja va 2 s. c o m*/ try { String urlStr = config.getString("interface_url", "http://localhost/mapbender/php/mod_portalCommunication_gt.php"); if (urlStr.indexOf("?") > 0) { urlStr = urlStr.concat("&PREQUEST=getWMC").concat("&PHPSESSID=" + sessionId); } else { urlStr = urlStr.concat("?PREQUEST=getWMC").concat("&PHPSESSID=" + sessionId); } if (log.isDebugEnabled()) { log.debug("MapBender Server Request: " + urlStr); } // Create an instance of HttpClient. HttpClient client = new HttpClient(); // Create a method instance. GetMethod method = new GetMethod(urlStr); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(2, false)); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { log.error("Requesting WMC faild for URL: " + urlStr); } // Read the response body. byte[] responseBody = method.getResponseBody(); response = new String(responseBody); } catch (HttpException e) { log.error("Fatal protocol violation: " + e.getMessage(), e); } catch (IOException e) { log.error("Fatal transport error: " + e.getMessage(), e); } finally { // Release the connection. method.releaseConnection(); } if (log.isDebugEnabled()) { log.debug("MapBender Server Response: " + response); } Document document = DocumentHelper.parseText(response); // check for valid server response String error = document.valueOf("//portal_communication/error"); if (error != null && error.length() > 0) { throw new Exception("MapBender Server Error: " + error); } String sessionMapBender = document.valueOf("//portal_communication/session"); if (sessionMapBender != null && !sessionMapBender.equals(sessionId)) { throw new Exception( "MapBender Server Error: session Id (" + sessionId + ") from request and session Id (" + sessionMapBender + ") from MapBender are not the same."); } String urlEncodedWmc = document.valueOf("//portal_communication/wmc"); return urlEncodedWmc; } catch (Exception e) { log.error(e.toString()); } return null; }
From source file:gr.upatras.ece.nam.fci.uop.UoPGWClient.java
/** * It makes a GET towards the FedWay gateway. The response is retrieved with the {@link PanlabGWClient#getResponse_stream()} ; * @author ctranoris/*from w ww. ja v a 2s. co m*/ * @param subject sets the name of the resource Instance, e.g.: uop.rubis_db-27 * @param ptmAlias sets the name of the resource Instance, e.g.: uop * @see PanlabGWClient#getResponse_stream() */ public void informFedWay(String fedway, String subject, String myDescription, String resourceid, Date start_ts, Date end_ts, String guid, String scenarioid, String scenarioName, String username) { ////http://nam.ece.upatras.gr/fedway/submit_event.php?subject=aResource&descr=myDescription&resourceid=123456&start_ts=2011-09-15%2017:00:00&end_ts=2011-09-17%2011:01:31&guid=guid5&scenarioid=scen1234&scenarioName=myScenario DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); HttpClient client = new HttpClient(); String url = ""; try { url = "subject=" + URLEncoder.encode(subject, "UTF-8") + "&descr=" + URLEncoder.encode(myDescription, "UTF-8") + "&resourceid=" + URLEncoder.encode(resourceid, "UTF-8") + "&start_ts=" + URLEncoder.encode(sdf.format(start_ts), "UTF-8") + //2011-09-15%2017:00:00 "&end_ts=" + URLEncoder.encode(sdf.format(end_ts), "UTF-8") + //2011-09-17%2011:01:31 "&guid=" + URLEncoder.encode(guid, "UTF-8") + "&scenarioid=" + URLEncoder.encode(scenarioid, "UTF-8") + "&scenarioName=" + URLEncoder.encode(scenarioName, "UTF-8") + "&username=" + URLEncoder.encode(username, "UTF-8"); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } url = fedway + "/submit_event.php?" + url; log.info("Request: " + url); // Create a method instance. GetMethod get = new GetMethod(url); get.setRequestHeader("User-Agent", userAgent); get.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); try { // execute the GET client.executeMethod(get); // print the status and response InputStream responseBody = get.getResponseBodyAsStream(); CopyInputStream cis = new CopyInputStream(responseBody); response_stream = cis.getCopy(); log.info("Response body=" + "\n" + convertStreamToString(response_stream)); response_stream.reset(); } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); e.printStackTrace(); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { // release any connection resources used by the method get.releaseConnection(); } }
From source file:gr.upatras.ece.nam.fci.uop.UoPGWClient.java
/** * It makes a GET towards the FedWay gateway. * @author ctranoris/*from www .j a va2s. co m*/ * @see PanlabGWClient#getResponse_stream() */ public String checkFedWay(String fedway, String subject, String myDescription, String resourceid, Date start_ts, Date end_ts, String guid, String scenarioid, String scenarioName, String username) { ////http://nam.ece.upatras.gr/fedway/submit_event.php?subject=aResource&descr=myDescription&resourceid=123456&start_ts=2011-09-15%2017:00:00&end_ts=2011-09-17%2011:01:31&guid=guid5&scenarioid=scen1234&scenarioName=myScenario DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); HttpClient client = new HttpClient(); String url = ""; try { url = "subject=" + URLEncoder.encode(subject, "UTF-8") + "&descr=" + URLEncoder.encode(myDescription, "UTF-8") + "&resourceid=" + URLEncoder.encode(resourceid, "UTF-8") + "&start_ts=" + URLEncoder.encode(sdf.format(start_ts), "UTF-8") + //2011-09-15%2017:00:00 "&end_ts=" + URLEncoder.encode(sdf.format(end_ts), "UTF-8") + //2011-09-17%2011:01:31 "&guid=" + URLEncoder.encode(guid, "UTF-8") + "&scenarioid=" + URLEncoder.encode(scenarioid, "UTF-8") + "&scenarioName=" + URLEncoder.encode(scenarioName, "UTF-8") + "&username=" + URLEncoder.encode(username, "UTF-8"); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } url = fedway + "/submit_event_check.php?" + url; log.info("Request: " + url); // Create a method instance. GetMethod get = new GetMethod(url); get.setRequestHeader("User-Agent", userAgent); get.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); try { // execute the GET client.executeMethod(get); // print the status and response InputStream responseBody = get.getResponseBodyAsStream(); CopyInputStream cis = new CopyInputStream(responseBody); response_stream = cis.getCopy(); log.info("Response body=" + "\n" + convertStreamToString(response_stream)); response_stream.reset(); return this.getResponse_streamAsString(); } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); e.printStackTrace(); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { // release any connection resources used by the method get.releaseConnection(); } return "FAILED"; }
From source file:gr.upatras.ece.nam.fci.uop.UoPGWClient.java
/** * It makes a DELETE towards the gateway * @author ctranoris//from w w w. jav a2s .com * @param resourceInstance sets the name of the resource Instance, e.g.: uop.rubis_db-27 * @param ptmAlias sets the name of the provider URI, e.g.: uop * @param content sets the name of the content; send in utf8 */ public void DELETEexecute(String resourceInstance, String ptmAlias, String content) { log.info("content body=" + "\n" + content); HttpClient client = new HttpClient(); String tgwcontent = content; // resource instance is like uop.rubis_db-6 so we need to make it like // this /uop/uop.rubis_db-6 String ptm = ptmAlias; String url = uopGWAddress + "/" + ptm + "/" + resourceInstance; log.info("Request: " + url); // Create a method instance. DeleteMethod delMethod = new DeleteMethod(url); delMethod.setRequestHeader("User-Agent", userAgent); delMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // Provide custom retry handler is necessary // delMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, // new DefaultHttpMethodRetryHandler(3, false)); // RequestEntity requestEntity = null; // try { // requestEntity = new StringRequestEntity(tgwcontent, // "application/x-www-form-urlencoded", "utf-8"); // } catch (UnsupportedEncodingException e1) { // e1.printStackTrace(); // } //delMethod.setRequestEntity(requestEntity); try { // Execute the method. int statusCode = client.executeMethod(delMethod); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + delMethod.getStatusLine()); } // Deal with the response. // Use caution: ensure correct character encoding and is not binary // data // print the status and response InputStream responseBody = delMethod.getResponseBodyAsStream(); CopyInputStream cis = new CopyInputStream(responseBody); response_stream = cis.getCopy(); log.info("Response body=" + "\n" + convertStreamToString(response_stream)); response_stream.reset(); // log.info("for address: " + url + " the response is:\n " // + post.getResponseBodyAsString()); } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { // Release the connection. delMethod.releaseConnection(); } }
From source file:gr.upatras.ece.nam.fci.uop.UoPGWClient.java
/** * It makes a POST towards the gateway/*from w ww. j a va2 s . co m*/ * @author ctranoris * @param resourceInstance sets the name of the resource Instance, e.g.: uop.rubis_db-27 * @param ptmAlias sets the name of the provider URI, e.g.: uop * @param content sets the name of the content; send in utf8 */ public boolean POSTExecute(String resourceInstance, String ptmAlias, String content) { boolean status = false; log.info("content body=" + "\n" + content); HttpClient client = new HttpClient(); String tgwcontent = content; // resource instance is like uop.rubis_db-6 so we need to make it like // this /uop/uop.rubis_db-6 String ptm = ptmAlias; String url = uopGWAddress + "/" + ptm + "/" + resourceInstance; log.info("Request: " + url); // Create a method instance. PostMethod post = new PostMethod(url); post.setRequestHeader("User-Agent", userAgent); post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); // Provide custom retry handler is necessary post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(0, false)); //HttpMethodParams. RequestEntity requestEntity = null; try { requestEntity = new StringRequestEntity(tgwcontent, "application/x-www-form-urlencoded", "utf-8"); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } post.setRequestEntity(requestEntity); try { // Execute the method. int statusCode = client.executeMethod(post); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + post.getStatusLine()); } // Deal with the response. // Use caution: ensure correct character encoding and is not binary // data // print the status and response InputStream responseBody = post.getResponseBodyAsStream(); CopyInputStream cis = new CopyInputStream(responseBody); response_stream = cis.getCopy(); log.info("Response body=" + "\n" + convertStreamToString(response_stream)); response_stream.reset(); // log.info("for address: " + url + " the response is:\n " // + post.getResponseBodyAsString()); status = true; } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); return false; } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); return false; } finally { // Release the connection. post.releaseConnection(); } return status; }