try { Part[] parts = {new StringPart(schema.getSchemaName(), schema.toString())}; MultipartRequestEntity requestEntity = new MultipartRequestEntity(parts, new HttpMethodParams()); httpPost.setRequestEntity(requestEntity); int responseCode = HTTP_CLIENT.executeMethod(httpPost);
public static PostMethod sendMultipartPostRequest(String url, String body) throws IOException { HttpClient httpClient = new HttpClient(); PostMethod postMethod = new PostMethod(url); // our handlers ignore key...so we can put anything here Part[] parts = {new StringPart("body", body)}; postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams())); httpClient.executeMethod(postMethod); return postMethod; }
public static PutMethod sendMultipartPutRequest(String url, String body) throws IOException { HttpClient httpClient = new HttpClient(); PutMethod putMethod = new PutMethod(url); // our handlers ignore key...so we can put anything here Part[] parts = {new StringPart("body", body)}; putMethod.setRequestEntity(new MultipartRequestEntity(parts, putMethod.getParams())); httpClient.executeMethod(putMethod); return putMethod; } }
private RequestEntity configureMultipartFileUpload(HttpMethod m, final RestRequest request, RequestEntity requestEntity, Map<String, RestMultipart> multipartFiles) { MultipartRequestEntity multipartRequestEntity = null; // Current File Name reading for tracking missing file String fileName = null; List<Part> fileParts = new ArrayList<Part>(multipartFiles.size()); // Read File Part for (Map.Entry<String, RestMultipart> multipartFile : multipartFiles.entrySet()) { Part filePart = createMultipart(multipartFile.getKey(), multipartFile.getValue()); fileParts.add(filePart); } Part[] parts = fileParts.toArray(new Part[fileParts.size()]); multipartRequestEntity = new MultipartRequestEntity(parts, ((EntityEnclosingMethod) m).getParams()); return multipartRequestEntity; }
PostMethod getJsonUrlRequest() { PostMethod request = new PostMethod(getJsonUrl(this.packId)); List<Part> parts = new ArrayList<Part>(); for (NameValuePair part : this.stringParams.values()) { parts.add(new StringPart(part.getName(), part.getValue(), getCharset().name())); } for (Part part : this.fileParams.values()) { parts.add(part); } request.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), request.getParams())); return request; }
private PostMethod addFiles(PostMethod post) { if (files.size() > 0) { ArrayList<Part> parts = new ArrayList<Part>(); int i = 0; for(String key: files.keySet()) { VirtualFile virtualFile = files.get(key); parts.add(new StringPart("name", key)); parts.add(new StringPart("json", "{\"parameter\":{\"name\":\"" + key + "\",\"file\":\""+ String.format("file%d", i) +"\"}}")); parts.add(new FilePart(String.format("file%d", i), new VirtualFilePartSource(virtualFile))); i++; } post.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), post.getParams())); files.clear(); } return post; }
@Override public SimpleResponse getUpdateResponse() throws Exception { PostMethod request = new PostMethod(getUpdateUrl()); List<Part> parts = new ArrayList<Part>(); for (NameValuePair part : this.stringParams.values()) { parts.add(new StringPart(part.getName(), part.getValue(), getCharset().name())); } for (Part part : this.fileParams.values()) { parts.add(part); } request.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), request.getParams())); try { return executeSimpleRequest(request); } finally { request.releaseConnection(); } }
@Override public DetailedResponse getDetailedResponse(final ResponseProgressListener listener) throws Exception { PostMethod request = new PostMethod(getHtmlUrl(this.packId)); List<Part> parts = new ArrayList<Part>(); for (NameValuePair part : this.stringParams.values()) { parts.add(new StringPart(part.getName(), part.getValue(), getCharset().name())); } for (Part part : this.fileParams.values()) { parts.add(part); } request.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), request.getParams())); try { return executeDetailedRequest(request, listener); } finally { request.releaseConnection(); } }
@Override public void installLocalBundle(final InputStream jarredBundle, String sourceLocation) throws OsgiClientException { if (jarredBundle == null) { throw new IllegalArgumentException("jarredBundle may not be null"); } 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(); }
/** 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); 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)); } }
public String upload(String url, WeiXinHttpUpload upload) { PostMethod postMethod = new PostMethod(url); try { Part[] parts = createParts(upload); HttpMethodParams params = postMethod.getParams(); postMethod.setRequestEntity(new MultipartRequestEntity(parts, params)); HttpClient client = getVisitor().getHttpClient(); client.executeMethod(postMethod); return new String(postMethod.getResponseBody(), "utf-8"); } catch (Exception e) { throw new RuntimeException(e); } finally { postMethod.releaseConnection(); } }
public String executePost(String url, Map<String, String> parameters, List<UploadItem> uploadItems, IProgressMonitor monitor) throws ReviewboardException { PostMethod postRequest = new PostMethod(stripSlash(location.getUrl()) + url); configureRequestForJson(postRequest); List<Part> parts = new ArrayList<Part>(); for ( Map.Entry<String, String> paramEntry : parameters.entrySet() ) parts.add(new StringPart(paramEntry.getKey(), paramEntry.getValue())); for ( UploadItem uploadItem : uploadItems ) { String partName = uploadItem.getName(); parts.add(new FilePart(partName, new ByteArrayPartSource(uploadItem.getFileName(), uploadItem.getContent()))); } postRequest.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), postRequest.getParams())); return executeMethod(postRequest, monitor); }
@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();
public HttpMethod deployPomWithRest( final String repositoryId, final File pomFile ) throws IOException { // the method we are calling final PostMethod filePost = new PostMethod( nexusRestClient.toNexusURL( "service/local/artifact/maven/content" ).toExternalForm() ); filePost.getParams().setBooleanParameter( HttpMethodParams.USE_EXPECT_CONTINUE, true ); final Part[] parts = { new StringPart( "r", repositoryId ), new StringPart( "hasPom", "true" ), new FilePart( pomFile.getName(), pomFile ) }; filePost.setRequestEntity( new MultipartRequestEntity( parts, filePost.getParams() ) ); LOG.debug( "URL: " + filePost.getURI() ); LOG.debug( "Method: Post" ); LOG.debug( "params: " ); LOG.debug( "\tr: " + repositoryId ); LOG.debug( "\thasPom: true" ); LOG.debug( "\tpom: " + pomFile ); return nexusRestClient.executeHTTPClientMethod( filePost ); }
public static HttpMethod deployUsingPomWithRestReturnResult( String restServiceURL, String repositoryId, File fileToDeploy, File pomFile, String classifier, String extention ) throws HttpException, IOException { // the method we are calling PostMethod filePost = new PostMethod( restServiceURL ); filePost.getParams().setBooleanParameter( HttpMethodParams.USE_EXPECT_CONTINUE, true ); classifier = ( classifier == null ) ? "" : classifier; extention = ( extention == null ) ? "" : extention; Part[] parts = { new StringPart( "r", repositoryId ), new StringPart( "e", extention ), new StringPart( "c", classifier ), new StringPart( "hasPom", "true" ), new FilePart( pomFile.getName(), pomFile ), new FilePart( fileToDeploy.getName(), fileToDeploy ) }; filePost.setRequestEntity( new MultipartRequestEntity( parts, filePost.getParams() ) ); LOG.debug( "URL: " + restServiceURL ); LOG.debug( "Method: Post" ); LOG.debug( "params: " ); LOG.debug( "\tr: " + repositoryId ); LOG.debug( "\thasPom: true" ); LOG.debug( "\tpom: " + pomFile ); LOG.debug( "\tfileToDeploy: " + fileToDeploy ); return RequestFacade.executeHTTPClientMethod( new URL( restServiceURL ), filePost ); }
/** 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); } 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)); } }
void setcontent() { switch (this.methodclass) { case Put: if(this.content != null) ((PutMethod)method).setRequestEntity(this.content); break; case Post: if(multiparts != null && multiparts.length > 0) { MultipartRequestEntity mre = new MultipartRequestEntity(multiparts,method.getParams()); ((PostMethod)method).setRequestEntity(mre); } else if(this.content != null) ((PostMethod)method).setRequestEntity(this.content); break; case Head: case Get: case Options: default: break; } this.content = null; // do not reuse this.multiparts = null; }
void setcontent() { switch (this.methodclass) { case Put: if(this.content != null) ((PutMethod) method).setRequestEntity(this.content); break; case Post: if(multiparts != null && multiparts.length > 0) { MultipartRequestEntity mre = new MultipartRequestEntity(multiparts, method.getParams()); ((PostMethod) method).setRequestEntity(mre); } else if(this.content != null) ((PostMethod) method).setRequestEntity(this.content); break; case Head: case Get: case Options: default: break; } this.content = null; // do not reuse this.multiparts = null; }
public static int deployUsingGavWithRest( String restServiceURL, String repositoryId, Gav gav, File fileToDeploy ) throws HttpException, IOException { // the method we are calling PostMethod filePost = new PostMethod( restServiceURL ); filePost.getParams().setBooleanParameter( HttpMethodParams.USE_EXPECT_CONTINUE, true ); Part[] parts = { new StringPart( "r", repositoryId ), new StringPart( "g", gav.getGroupId() ), new StringPart( "a", gav.getArtifactId() ), new StringPart( "v", gav.getVersion() ), new StringPart( "p", gav.getExtension() ), new StringPart( "c", "" ), new FilePart( fileToDeploy.getName(), fileToDeploy ), }; filePost.setRequestEntity( new MultipartRequestEntity( parts, filePost.getParams() ) ); return RequestFacade.executeHTTPClientMethod( new URL( restServiceURL ), filePost ).getStatusCode(); }
public int deployUsingGavWithRest( final String restServiceURL, final String repositoryId, final Gav gav, final File fileToDeploy ) throws IOException { // the method we are calling final PostMethod filePost = new PostMethod( restServiceURL ); filePost.getParams().setBooleanParameter( HttpMethodParams.USE_EXPECT_CONTINUE, true ); final String extension = gav.getExtension() != null ? gav.getExtension() : ""; final String classifier = gav.getClassifier() != null ? gav.getClassifier() : ""; final Part[] parts = { new StringPart( "r", repositoryId ), new StringPart( "g", gav.getGroupId() ), new StringPart( "a", gav.getArtifactId() ), new StringPart( "v", gav.getVersion() ), new StringPart( "p", extension ), new StringPart( "c", classifier ), new FilePart( fileToDeploy.getName(), fileToDeploy ) }; filePost.setRequestEntity( new MultipartRequestEntity( parts, filePost.getParams() ) ); return nexusRestClient.executeHTTPClientMethod( filePost ).getStatusCode(); }