List of usage examples for org.apache.commons.httpclient.methods PutMethod setRequestEntity
public void setRequestEntity(RequestEntity paramRequestEntity)
From source file:org.apache.wink.itest.standard.JAXRSReaderTest.java
/** * Tests putting and then getting a Reader. * //from w w w.j a va2 s . c o m * @throws HttpException * @throws IOException */ public void testPutReader() throws HttpException, IOException { HttpClient client = new HttpClient(); PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/reader"); putMethod.setRequestEntity(new StringRequestEntity("wxyz", "char/array", "UTF-8")); try { client.executeMethod(putMethod); assertEquals(204, putMethod.getStatusCode()); } finally { putMethod.releaseConnection(); } GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/reader"); try { client.executeMethod(getMethod); assertEquals(200, getMethod.getStatusCode()); InputStream is = getMethod.getResponseBodyAsStream(); InputStreamReader isr = new InputStreamReader(is); char[] buffer = new char[1]; int read = 0; int offset = 0; while ((read = isr.read(buffer, offset, buffer.length - offset)) != -1) { offset += read; if (offset >= buffer.length) { buffer = ArrayUtils.copyOf(buffer, buffer.length * 2); } } char[] carr = ArrayUtils.copyOf(buffer, offset); int checkEOF = is.read(); assertEquals(-1, checkEOF); String str = new String(carr); assertEquals("wxyz", str); String contentType = (getMethod.getResponseHeader("Content-Type") == null) ? null : getMethod.getResponseHeader("Content-Type").getValue(); assertNotNull(contentType, contentType); Header contentLengthHeader = getMethod.getResponseHeader("Content-Length"); assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(), contentLengthHeader); } finally { getMethod.releaseConnection(); } }
From source file:org.apache.wink.itest.standard.JAXRSReaderTest.java
/** * @throws HttpException/* w w w . ja v a2s. c om*/ * @throws IOException */ public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException, IOException { HttpClient client = new HttpClient(); PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/reader"); putMethod.setRequestEntity(new StringRequestEntity("wxyz", "char/array", "UTF-8")); try { client.executeMethod(putMethod); assertEquals(204, putMethod.getStatusCode()); } finally { putMethod.releaseConnection(); } GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/reader"); getMethod.addRequestHeader("Accept", "mytype/subtype"); try { client.executeMethod(getMethod); assertEquals(200, getMethod.getStatusCode()); InputStream is = getMethod.getResponseBodyAsStream(); InputStreamReader isr = new InputStreamReader(is); char[] buffer = new char[1]; int read = 0; int offset = 0; while ((read = isr.read(buffer, offset, buffer.length - offset)) != -1) { offset += read; if (offset >= buffer.length) { buffer = ArrayUtils.copyOf(buffer, buffer.length * 2); } } char[] carr = ArrayUtils.copyOf(buffer, offset); int checkEOF = is.read(); assertEquals(-1, checkEOF); String str = new String(carr); assertEquals("wxyz", str); assertEquals("mytype/subtype", getMethod.getResponseHeader("Content-Type").getValue()); Header contentLengthHeader = getMethod.getResponseHeader("Content-Length"); assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(), contentLengthHeader); } finally { getMethod.releaseConnection(); } }
From source file:org.apache.wink.itest.standard.JAXRSSourceTest.java
/** * Tests putting and then getting a source. * //from www. ja v a 2 s .com * @throws HttpException * @throws IOException */ public void testPutSource() throws HttpException, IOException { HttpClient client = new HttpClient(); PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/source"); putMethod.setRequestEntity(new StringRequestEntity( "<?xml version=\"1.0\" encoding=\"UTF-8\"?><message><user>user1</user><password>user1pwd</password></message>", "application/xml", "UTF-8")); try { client.executeMethod(putMethod); assertEquals(204, putMethod.getStatusCode()); } finally { putMethod.releaseConnection(); } GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/source"); try { client.executeMethod(getMethod); String str = getMethod.getResponseBodyAsString(); assertEquals(str, 200, getMethod.getStatusCode()); assertTrue(str, str.contains("<message><user>user1</user><password>user1pwd</password></message>")); String contentType = (getMethod.getResponseHeader("Content-Type") == null) ? null : getMethod.getResponseHeader("Content-Type").getValue(); assertNotNull(contentType, contentType); Header contentLengthHeader = getMethod.getResponseHeader("Content-Length"); assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(), contentLengthHeader); } finally { getMethod.releaseConnection(); } }
From source file:org.apache.wink.itest.standard.JAXRSStreamingOutputTest.java
/** * Tests putting and then getting a StreamingOutput. * //from w w w. j av a2 s . c o m * @throws HttpException * @throws IOException */ public void testPutStreamngOutput() throws HttpException, IOException { HttpClient client = new HttpClient(); PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/streamingoutput"); byte[] barr = new byte[100000]; Random r = new Random(); r.nextBytes(barr); putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "bytes/array")); try { client.executeMethod(putMethod); assertEquals(204, putMethod.getStatusCode()); } finally { putMethod.releaseConnection(); } GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/streamingoutput"); try { client.executeMethod(getMethod); assertEquals(200, getMethod.getStatusCode()); InputStream is = getMethod.getResponseBodyAsStream(); byte[] receivedBArr = new byte[barr.length]; DataInputStream dis = new DataInputStream(is); dis.readFully(receivedBArr); int checkEOF = dis.read(); assertEquals(-1, checkEOF); for (int c = 0; c < barr.length; ++c) { assertEquals(barr[c], receivedBArr[c]); } String contentType = (getMethod.getResponseHeader("Content-Type") == null) ? null : getMethod.getResponseHeader("Content-Type").getValue(); assertNotNull(contentType, contentType); Header contentLengthHeader = getMethod.getResponseHeader("Content-Length"); assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(), contentLengthHeader); } finally { getMethod.releaseConnection(); } }
From source file:org.apache.wink.itest.standard.JAXRSStreamingOutputTest.java
/** * Tests receiving a StreamingOutput with a non-standard content-type. * /*from w w w . j ava 2 s. com*/ * @throws HttpException * @throws IOException */ public void testWithRequestAcceptHeaderWillReturnRequestedContentType() throws HttpException, IOException { HttpClient client = new HttpClient(); PutMethod putMethod = new PutMethod(getBaseURI() + "/providers/standard/streamingoutput"); byte[] barr = new byte[100000]; Random r = new Random(); r.nextBytes(barr); putMethod.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(barr), "any/type")); try { client.executeMethod(putMethod); assertEquals(204, putMethod.getStatusCode()); } finally { putMethod.releaseConnection(); } GetMethod getMethod = new GetMethod(getBaseURI() + "/providers/standard/streamingoutput"); getMethod.addRequestHeader("Accept", "mytype/subtype"); try { client.executeMethod(getMethod); assertEquals(200, getMethod.getStatusCode()); InputStream is = getMethod.getResponseBodyAsStream(); byte[] receivedBArr = new byte[barr.length]; DataInputStream dis = new DataInputStream(is); dis.readFully(receivedBArr); int checkEOF = dis.read(); assertEquals(-1, checkEOF); for (int c = 0; c < barr.length; ++c) { assertEquals(barr[c], receivedBArr[c]); } assertEquals("mytype/subtype", getMethod.getResponseHeader("Content-Type").getValue()); Header contentLengthHeader = getMethod.getResponseHeader("Content-Length"); assertNull(contentLengthHeader == null ? "null" : contentLengthHeader.getValue(), contentLengthHeader); } finally { getMethod.releaseConnection(); } }
From source file:org.apache.wink.itest.subresources.JAXRSExceptionsSubresourcesTest.java
/** * Test that a checked exception is processed properly. * /*w ww. j a v a 2 s . c o m*/ * @throws Exception */ public void testCheckedException() throws Exception { HttpClient client = new HttpClient(); PutMethod putMethod = new PutMethod(getBaseURI() + "/commentdata"); try { putMethod.setRequestEntity(new StringRequestEntity("<comment></comment>", "text/xml", null)); client.executeMethod(putMethod); assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), putMethod.getStatusCode()); // assertLogContainsException("jaxrs.tests.exceptions.subresources.server.GuestbookException: Unexpected ID."); } finally { putMethod.releaseConnection(); } }
From source file:org.apache.zeppelin.realm.ZeppelinHubRealm.java
/** * Send to ZeppelinHub a login request based on the request body which is a JSON that contains 2 * fields "login" and "password"./*from w ww .j a v a 2 s .c o m*/ * * @param requestBody JSON string of ZeppelinHub payload. * @return Account object with login, name (if set in ZeppelinHub), and mail. * @throws AuthenticationException if fail to login. */ protected User authenticateUser(String requestBody) { PutMethod put = new PutMethod(Joiner.on("/").join(zeppelinhubUrl, USER_LOGIN_API_ENDPOINT)); String responseBody = StringUtils.EMPTY; String userSession = StringUtils.EMPTY; try { put.setRequestEntity(new StringRequestEntity(requestBody, JSON_CONTENT_TYPE, UTF_8_ENCODING)); int statusCode = httpClient.executeMethod(put); if (statusCode != HttpStatus.SC_OK) { LOG.error("Cannot login user, HTTP status code is {} instead on 200 (OK)", statusCode); put.releaseConnection(); throw new AuthenticationException("Couldnt login to ZeppelinHub. " + "Login or password incorrect"); } responseBody = put.getResponseBodyAsString(); userSession = put.getResponseHeader(USER_SESSION_HEADER).getValue(); put.releaseConnection(); } catch (IOException e) { LOG.error("Cannot login user", e); throw new AuthenticationException(e.getMessage()); } User account = null; try { account = gson.fromJson(responseBody, User.class); } catch (JsonParseException e) { LOG.error("Cannot deserialize ZeppelinHub response to User instance", e); throw new AuthenticationException("Cannot login to ZeppelinHub"); } // Add ZeppelinHub user_session token this singleton map, this will help ZeppelinHubRepo // to get specific information about the current user. UserSessionContainer.instance.setSession(account.login, userSession); /* TODO(khalid): add proper roles and add listener */ HashSet<String> userAndRoles = new HashSet<String>(); userAndRoles.add(account.login); ZeppelinServer.notebookWsServer.broadcastReloadedNoteList( new org.apache.zeppelin.user.AuthenticationInfo(account.login), userAndRoles); return account; }
From source file:org.apache.zeppelin.rest.AbstractTestRestApi.java
protected static PutMethod httpPut(String path, String body) throws IOException { LOG.info("Connecting to {}", url + path); HttpClient httpClient = new HttpClient(); PutMethod putMethod = new PutMethod(url + path); putMethod.addRequestHeader("Origin", url); RequestEntity entity = new ByteArrayRequestEntity(body.getBytes("UTF-8")); putMethod.setRequestEntity(entity); httpClient.executeMethod(putMethod); LOG.info("{} - {}", putMethod.getStatusCode(), putMethod.getStatusText()); return putMethod; }
From source file:org.archive.wayback.resourceindex.indexer.IndexClient.java
/** * @param cdx/*from ww w . j a va 2 s.c om*/ * @return true if CDX was added to local or remote index * @throws HttpException * @throws IOException */ public boolean addCDX(File cdx) throws HttpException, IOException { boolean added = false; if (target == null) { throw new IOException("No target set"); } String base = cdx.getName(); if (target.startsWith("http://")) { String finalUrl = target; if (target.endsWith("/")) { finalUrl = target + base; } else { finalUrl = target + "/" + base; } PutMethod method = new PutMethod(finalUrl); method.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(cdx))); int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { LOGGER.info("Uploaded cdx " + cdx.getAbsolutePath() + " to " + finalUrl); if (!cdx.delete()) { throw new IOException("FAILED delete " + cdx.getAbsolutePath()); } added = true; } else { throw new IOException("Method failed: " + method.getStatusLine() + " for URL " + finalUrl + " on file " + cdx.getAbsolutePath()); } } else { // assume a local directory: File toBeMergedDir = new File(target); if (!toBeMergedDir.exists()) { toBeMergedDir.mkdirs(); } if (!toBeMergedDir.exists()) { throw new IOException("Target " + target + " does not exist"); } if (!toBeMergedDir.isDirectory()) { throw new IOException("Target " + target + " is not a dir"); } if (!toBeMergedDir.canWrite()) { throw new IOException("Target " + target + " is not writable"); } File toBeMergedFile = new File(toBeMergedDir, base); if (toBeMergedFile.exists()) { LOGGER.severe("WARNING: " + toBeMergedFile.getAbsolutePath() + "already exists!"); } else { if (cdx.renameTo(toBeMergedFile)) { LOGGER.info("Queued " + toBeMergedFile.getAbsolutePath() + " for merging."); added = true; } else { LOGGER.severe("FAILED rename(" + cdx.getAbsolutePath() + ") to (" + toBeMergedFile.getAbsolutePath() + ")"); } } } return added; }
From source file:org.archive.wayback.resourceindex.updater.IndexClient.java
/** * @param cdx/*w w w. j a va 2 s . co m*/ * @return true if CDX was added to local or remote index * @throws HttpException * @throws IOException */ public boolean addCDX(File cdx) throws HttpException, IOException { boolean added = false; if (target == null) { throw new IOException("No target set"); } String base = cdx.getName(); if (target.startsWith("http://")) { String finalUrl = target; if (target.endsWith("/")) { finalUrl = target + base; } else { finalUrl = target + "/" + base; } PutMethod method = new PutMethod(finalUrl); method.setRequestEntity(new InputStreamRequestEntity(new FileInputStream(cdx))); int statusCode = client.executeMethod(method); if (statusCode == HttpStatus.SC_OK) { LOGGER.info("Uploaded cdx " + cdx.getAbsolutePath() + " to " + finalUrl); if (!cdx.delete()) { throw new IOException("FAILED delete " + cdx.getAbsolutePath()); } added = true; } else { throw new IOException("Method failed: " + method.getStatusLine() + " for URL " + finalUrl + " on file " + cdx.getAbsolutePath()); } } else { // assume a local directory: File toBeMergedDir = new File(target); if (!toBeMergedDir.exists()) { toBeMergedDir.mkdirs(); } if (!toBeMergedDir.exists()) { throw new IOException("Target " + target + " does not exist"); } if (!toBeMergedDir.isDirectory()) { throw new IOException("Target " + target + " is not a dir"); } if (!toBeMergedDir.canWrite()) { throw new IOException("Target " + target + " is not writable"); } File toBeMergedFile = new File(toBeMergedDir, base); if (toBeMergedFile.exists()) { LOGGER.warning("WARNING: " + toBeMergedFile.getAbsolutePath() + "already exists!"); } else { if (cdx.renameTo(toBeMergedFile)) { LOGGER.info("Queued " + toBeMergedFile.getAbsolutePath() + " for merging."); added = true; } else { LOGGER.severe("FAILED rename(" + cdx.getAbsolutePath() + ") to (" + toBeMergedFile.getAbsolutePath() + ")"); } } } return added; }