List of usage examples for org.apache.commons.httpclient.methods FileRequestEntity FileRequestEntity
public FileRequestEntity(final File file, final String contentType)
From source file:org.ala.spatial.util.UploadSpatialResource.java
public static String loadResource(String url, String extra, String username, String password, String resourcepath) {/*from w w w .ja v a 2 s . c o m*/ String output = ""; HttpClient client = new HttpClient(); client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); File input = new File(resourcepath); PutMethod put = new PutMethod(url); put.setDoAuthentication(true); //put.addRequestHeader("Content-type", "application/zip"); // Request content will be retrieved directly // from the input stream RequestEntity entity = new FileRequestEntity(input, "application/zip"); put.setRequestEntity(entity); // Execute the request try { int result = client.executeMethod(put); // get the status code System.out.println("Response status code: " + result); // Display response System.out.println("Response body: "); System.out.println(put.getResponseBodyAsString()); output += result; } catch (Exception e) { System.out.println("Something went wrong with UploadSpatialResource"); e.printStackTrace(System.out); } finally { // Release current connection to the connection pool once you are done put.releaseConnection(); } return output; }
From source file:org.ala.spatial.util.UploadSpatialResource.java
public static String loadSld(String url, String extra, String username, String password, String resourcepath) { System.out.println("loadSld url:" + url); System.out.println("path:" + resourcepath); String output = ""; HttpClient client = new HttpClient(); client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); File input = new File(resourcepath); PutMethod put = new PutMethod(url); put.setDoAuthentication(true);/*from w w w .j a va 2s . c om*/ // Request content will be retrieved directly // from the input stream RequestEntity entity = new FileRequestEntity(input, "application/vnd.ogc.sld+xml"); put.setRequestEntity(entity); // Execute the request try { int result = client.executeMethod(put); // get the status code System.out.println("Response status code: " + result); // Display response System.out.println("Response body: "); System.out.println(put.getResponseBodyAsString()); output += result; } catch (Exception e) { System.out.println("Something went wrong with UploadSpatialResource"); e.printStackTrace(System.out); } finally { // Release current connection to the connection pool once you are done put.releaseConnection(); } return output; }
From source file:org.ala.spatial.util.UploadSpatialResource.java
public static String loadCreateStyle(String url, String extra, String username, String password, String name) { System.out.println("loadCreateStyle url:" + url); System.out.println("name:" + name); String output = ""; HttpClient client = new HttpClient(); client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); PostMethod post = new PostMethod(url); post.setDoAuthentication(true);/*www .j av a 2 s . co m*/ // Execute the request try { // Request content will be retrieved directly // from the input stream File file = File.createTempFile("sld", "xml"); System.out.println("file:" + file.getPath()); FileWriter fw = new FileWriter(file); fw.append("<style><name>" + name + "</name><filename>" + name + "</filename></style>"); fw.close(); RequestEntity entity = new FileRequestEntity(file, "text/xml"); post.setRequestEntity(entity); int result = client.executeMethod(post); // get the status code System.out.println("Response status code: " + result); // Display response System.out.println("Response body: "); System.out.println(post.getResponseBodyAsString()); output += result; } catch (Exception e) { System.out.println("Something went wrong with UploadSpatialResource"); e.printStackTrace(System.out); } finally { // Release current connection to the connection pool once you are done post.releaseConnection(); } return output; }
From source file:org.ala.spatial.util.UploadSpatialResource.java
public static String assignSld(String url, String extra, String username, String password, String data) { System.out.println("assignSld url:" + url); System.out.println("data:" + data); String output = ""; HttpClient client = new HttpClient(); client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); PutMethod put = new PutMethod(url); put.setDoAuthentication(true);/*from w ww .j a v a 2 s . c o m*/ // Execute the request try { // Request content will be retrieved directly // from the input stream File file = File.createTempFile("sld", "xml"); System.out.println("file:" + file.getPath()); FileWriter fw = new FileWriter(file); fw.append(data); fw.close(); RequestEntity entity = new FileRequestEntity(file, "text/xml"); put.setRequestEntity(entity); int result = client.executeMethod(put); // get the status code System.out.println("Response status code: " + result); // Display response System.out.println("Response body: "); System.out.println(put.getResponseBodyAsString()); output += result; } catch (Exception e) { System.out.println("Something went wrong with UploadSpatialResource"); e.printStackTrace(System.out); } finally { // Release current connection to the connection pool once you are done put.releaseConnection(); } return output; }
From source file:org.ala.spatial.util.UploadSpatialResource.java
/** * sends a PUT or POST call to a URL using authentication and including a * file upload//from w w w . j a va 2 s. c o m * * @param type one of UploadSpatialResource.PUT for a PUT call or * UploadSpatialResource.POST for a POST call * @param url URL for PUT/POST call * @param username account username for authentication * @param password account password for authentication * @param resourcepath local path to file to upload, null for no file to * upload * @param contenttype file MIME content type * @return server response status code as String or empty String if * unsuccessful */ public static String httpCall(int type, String url, String username, String password, String resourcepath, String contenttype) { String output = ""; HttpClient client = new HttpClient(); client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); RequestEntity entity = null; if (resourcepath != null) { File input = new File(resourcepath); entity = new FileRequestEntity(input, contenttype); } HttpMethod call = null; ; if (type == PUT) { PutMethod put = new PutMethod(url); put.setDoAuthentication(true); if (entity != null) { put.setRequestEntity(entity); } call = put; } else if (type == POST) { PostMethod post = new PostMethod(url); if (entity != null) { post.setRequestEntity(entity); } call = post; } else { SpatialLogger.log("UploadSpatialResource", "invalid type: " + type); return output; } // Execute the request try { int result = client.executeMethod(call); output += result; } catch (Exception e) { SpatialLogger.log("UploadSpatialResource", "failed upload to: " + url); } finally { // Release current connection to the connection pool once you are done call.releaseConnection(); } return output; }
From source file:org.apache.camel.component.http.HttpProducer.java
/** * Creates a holder object for the data to send to the remote server. * * @param exchange the exchange with the IN message with data to send * @return the data holder/*from www.j a va 2 s .c o m*/ * @throws CamelExchangeException is thrown if error creating RequestEntity */ protected RequestEntity createRequestEntity(Exchange exchange) throws CamelExchangeException { Message in = exchange.getIn(); if (in.getBody() == null) { return null; } RequestEntity answer = in.getBody(RequestEntity.class); if (answer == null) { try { Object data = in.getBody(); if (data != null) { String contentType = ExchangeHelper.getContentType(exchange); if (contentType != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) { // serialized java object Serializable obj = in.getMandatoryBody(Serializable.class); // write object to output stream ByteArrayOutputStream bos = new ByteArrayOutputStream(); HttpHelper.writeObjectToStream(bos, obj); answer = new ByteArrayRequestEntity(bos.toByteArray(), HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT); IOHelper.close(bos); } else if (data instanceof File || data instanceof GenericFile) { // file based (could potentially also be a FTP file etc) File file = in.getBody(File.class); if (file != null) { answer = new FileRequestEntity(file, contentType); } } else if (data instanceof String) { // be a bit careful with String as any type can most likely be converted to String // so we only do an instanceof check and accept String if the body is really a String // do not fallback to use the default charset as it can influence the request // (for example application/x-www-form-urlencoded forms being sent) String charset = IOHelper.getCharsetName(exchange, false); answer = new StringRequestEntity((String) data, contentType, charset); } // fallback as input stream if (answer == null) { // force the body as an input stream since this is the fallback InputStream is = in.getMandatoryBody(InputStream.class); answer = new InputStreamRequestEntity(is, contentType); } } } catch (UnsupportedEncodingException e) { throw new CamelExchangeException("Error creating RequestEntity from message body", exchange, e); } catch (IOException e) { throw new CamelExchangeException("Error serializing message body", exchange, e); } } return answer; }
From source file:org.apache.cxf.systest.jaxrs.JAXRSClientServerBookTest.java
private void doAddBook(String address) throws Exception { File input = new File(getClass().getResource("resources/add_book.txt").toURI()); PostMethod post = new PostMethod(address); post.setRequestHeader("Content-Type", "application/xml"); RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); post.setRequestEntity(entity);//from ww w. j a v a 2 s . co m HttpClient httpclient = new HttpClient(); try { int result = httpclient.executeMethod(post); assertEquals(200, result); InputStream expected = getClass().getResourceAsStream("resources/expected_add_book.txt"); assertEquals(stripXmlInstructionIfNeeded(getStringFromInputStream(expected)), stripXmlInstructionIfNeeded(post.getResponseBodyAsString())); } finally { // Release current connection to the connection pool once you are done post.releaseConnection(); } }
From source file:org.apache.cxf.systest.jaxrs.JAXRSClientServerBookTest.java
@Test public void testUpdateBook() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/books"; File input = new File(getClass().getResource("resources/update_book.txt").toURI()); PutMethod put = new PutMethod(endpointAddress); RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); put.setRequestEntity(entity);//from ww w . ja va 2 s . com HttpClient httpclient = new HttpClient(); try { int result = httpclient.executeMethod(put); assertEquals(200, result); InputStream expected = getClass().getResourceAsStream("resources/expected_update_book.txt"); assertEquals(stripXmlInstructionIfNeeded(getStringFromInputStream(expected)), stripXmlInstructionIfNeeded(getStringFromInputStream(put.getResponseBodyAsStream()))); } finally { // Release current connection to the connection pool once you are // done put.releaseConnection(); } }
From source file:org.apache.cxf.systest.jaxrs.JAXRSClientServerBookTest.java
@Test public void testUpdateBookWithDom() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/bookswithdom"; File input = new File(getClass().getResource("resources/update_book.txt").toURI()); PutMethod put = new PutMethod(endpointAddress); RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); put.setRequestEntity(entity);//from w w w . ja va2s . c o m HttpClient httpclient = new HttpClient(); try { int result = httpclient.executeMethod(put); assertEquals(200, result); String resp = put.getResponseBodyAsString(); InputStream expected = getClass().getResourceAsStream("resources/update_book.txt"); String s = getStringFromInputStream(expected); //System.out.println(resp); //System.out.println(s); assertTrue(resp.indexOf(s) >= 0); } finally { // Release current connection to the connection pool once you are // done put.releaseConnection(); } }
From source file:org.apache.cxf.systest.jaxrs.JAXRSClientServerBookTest.java
@Test public void testUpdateBookWithJSON() throws Exception { String endpointAddress = "http://localhost:" + PORT + "/bookstore/bookswithjson"; File input = new File(getClass().getResource("resources/update_book_json.txt").toURI()); PutMethod put = new PutMethod(endpointAddress); RequestEntity entity = new FileRequestEntity(input, "application/json; charset=ISO-8859-1"); put.setRequestEntity(entity);/*from w ww. j a v a2s. com*/ HttpClient httpclient = new HttpClient(); try { int result = httpclient.executeMethod(put); assertEquals(200, result); InputStream expected = getClass().getResourceAsStream("resources/expected_update_book.txt"); assertEquals(stripXmlInstructionIfNeeded(getStringFromInputStream(expected)), stripXmlInstructionIfNeeded(getStringFromInputStream(put.getResponseBodyAsStream()))); } finally { // Release current connection to the connection pool once you are // done put.releaseConnection(); } }