Java Code Examples for com.sun.jersey.api.client.WebResource.Builder#post()

The following examples show how to use com.sun.jersey.api.client.WebResource.Builder#post() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestRequestSender.java    From osiris with Apache License 2.0 9 votes vote down vote up
public ClientResponse<File> upload(String url, File f, Headers... headers) {
	@SuppressWarnings("resource")
	FormDataMultiPart form = new FormDataMultiPart();
	form.bodyPart(new FileDataBodyPart("file", f, MediaType.APPLICATION_OCTET_STREAM_TYPE));

	String urlCreated = createURI(url);
	ClientConfig cc = new DefaultClientConfig();
	cc.getClasses().add(MultiPartWriter.class);
	WebResource webResource = Client.create(cc).resource(urlCreated);
	Builder builder = webResource.type(MULTIPART_MEDIA).accept(MEDIA).accept("text/plain");
	for (Headers h : headers) {
		builder.header(h.getKey(), h.getValue());
	}

	com.sun.jersey.api.client.ClientResponse clienteResponse = null;

	clienteResponse = builder.post(com.sun.jersey.api.client.ClientResponse.class, form);

	return new ClientResponse<File>(clienteResponse, File.class);
}
 
Example 2
Source File: RestRequestSender.java    From osiris with Apache License 2.0 6 votes vote down vote up
public <T> ClientResponse<T> invoke(RestMethod method, String url, Object requestEntity, Class<T> expectedResponse,
		BasicAuth basicAuth, Headers... headers) {
	Builder builder = createBuilder(url, headers, basicAuth);

	com.sun.jersey.api.client.ClientResponse clienteResponse = null;
	switch (method) {
	case POST:
		clienteResponse = builder.post(com.sun.jersey.api.client.ClientResponse.class, requestEntity);
		break;
	case PUT:
		clienteResponse = builder.put(com.sun.jersey.api.client.ClientResponse.class, requestEntity);
		break;
	case DELETE:
		clienteResponse = builder.delete(com.sun.jersey.api.client.ClientResponse.class, requestEntity);
		break;
	default:
		throw new UnsupportedOperationException();
	}

	return new ClientResponse<T>(clienteResponse, expectedResponse);
}
 
Example 3
Source File: RestRequestSender.java    From osiris with Apache License 2.0 6 votes vote down vote up
public <T> ClientResponse<T> invoke(RestMethod method, String url, Object requestEntity,
		GenericType<T> genericType, BasicAuth basicAuth, Headers... headers) {

	Builder builder = createBuilder(url, headers, basicAuth);

	com.sun.jersey.api.client.ClientResponse clienteResponse = null;
	switch (method) {
	case POST:
		clienteResponse = builder.post(com.sun.jersey.api.client.ClientResponse.class, requestEntity);
		break;
	case PUT:
		clienteResponse = builder.put(com.sun.jersey.api.client.ClientResponse.class, requestEntity);
		break;
	case DELETE:
		clienteResponse = builder.delete(com.sun.jersey.api.client.ClientResponse.class, requestEntity);
		break;
	default:
		throw new UnsupportedOperationException();
	}

	return new ClientResponse<T>(clienteResponse, genericType);
}
 
Example 4
Source File: RestRequestSender.java    From osiris with Apache License 2.0 6 votes vote down vote up
public void uploadVoid(String url, File f, String formName, Headers... headers) {

		FormDataMultiPart form = new FormDataMultiPart().field(formName, f, MediaType.MULTIPART_FORM_DATA_TYPE);
		String urlCreated = createURI(url);
		ClientConfig cc = new DefaultClientConfig();
		cc.getClasses().add(MultiPartWriter.class);
		WebResource webResource = Client.create(cc).resource(urlCreated);

		Builder builder = webResource.type(MULTIPART_MEDIA).accept(MEDIA);

		for (Headers h : headers) {
			builder.header(h.getKey(), h.getValue());
		}

		builder.post(form);
	}
 
Example 5
Source File: RestRequestSender.java    From osiris with Apache License 2.0 6 votes vote down vote up
public <T> ClientResponse<T> upload(String url, File f, Class<T> expectedResponse, Headers... headers) {
	
	@SuppressWarnings("resource")
	FormDataMultiPart form = new FormDataMultiPart();
	form.bodyPart(new FileDataBodyPart("file", f, MediaType.APPLICATION_OCTET_STREAM_TYPE));

	String urlCreated = createURI(url);
	ClientConfig cc = new DefaultClientConfig();
	cc.getClasses().add(MultiPartWriter.class);
	WebResource webResource = Client.create(cc).resource(urlCreated);
	Builder builder = webResource.type(MULTIPART_MEDIA).accept(MEDIA).accept("text/plain");
	for (Headers h : headers) {
		builder.header(h.getKey(), h.getValue());
	}

	com.sun.jersey.api.client.ClientResponse clienteResponse = null;

	clienteResponse = builder.post(com.sun.jersey.api.client.ClientResponse.class, form);

	return new ClientResponse<T>(clienteResponse, expectedResponse);
}
 
Example 6
Source File: RestRequestSender.java    From osiris with Apache License 2.0 6 votes vote down vote up
public ClientResponse<File> uploadNoMultipart(String url, File f, Headers... headers) throws FileNotFoundException {

		InputStream is = new FileInputStream(f);

		String urlCreated = createURI(url);
		ClientConfig cc = new DefaultClientConfig();
		cc.getClasses().add(MultiPartWriter.class);
		WebResource webResource = Client.create(cc).resource(urlCreated);
		Builder builder = webResource.type(MediaType.APPLICATION_OCTET_STREAM).accept(MEDIA).accept("text/plain");

		String sContentDisposition = "attachment; filename=\"" + f.getName() + "\"";
		builder.header("Content-Disposition", sContentDisposition);

		for (Headers h : headers) {
			builder.header(h.getKey(), h.getValue());
		}

		com.sun.jersey.api.client.ClientResponse clienteResponse = null;

		clienteResponse = builder.post(com.sun.jersey.api.client.ClientResponse.class, is);

		return new ClientResponse<File>(clienteResponse, File.class);
	}
 
Example 7
Source File: CPEClientSession.java    From tr069-simulator with MIT License 6 votes vote down vote up
private ACSResponse sendData (WebResource service, String reqString, List<NewCookie> cookies) {
	
	Builder builder = service.accept(MediaType.APPLICATION_XML)
			.type(MediaType.APPLICATION_XML);
	for ( NewCookie c : cookies ) {
	    //System.out.println( "Request Setting cookie  ======================== " + c.getName() + " = " + c.getValue() );
	    builder.cookie( c );
	}
	ClientResponse 	response 	= builder.post(ClientResponse.class, reqString);
	String 			rdata 		= response.getEntity(String.class);		
	ACSResponse 	acsresp 	= new ACSResponse();
	acsresp.setResponse(rdata);
	acsresp.setCookies(response.getCookies());
	acsresp.setHeaders(response.getHeaders());		
	if (rdata == null || rdata.length() <= 0) {
		//System.out.println("Response data is NULL ------> " + rdata + ". Closing connection  >>>>>>> ");
		//response.close();
	}
	//System.out.println( "Printing Response  Headers      -------------------- " + response.getHeaders());
	//System.out.println( "Printing Response getCookies    -------------------- " +  response.getCookies());
	//System.out.println( "Printing Response Data -------------------- " +  response. );
	System.out.println(rdata);
	return acsresp;
}
 
Example 8
Source File: DockerClient.java    From docker-java with Apache License 2.0 6 votes vote down vote up
public void startContainer(String containerId, HostConfig hostConfig) throws DockerException, NotFoundException {

		WebResource webResource = client.resource(restEndpointUrl + String.format("/containers/%s/start", containerId));

		try {
			LOGGER.trace("POST: {}", webResource);
			Builder builder = webResource.accept(MediaType.TEXT_PLAIN);
			if (hostConfig != null) {
				builder.type(MediaType.APPLICATION_JSON).post(hostConfig);
			} else {
				builder.post((HostConfig) null);
			}
		} catch (UniformInterfaceException exception) {
			if (exception.getResponse().getStatus() == 404) {
				throw new NotFoundException(String.format("No such container %s", containerId));
			} else if (exception.getResponse().getStatus() == 204) {
				//no error
				LOGGER.trace("Successfully started container {}", containerId);
			} else if (exception.getResponse().getStatus() == 500) {
				throw new DockerException("Server error", exception);
			} else {
				throw new DockerException(exception);
			}
		}
	}
 
Example 9
Source File: RestRequestSender.java    From osiris with Apache License 2.0 5 votes vote down vote up
public SimpleClientResponse invoke(RestMethod method, String url, BasicAuth basicAuth, Headers... headers) {

		Builder builder = createBuilder(url, headers, basicAuth);

		com.sun.jersey.api.client.ClientResponse clienteResponse = null;
		switch (method) {
		case GET:
			clienteResponse = builder.get(com.sun.jersey.api.client.ClientResponse.class);
			break;
		case DELETE:
			clienteResponse = builder.delete(com.sun.jersey.api.client.ClientResponse.class);
			break;
		case POST:
			clienteResponse = builder.post(com.sun.jersey.api.client.ClientResponse.class);
			break;
		case PUT:
			clienteResponse = builder.put(com.sun.jersey.api.client.ClientResponse.class);
			break;			
		case HEAD:
			clienteResponse = builder.head();
			break;

		default:
			throw new UnsupportedOperationException();
		}

		return new SimpleClientResponse(clienteResponse);
	}
 
Example 10
Source File: JiraRestAccess.java    From rtc2jira with GNU General Public License v2.0 3 votes vote down vote up
public ClientResponse postMultiPart(String ressource, FormDataMultiPart multiPart) {
  WebResource webResource = client.resource(restHome + ressource);

  Builder responseBuilder =
      webResource.header("Authorization", "Basic " + authentification).header("X-Atlassian-Token", "nocheck")
          .type(MediaType.MULTIPART_FORM_DATA_TYPE).accept(MediaType.APPLICATION_JSON);


  return responseBuilder.post(ClientResponse.class, multiPart);
}