List of usage examples for org.apache.commons.httpclient.methods PutMethod setRequestEntity
public void setRequestEntity(RequestEntity paramRequestEntity)
From source file:org.olat.restapi.UserMgmtTest.java
@Test public void testCreateUser() throws IOException { final HttpClient c = loginWithCookie("administrator", "olat"); final UserVO vo = new UserVO(); final String username = UUID.randomUUID().toString(); vo.setLogin(username);//www . j av a 2s . co m vo.setFirstName("John"); vo.setLastName("Smith"); vo.setEmail(username + "@frentix.com"); vo.putProperty("telOffice", "39847592"); vo.putProperty("telPrivate", "39847592"); vo.putProperty("telMobile", "39847592"); vo.putProperty("gender", "Female");// male or female vo.putProperty("birthDay", "12/12/2009"); final String stringuifiedAuth = stringuified(vo); final PutMethod method = createPut("/users", MediaType.APPLICATION_JSON, true); final RequestEntity entity = new StringRequestEntity(stringuifiedAuth, MediaType.APPLICATION_JSON, "UTF-8"); method.setRequestEntity(entity); method.addRequestHeader("Accept-Language", "en"); final int code = c.executeMethod(method); assertTrue(code == 200 || code == 201); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final UserVO savedVo = parse(body, UserVO.class); final Identity savedIdent = BaseSecurityManager.getInstance().findIdentityByName(username); assertNotNull(savedVo); assertNotNull(savedIdent); assertEquals(savedVo.getKey(), savedIdent.getKey()); assertEquals(savedVo.getLogin(), savedIdent.getName()); assertEquals("Female", savedIdent.getUser().getProperty("gender", Locale.ENGLISH)); assertEquals("39847592", savedIdent.getUser().getProperty("telPrivate", Locale.ENGLISH)); assertEquals("12/12/09", savedIdent.getUser().getProperty("birthDay", Locale.ENGLISH)); }
From source file:org.olat.restapi.UserMgmtTest.java
/** * Test machine format for gender and date *//*from ww w . j a v a 2 s . co m*/ @Test public void testCreateUser2() throws IOException { final HttpClient c = loginWithCookie("administrator", "olat"); final UserVO vo = new UserVO(); final String username = UUID.randomUUID().toString(); vo.setLogin(username); vo.setFirstName("John"); vo.setLastName("Smith"); vo.setEmail(username + "@frentix.com"); vo.putProperty("telOffice", "39847592"); vo.putProperty("telPrivate", "39847592"); vo.putProperty("telMobile", "39847592"); vo.putProperty("gender", "female");// male or female vo.putProperty("birthDay", "20091212"); final String stringuifiedAuth = stringuified(vo); final PutMethod method = createPut("/users", MediaType.APPLICATION_JSON, true); final RequestEntity entity = new StringRequestEntity(stringuifiedAuth, MediaType.APPLICATION_JSON, "UTF-8"); method.setRequestEntity(entity); method.addRequestHeader("Accept-Language", "en"); final int code = c.executeMethod(method); assertTrue(code == 200 || code == 201); final String body = method.getResponseBodyAsString(); method.releaseConnection(); final UserVO savedVo = parse(body, UserVO.class); final Identity savedIdent = BaseSecurityManager.getInstance().findIdentityByName(username); assertNotNull(savedVo); assertNotNull(savedIdent); assertEquals(savedVo.getKey(), savedIdent.getKey()); assertEquals(savedVo.getLogin(), savedIdent.getName()); assertEquals("Female", savedIdent.getUser().getProperty("gender", Locale.ENGLISH)); assertEquals("39847592", savedIdent.getUser().getProperty("telPrivate", Locale.ENGLISH)); assertEquals("12/12/09", savedIdent.getUser().getProperty("birthDay", Locale.ENGLISH)); }
From source file:org.olat.restapi.UserMgmtTest.java
@Test public void testCreateUserWithValidationError() throws IOException { final HttpClient c = loginWithCookie("administrator", "olat"); final UserVO vo = new UserVO(); vo.setLogin("rest-809"); vo.setFirstName("John"); vo.setLastName("Smith"); vo.setEmail(""); vo.putProperty("gender", "lu"); final String stringuifiedAuth = stringuified(vo); final PutMethod method = createPut("/users", MediaType.APPLICATION_JSON, true); final RequestEntity entity = new StringRequestEntity(stringuifiedAuth, MediaType.APPLICATION_JSON, "UTF-8"); method.setRequestEntity(entity); final int code = c.executeMethod(method); assertTrue(code == 406);//w w w. j a v a 2 s . c om final String body = method.getResponseBodyAsString(); method.releaseConnection(); final List<ErrorVO> errors = parseErrorArray(body); assertNotNull(errors); assertFalse(errors.isEmpty()); assertTrue(errors.size() >= 2); assertNotNull(errors.get(0).getCode()); assertNotNull(errors.get(0).getTranslation()); assertNotNull(errors.get(1).getCode()); assertNotNull(errors.get(1).getTranslation()); final Identity savedIdent = BaseSecurityManager.getInstance().findIdentityByName("rest-809"); assertNull(savedIdent); }
From source file:org.ozsoft.xmldb.exist.ExistConnector.java
@Override public void storeResource(String uri, InputStream is) throws XmldbException { // Use the REST interface for storing resources. String actualUri = String.format("%s/rest%s", existUri, uri); PutMethod putMethod = new PutMethod(actualUri); putMethod.setRequestEntity(new InputStreamRequestEntity(is)); try {/*from w w w.j av a 2 s . c om*/ int statusCode = httpClient.executeMethod(putMethod); if (statusCode >= STATUS_ERROR) { if (statusCode == NOT_FOUND) { throw new NotFoundException(uri); } else if (statusCode == AUTHORIZATION_REQUIRED || statusCode == NOT_AUTHORIZED) { throw new NotAuthorizedException(String.format("Not authorized to store resource '%s'", uri)); } else { throw new XmldbException( String.format("Could not store resource '%s' (HTTP status code: %d)", uri, statusCode)); } } } catch (IOException e) { String msg = String.format("Could not store resource '%s': %s", uri, e.getMessage()); LOG.error(msg, e); throw new XmldbException(msg, e); } }
From source file:org.review_board.ereviewboard.core.client.ReviewboardHttpClient.java
public String executePut(String url, Map<String, String> parameters, IProgressMonitor monitor) throws ReviewboardException { PutMethod putMethod = new PutMethod(stripSlash(location.getUrl()) + url); configureRequestForJson(putMethod);//w w w. ja v a 2s . com List<NameValuePair> pairs = new ArrayList<NameValuePair>(); for (Map.Entry<String, String> entry : parameters.entrySet()) pairs.add(new NameValuePair(entry.getKey(), entry.getValue())); putMethod.setQueryString(pairs.toArray(new NameValuePair[0])); String queryString = putMethod.getQueryString(); putMethod.setQueryString(""); try { putMethod.setRequestEntity(new StringRequestEntity(queryString, null, "UTF-8")); } catch (UnsupportedEncodingException e) { throw new ReviewboardException(e.getMessage(), e); } return executeMethod(putMethod, monitor); }
From source file:org.roosster.api.TestApi.java
/** * //from w w w.jav a2 s .c o m */ public void testApi() throws Exception { // FIRST ADD URL System.out.println("Trying to POST '" + TEST_URL + "' as a new entry to " + API_ENDPOINT); PostMethod post = new PostMethod(API_ENDPOINT + "?force=true"); post.setRequestEntity(postRequestBody); int statusCode = client.executeMethod(post); assertEquals("POST to " + TEST_URL + " failed", 200, statusCode); logMethodResponse(post); // ... THEN GET IT System.out.println("Trying to GET '" + TEST_URL + "' as a new entry to " + API_ENDPOINT); GetMethod get = new GetMethod(API_ENDPOINT + "/entry?url=" + TEST_URL); statusCode = client.executeMethod(get); assertEquals("GET from " + TEST_URL + " failed", 200, statusCode); logMethodResponse(get); // ... THEN UPDATE IT System.out.println("Trying to PUT '" + TEST_URL + "' as a new entry to " + API_ENDPOINT); PutMethod put = new PutMethod(API_ENDPOINT); put.setRequestEntity(putRequestBody); statusCode = client.executeMethod(put); assertEquals("PUT to " + TEST_URL + " failed", 200, statusCode); logMethodResponse(put); // ... THEN GET IT AGAIN System.out.println("Trying to GET '" + TEST_URL + "' as a new entry to " + API_ENDPOINT); get = new GetMethod(API_ENDPOINT + "/entry?url=" + TEST_URL); statusCode = client.executeMethod(get); assertEquals("GET from " + TEST_URL + " failed", 200, statusCode); logMethodResponse(get); // ... AND DELETE IT AGAIN System.out.println("Trying to DELETE '" + TEST_URL + "' as a new entry to " + API_ENDPOINT); DeleteMethod del = new DeleteMethod(API_ENDPOINT + "?url=" + TEST_URL); statusCode = client.executeMethod(del); assertEquals("GET from " + TEST_URL + " failed", 200, statusCode); logMethodResponse(get); // ... THEN GET IT AGAIN System.out.println("Trying to GET '" + TEST_URL + "' as a new entry to " + API_ENDPOINT); get = new GetMethod(API_ENDPOINT + "/entry?url=" + TEST_URL); statusCode = client.executeMethod(get); assertEquals("GET from " + TEST_URL + " failed", 200, statusCode); logMethodResponse(get); }
From source file:org.sonatype.nexus.integrationtests.nexus4548.Nexus4548RepoTargetPermissionMatchesPathInRepoIT.java
private HttpMethod put(final String gavPath, final int code) throws Exception { PutMethod putMethod = new PutMethod(getNexusTestRepoUrl() + gavPath); putMethod.setRequestEntity(new FileRequestEntity(getTestFile("pom-a.pom"), "text/xml")); final HttpMethod httpMethod = RequestFacade.executeHTTPClientMethod(putMethod); assertThat(httpMethod.getStatusCode(), Matchers.is(code)); return httpMethod; }
From source file:org.sonatype.nexus.integrationtests.nxcm970.ContinuousDeployer.java
public void run() { PutMethod method = new PutMethod(targetUrl); method.setRequestEntity(new InputStreamRequestEntity(new EndlessBlockingInputStream(this))); try {//from ww w .ja v a 2 s.c o m result = httpClient.executeMethod(method); } catch (Exception e) { result = -2; e.printStackTrace(); } }
From source file:org.sonatype.nexus.proxy.storage.remote.commonshttpclient.CommonsHttpClientRemoteStorage.java
@Override public void storeItem(ProxyRepository repository, StorageItem item) throws UnsupportedStorageOperationException, RemoteStorageException { if (!(item instanceof StorageFileItem)) { throw new UnsupportedStorageOperationException("Storing of non-files remotely is not supported!"); }//from w ww . j av a 2s . co m StorageFileItem fItem = (StorageFileItem) item; ResourceStoreRequest request = new ResourceStoreRequest(item); URL remoteURL = getAbsoluteUrlFromBase(repository, request); PutMethod method = new PutMethod(remoteURL.toString()); try { method.setRequestEntity( new InputStreamRequestEntity(fItem.getInputStream(), fItem.getLength(), fItem.getMimeType())); int response = executeMethod(repository, request, method, remoteURL); if (response != HttpStatus.SC_OK && response != HttpStatus.SC_CREATED && response != HttpStatus.SC_NO_CONTENT && response != HttpStatus.SC_ACCEPTED) { throw new RemoteStorageException("Unexpected response code while executing " + method.getName() + " method [repositoryId=\"" + repository.getId() + "\", requestPath=\"" + request.getRequestPath() + "\", remoteUrl=\"" + remoteURL.toString() + "\"]. Expected: \"any success (2xx)\". Received: " + response + " : " + HttpStatus.getStatusText(response)); } } catch (IOException e) { throw new RemoteStorageException( e.getMessage() + " [repositoryId=\"" + repository.getId() + "\", requestPath=\"" + request.getRequestPath() + "\", remoteUrl=\"" + remoteURL.toString() + "\"]", e); } finally { method.releaseConnection(); } }
From source file:org.sonatype.nexus.restlight.common.AbstractRESTLightClient.java
protected Document doPut(final String path, final Map<String, ? extends Object> requestParams, final Document body, final boolean expectResponseBody) throws RESTLightClientException { LogManager.getLogger(getClass().getName()).debug("Putting to: '" + path + "'"); PutMethod method = new PutMethod(baseUrl + path); method.addRequestHeader("Content-Type", "application/xml"); if (body != null && body.getRootElement() != null) { try {//from w ww . j a v a 2 s . c o m method.setRequestEntity(new StringRequestEntity( new XMLOutputter(Format.getCompactFormat()).outputString(body), "text/plain", "UTF-8")); } catch (UnsupportedEncodingException e) { throw new RESTLightClientException("Failed to construct POST request. Reason: " + e.getMessage(), e); } } addRequestParams(method, requestParams); try { client.executeMethod(method); } catch (HttpException e) { throw new RESTLightClientException("PUT request execution failed. Reason: " + e.getMessage(), e); } catch (IOException e) { throw new RESTLightClientException("PUT request execution failed. Reason: " + e.getMessage(), e); } int status = method.getStatusCode(); String statusText = method.getStatusText(); if (status < 200 || status > 299) { String errorBody = ""; try { errorBody = method.getResponseBodyAsString(); } catch (IOException ioe) { } throw new RESTLightClientException( "PUT request failed; HTTP status: " + status + ", " + statusText + "\nHTTP body: " + errorBody); } if (expectResponseBody) { try { return new SAXBuilder().build(method.getResponseBodyAsStream()); } catch (JDOMException e) { throw new RESTLightClientException("Failed to parse response body as XML for PUT request.", e); } catch (IOException e) { throw new RESTLightClientException("Could not retrieve body as a String from PUT request.", e); } finally { method.releaseConnection(); } } else { method.releaseConnection(); return null; } }