use of org.apache.commons.httpclient.methods.PutMethod in project xwiki-platform by xwiki.
the class InstallMojo method executePutXml.
private PutMethod executePutXml(String uri, Object object, Marshaller marshaller, HttpClient httpClient) throws Exception {
PutMethod putMethod = new PutMethod(uri);
putMethod.addRequestHeader("Accept", MediaType.APPLICATION_XML.toString());
StringWriter writer = new StringWriter();
marshaller.marshal(object, writer);
RequestEntity entity = new StringRequestEntity(writer.toString(), MediaType.APPLICATION_XML.toString(), "UTF-8");
putMethod.setRequestEntity(entity);
httpClient.executeMethod(putMethod);
return putMethod;
}
use of org.apache.commons.httpclient.methods.PutMethod in project xwiki-platform by xwiki.
the class InstallMojo method installExtensions.
private void installExtensions(Marshaller marshaller, Unmarshaller unmarshaller, HttpClient httpClient) throws Exception {
InstallRequest installRequest = new InstallRequest();
// Set a job id to save the job result
installRequest.setId("extension", "provision", UUID.randomUUID().toString());
installRequest.setInteractive(false);
// Set the extension list to install
for (ExtensionId extensionId : this.extensionIds) {
org.xwiki.extension.ExtensionId extId = new org.xwiki.extension.ExtensionId(extensionId.getId(), extensionId.getVersion());
getLog().info(String.format("Installing extension [%s]...", extId));
installRequest.addExtension(extId);
}
// Set the namespaces into which to install the extensions
if (this.namespaces == null || this.namespaces.isEmpty()) {
installRequest.addNamespace("wiki:xwiki");
} else {
for (String namespace : this.namespaces) {
installRequest.addNamespace(namespace);
}
}
// Set any user for installing pages (if defined)
if (this.installUserReference != null) {
installRequest.setProperty("user.reference", new DocumentReference("xwiki", "XWiki", "superadmin"));
}
JobRequest request = getModelFactory().toRestJobRequest(installRequest);
String uri = String.format("%s/jobs?jobType=install&async=false", this.xwikiRESTURL);
PutMethod putMethod = executePutXml(uri, request, marshaller, httpClient);
// Verify results
// Verify that we got a 200 response
int statusCode = putMethod.getStatusCode();
if (statusCode < 200 || statusCode >= 300) {
throw new MojoExecutionException(String.format("Job execution failed. Response status code [%s], reason [%s]", statusCode, putMethod.getResponseBodyAsString()));
}
// Get the job status
JobStatus jobStatus = (JobStatus) unmarshaller.unmarshal(putMethod.getResponseBodyAsStream());
if (jobStatus.getErrorMessage() != null && jobStatus.getErrorMessage().length() > 0) {
throw new MojoExecutionException(String.format("Job execution failed. Reason [%s]", jobStatus.getErrorMessage()));
}
// Release connection
putMethod.releaseConnection();
}
use of org.apache.commons.httpclient.methods.PutMethod in project fabric8 by jboss-fuse.
the class CrmSecureTest method putCustomerTest.
/**
* HTTP PUT http://localhost:8181/cxf/crm/customerservice/customers is used to upload the contents of
* the update_customer.xml file to update the customer information for customer 123.
* <p/>
* On the server side, it matches the CustomerService's updateCustomer() method
*
* @throws Exception
*/
@Test
public void putCustomerTest() throws IOException {
LOG.info("============================================");
LOG.info("Sent HTTP PUT request to update customer info");
String inputFile = this.getClass().getResource("/update_customer.xml").getFile();
File input = new File(inputFile);
PutMethod put = new PutMethod(CUSTOMER_SERVICE_URL);
put.getHostAuthState().setAuthScheme(scheme);
RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1");
put.setRequestEntity(entity);
int result = 0;
try {
result = httpClient.executeMethod(put);
LOG.info("Response status code: " + result);
LOG.info("Response body: ");
LOG.info(put.getResponseBodyAsString());
} catch (IOException e) {
LOG.error("Error connecting to {}", CUSTOMER_SERVICE_URL);
LOG.error("You should build the 'rest' quick start and deploy it to a local Fabric8 before running this test");
LOG.error("Please read the README.md file in 'rest' quick start root");
fail("Connection error");
} finally {
// Release current connection to the connection pool once you are
// done
put.releaseConnection();
}
assertEquals(result, 200);
}
use of org.apache.commons.httpclient.methods.PutMethod in project jaggery by wso2.
the class XMLHttpRequestHostObject method send.
private void send(Context cx, Object obj) throws ScriptException {
final HttpMethodBase method;
if ("GET".equalsIgnoreCase(methodName)) {
method = new GetMethod(this.url);
} else if ("HEAD".equalsIgnoreCase(methodName)) {
method = new HeadMethod(this.url);
} else if ("POST".equalsIgnoreCase(methodName)) {
PostMethod post = new PostMethod(this.url);
if (obj instanceof FormDataHostObject) {
FormDataHostObject fd = ((FormDataHostObject) obj);
List<Part> parts = new ArrayList<Part>();
for (Map.Entry<String, String> entry : fd) {
parts.add(new StringPart(entry.getKey(), entry.getValue()));
}
post.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), post.getParams()));
} else {
String content = getRequestContent(obj);
if (content != null) {
post.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(content.getBytes())));
}
}
method = post;
} else if ("PUT".equalsIgnoreCase(methodName)) {
PutMethod put = new PutMethod(this.url);
String content = getRequestContent(obj);
if (content != null) {
put.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(content.getBytes())));
}
method = put;
} else if ("DELETE".equalsIgnoreCase(methodName)) {
method = new DeleteMethod(this.url);
} else if ("TRACE".equalsIgnoreCase(methodName)) {
method = new TraceMethod(this.url);
} else if ("OPTIONS".equalsIgnoreCase(methodName)) {
method = new OptionsMethod(this.url);
} else {
throw new ScriptException("Unknown HTTP method : " + methodName);
}
for (Header header : requestHeaders) {
method.addRequestHeader(header);
}
if (username != null) {
httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
}
this.method = method;
final XMLHttpRequestHostObject xhr = this;
if (async) {
updateReadyState(cx, xhr, LOADING);
final ContextFactory factory = cx.getFactory();
final ExecutorService es = Executors.newSingleThreadExecutor();
es.submit(new Callable() {
public Object call() throws Exception {
Context ctx = RhinoEngine.enterContext(factory);
try {
executeRequest(ctx, xhr);
} catch (ScriptException e) {
log.error(e.getMessage(), e);
} finally {
es.shutdown();
RhinoEngine.exitContext();
}
return null;
}
});
} else {
executeRequest(cx, xhr);
}
}
use of org.apache.commons.httpclient.methods.PutMethod in project application by collectionspace.
the class ServicesConnection method createMethod.
private HttpMethod createMethod(RequestMethod method, String uri, InputStream data) throws ConnectionException {
uri = prepend_base(uri);
if (uri == null)
throw new ConnectionException("URI must not be null");
// Extract QP's
int qp_start = uri.indexOf('?');
String qps = null;
if (qp_start != -1) {
qps = uri.substring(qp_start + 1);
uri = uri.substring(0, qp_start);
}
HttpMethod out = null;
switch(method) {
case POST:
{
out = new PostMethod(uri);
if (data != null)
((PostMethod) out).setRequestEntity(new InputStreamRequestEntity(data));
break;
}
case PUT:
{
out = new PutMethod(uri);
if (data != null)
((PutMethod) out).setRequestEntity(new InputStreamRequestEntity(data));
break;
}
case GET:
out = new GetMethod(uri);
break;
case DELETE:
out = new DeleteMethod(uri);
break;
default:
throw new ConnectionException("Unsupported method " + method, 0, uri);
}
if (qps != null)
out.setQueryString(qps);
out.setDoAuthentication(true);
return out;
}
Aggregations