List of usage examples for org.apache.commons.httpclient.methods.multipart MultipartRequestEntity MultipartRequestEntity
public MultipartRequestEntity(Part[] paramArrayOfPart, HttpMethodParams paramHttpMethodParams)
From source file:org.apache.sling.commons.testing.integration.SlingIntegrationTestClient.java
/** Upload to an file node structure, see SLING-168 */ public void uploadToFileNode(String url, File localFile, String fieldName, String typeHint) throws IOException { final Part[] parts = new Part[typeHint == null ? 1 : 2]; parts[0] = new FilePart(fieldName, localFile); if (typeHint != null) { parts[1] = new StringPart(fieldName + "@TypeHint", typeHint); }// www. j a va 2s . c o m final PostMethod post = new PostMethod(url); post.setFollowRedirects(false); post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams())); final int status = httpClient.executeMethod(post); final int expected = 200; if (status != expected) { throw new HttpStatusCodeException(expected, status, "POST", HttpTestBase.getResponseBodyAsStream(post, 0)); } }
From source file:org.apache.sling.commons.testing.integration.SlingIntegrationTestClient.java
/** Upload multiple files to file node structures */ public void uploadToFileNodes(String url, File[] localFiles, String[] fieldNames, String[] typeHints) throws IOException { List<Part> partsList = new ArrayList<Part>(); for (int i = 0; i < localFiles.length; i++) { Part filePart = new FilePart(fieldNames[i], localFiles[i]); partsList.add(filePart);/*w w w .j a va2 s . c o m*/ if (typeHints != null) { Part typeHintPart = new StringPart(fieldNames[i] + "@TypeHint", typeHints[i]); partsList.add(typeHintPart); } } final Part[] parts = partsList.toArray(new Part[partsList.size()]); final PostMethod post = new PostMethod(url); post.setFollowRedirects(false); post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams())); final int expected = 200; final int status = httpClient.executeMethod(post); if (status != expected) { throw new HttpStatusCodeException(expected, status, "POST", HttpTestBase.getResponseBodyAsStream(post, 0)); } }
From source file:org.apache.sling.ide.impl.resource.transport.DeleteNodeCommand.java
@Override public Result<Void> execute() { PostMethod post = new PostMethod(getPath()); try {/*w ww. j ava 2 s. c o m*/ Part[] parts = { new StringPart(":operation", "delete") }; post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams())); httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword())); httpClient.getParams().setAuthenticationPreemptive(true); int responseStatus = httpClient.executeMethod(post); return resultForResponseStatus(responseStatus); } catch (Exception e) { return AbstractResult.failure(new RepositoryException(e)); } finally { post.releaseConnection(); } }
From source file:org.apache.sling.ide.impl.resource.transport.UpdateContentCommand.java
@Override public Result<Void> execute() { PostMethod post = new PostMethod(getPath()); try {//from ww w .j a v a2s . co m List<Part> parts = new ArrayList<>(); for (Map.Entry<String, Object> property : properties.entrySet()) { if (ProtectedNodes.exists(property.getKey())) { continue; } Object propValue = property.getValue(); if (propValue instanceof String) { parts.add(new StringPart(property.getKey(), (String) propValue)); } else if (property != null) { // TODO handle multi-valued properties System.err.println("Unable to handle property " + property.getKey() + " of type " + property.getValue().getClass()); } } File f = new File(fileInfo.getLocation()); if (f.isFile()) { parts.add(new FilePart(fileInfo.getName(), f)); } post.setRequestEntity( new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), post.getParams())); httpClient.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(repositoryInfo.getUsername(), repositoryInfo.getPassword())); httpClient.getParams().setAuthenticationPreemptive(true); int responseStatus = httpClient.executeMethod(post); return resultForResponseStatus(responseStatus); } catch (Exception e) { return AbstractResult.failure(new RepositoryException(e)); } finally { post.releaseConnection(); } }
From source file:org.apache.sling.ide.osgi.impl.HttpOsgiClient.java
@Override public void installBundle(InputStream in, String fileName) throws OsgiClientException { if (in == null) { throw new IllegalArgumentException("in may not be null"); }// www .jav a 2 s. c o m if (fileName == null) { throw new IllegalArgumentException("fileName may not be null"); } // append pseudo path after root URL to not get redirected for nothing final PostMethod filePost = new PostMethod(repositoryInfo.appendPath("system/console/install")); try { // set referrer filePost.setRequestHeader("referer", "about:blank"); List<Part> partList = new ArrayList<>(); partList.add(new StringPart("action", "install")); partList.add(new StringPart("_noredir_", "_noredir_")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(in, baos); PartSource partSource = new ByteArrayPartSource(fileName, baos.toByteArray()); partList.add(new FilePart("bundlefile", partSource)); partList.add(new StringPart("bundlestart", "start")); Part[] parts = partList.toArray(new Part[partList.size()]); filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams())); int status = getHttpClient().executeMethod(filePost); if (status != 200) { throw new OsgiClientException("Method execution returned status " + status); } } catch (IOException e) { throw new OsgiClientException(e); } finally { filePost.releaseConnection(); } }
From source file:org.apache.sling.ide.osgi.impl.HttpOsgiClient.java
@Override public void installLocalBundle(final InputStream jarredBundle, String sourceLocation) throws OsgiClientException { if (jarredBundle == null) { throw new IllegalArgumentException("jarredBundle may not be null"); }//from w w w . j a v a 2 s. c o m new LocalBundleInstaller(getHttpClient(), repositoryInfo) { @Override void configureRequest(PostMethod method) throws IOException { Part[] parts = new Part[] { new FilePart("bundle", new ByteArrayPartSource("bundle.jar", IOUtils.toByteArray(jarredBundle))) }; method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams())); } }.installBundle(); }
From source file:org.apache.sling.maven.bundlesupport.AbstractBundleDeployMojo.java
private void post(String targetURL, File file) throws MojoExecutionException { PostMethod filePost = new PostMethod(targetURL); try {/*from w ww .j a v a 2 s .com*/ Part[] parts = { new FilePart(file.getName(), new FilePartSource(file.getName(), file)), new StringPart("_noredir_", "_noredir_") }; filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams())); HttpClient client = new HttpClient(); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); int status = client.executeMethod(filePost); if (status == HttpStatus.SC_OK) { getLog().info("Bundle deployed"); } else { String msg = "Deployment failed, cause: " + HttpStatus.getStatusText(status); if (failOnError) { throw new MojoExecutionException(msg); } else { getLog().error(msg); } } } catch (Exception ex) { throw new MojoExecutionException("Deployment on " + targetURL + " failed, cause: " + ex.getMessage(), ex); } finally { filePost.releaseConnection(); } }
From source file:org.apache.sling.maven.bundlesupport.AbstractBundleInstallMojo.java
/** * Install the bundle via POST to the Felix Web Console * @param targetURL the URL to the Felix Web Console Bundles listing * @param file the file to POST/* www . j av a 2s . co m*/ * @throws MojoExecutionException * @see <a href="http://felix.apache.org/documentation/subprojects/apache-felix-web-console/web-console-restful-api.html#post-requests">Webconsole RESTful API</a> * @see <a href="https://github.com/apache/felix/blob/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/core/BundlesServlet.java">BundlesServlet@Github</a> */ protected void postToFelix(String targetURL, File file) throws MojoExecutionException { // append pseudo path after root URL to not get redirected for nothing final PostMethod filePost = new PostMethod(targetURL + "/install"); try { // set referrer filePost.setRequestHeader("referer", "about:blank"); List<Part> partList = new ArrayList<Part>(); partList.add(new StringPart("action", "install")); partList.add(new StringPart("_noredir_", "_noredir_")); partList.add(new FilePart("bundlefile", new FilePartSource(file.getName(), file))); partList.add(new StringPart("bundlestartlevel", bundleStartLevel)); if (bundleStart) { partList.add(new StringPart("bundlestart", "start")); } if (refreshPackages) { partList.add(new StringPart("refreshPackages", "true")); } Part[] parts = partList.toArray(new Part[partList.size()]); filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams())); int status = getHttpClient().executeMethod(filePost); if (status == HttpStatus.SC_OK) { getLog().info("Bundle installed"); } else { String msg = "Installation failed, cause: " + HttpStatus.getStatusText(status); if (failOnError) { throw new MojoExecutionException(msg); } else { getLog().error(msg); } } } catch (Exception ex) { throw new MojoExecutionException("Installation on " + targetURL + " failed, cause: " + ex.getMessage(), ex); } finally { filePost.releaseConnection(); } }
From source file:org.apache.sling.maven.bundlesupport.AbstractBundleInstallMojo.java
/** * Perform the operation via POST to SlingPostServlet * @param targetURL the URL of the Sling instance to post the file to. * @param file the file being interacted with the POST to Sling. * @throws MojoExecutionException// w w w . ja v a2 s . c o m */ protected void postToSling(String targetURL, File file) throws MojoExecutionException { /* truncate off trailing slash as this has special behaviorisms in * the SlingPostServlet around created node name conventions */ if (targetURL.endsWith("/")) { targetURL = targetURL.substring(0, targetURL.length() - 1); } // append pseudo path after root URL to not get redirected for nothing final PostMethod filePost = new PostMethod(targetURL); try { Part[] parts = new Part[2]; // Add content type to force the configured mimeType value parts[0] = new FilePart("*", new FilePartSource(file.getName(), file), mimeType, null); // Add TypeHint to have jar be uploaded as file (not as resource) parts[1] = new StringPart("*@TypeHint", "nt:file"); /* Request JSON response from Sling instead of standard HTML, to * reduce the payload size (if the PostServlet supports it). */ filePost.setRequestHeader("Accept", JSON_MIME_TYPE); filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams())); int status = getHttpClient().executeMethod(filePost); // SlingPostServlet may return 200 or 201 on creation, accept both if (status == HttpStatus.SC_OK || status == HttpStatus.SC_CREATED) { getLog().info("Bundle installed"); } else { String msg = "Installation failed, cause: " + HttpStatus.getStatusText(status); if (failOnError) { throw new MojoExecutionException(msg); } else { getLog().error(msg); } } } catch (Exception ex) { throw new MojoExecutionException("Installation on " + targetURL + " failed, cause: " + ex.getMessage(), ex); } finally { filePost.releaseConnection(); } }
From source file:org.apache.sling.maven.bundlesupport.BundleUninstallMojo.java
@Override protected void postToSling(String targetURL, File file) throws MojoExecutionException { final PostMethod post = new PostMethod(getURLWithFilename(targetURL, file.getName())); try {// w w w . j a v a2s .c om // Add SlingPostServlet operation flag for deleting the content Part[] parts = new Part[1]; parts[0] = new StringPart(":operation", "delete"); post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams())); // Request JSON response from Sling instead of standard HTML post.setRequestHeader("Accept", JSON_MIME_TYPE); int status = getHttpClient().executeMethod(post); if (status == HttpStatus.SC_OK) { getLog().info("Bundle uninstalled"); } else { getLog().error("Uninstall failed, cause: " + HttpStatus.getStatusText(status)); } } catch (Exception ex) { throw new MojoExecutionException("Uninstall from " + targetURL + " failed, cause: " + ex.getMessage(), ex); } finally { post.releaseConnection(); } }