com.sun.jersey.api.client.WebResource.Builder Java Examples

The following examples show how to use com.sun.jersey.api.client.WebResource.Builder. 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
private Builder createBuilder(String url, Headers[] headers, BasicAuth auth) {
	WebResource resource;
	if (null != auth) {
		resource = Client.create(config).resource(createURI(url));
		resource.addFilter(new com.sun.jersey.api.client.filter.HTTPBasicAuthFilter(auth.getName(), auth
				.getPassword()));
	} else {
		resource = Client.create(config).resource(createURI(url));
	}

	Builder builder = resource.accept(acceptMediaType).type(applicationType);
	for (Headers h : headers) {
		builder.header(h.getKey(), h.getValue());
	}
	return builder;
}
 
Example #3
Source File: DockerClient.java    From docker-java with Apache License 2.0 6 votes vote down vote up
public ClientResponse copyFile(String containerId, String resource) throws DockerException {
	CopyConfig copyConfig = new CopyConfig();
	copyConfig.setResource(resource);

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

	try {
		LOGGER.trace("POST: " + webResource.toString());
		WebResource.Builder builder =
				webResource.accept(MediaType.APPLICATION_OCTET_STREAM_TYPE).type("application/json");

		return builder.post(ClientResponse.class, copyConfig.toString());
	} catch (UniformInterfaceException exception) {
		if (exception.getResponse().getStatus() == 400) {
			throw new DockerException("bad parameter");
		} else if (exception.getResponse().getStatus() == 404) {
			throw new DockerException(String.format("No such container %s", containerId));
		} else if (exception.getResponse().getStatus() == 500) {
			throw new DockerException("Server error", exception);
		} else {
			throw new DockerException(exception);
		}
	}
}
 
Example #4
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 #5
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 #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: 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 #8
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 #9
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 #10
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 #11
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, GenericType<T> genericType, 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;
	default:
		throw new UnsupportedOperationException();
	}

	return new ClientResponse<T>(clienteResponse, genericType);
}
 
Example #12
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, Class<T> expectedResponse, 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 HEAD:
		clienteResponse = builder.head();
		break;
	default:
		throw new UnsupportedOperationException();
	}

	return new ClientResponse<T>(clienteResponse, expectedResponse);
}
 
Example #13
Source File: RestHandler.java    From Insights with Apache License 2.0 6 votes vote down vote up
private static Builder getRequestBuilder(String url, JsonElement requestJson, Map<String, String> headers) {
	Client client = Client.create();
	//client.addFilter(new LoggingFilter(System.out));
	WebResource resource = client.resource(url);
	Builder requestBuilder = resource.accept(MediaType.APPLICATION_JSON);
	if(headers != null && headers.size() > 0){
		for(Map.Entry<String, String> entry : headers.entrySet()){
			requestBuilder = requestBuilder.header(entry.getKey(), entry.getValue());
		}
	}
	requestBuilder = requestBuilder.type(MediaType.APPLICATION_JSON);
	if(requestJson != null){
		requestBuilder = requestBuilder.entity(requestJson.toString());
	}
	return requestBuilder;
}
 
Example #14
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 #15
Source File: JiraRestAccess.java    From rtc2jira with GNU General Public License v2.0 5 votes vote down vote up
private Builder createJsonResponseBuilder(String resource) {
  WebResource webResource = client.resource(restHome + resource);
  Builder responseBuilder =
      webResource.header("Authorization", "Basic " + authentification).type(MediaType.APPLICATION_JSON)
          .accept(MediaType.APPLICATION_JSON);
  return responseBuilder;
}
 
Example #16
Source File: RestRequestSender.java    From osiris with Apache License 2.0 5 votes vote down vote up
public ClientResponse<InputStream> download(String url,String type, String accept,  Headers... headers) {

	WebResource resource = Client.create(config).resource(createURI(url));
	
	Builder builder = resource.accept(accept,MEDIA).type(type);
	for (Headers h : headers) {
		builder.header(h.getKey(), h.getValue());
	}

	com.sun.jersey.api.client.ClientResponse clienteResponse = null;
	clienteResponse = builder.get(com.sun.jersey.api.client.ClientResponse.class);
	
	return new ClientResponse<InputStream>(clienteResponse, InputStream.class);
}
 
Example #17
Source File: TestYandexTranslate.java    From localization_nifi with Apache License 2.0 4 votes vote down vote up
private TestRunner createTestRunner(final int statusCode) {
    return TestRunners.newTestRunner(new YandexTranslate() {
        @Override
        protected Builder prepareResource(final String key, final List<String> text, final String sourceLanguage, final String destLanguage) {
            final WebResource.Builder builder = Mockito.mock(WebResource.Builder.class);

            Mockito.doAnswer(new Answer<ClientResponse>() {
                @Override
                public ClientResponse answer(final InvocationOnMock invocation) throws Throwable {
                    final ClientResponse response = Mockito.mock(ClientResponse.class);

                    final StatusType statusType = new StatusType() {
                        @Override
                        public int getStatusCode() {
                            return statusCode;
                        }

                        @Override
                        public String getReasonPhrase() {
                            return String.valueOf(statusCode);
                        }

                        @Override
                        public Family getFamily() {
                            return statusCode == 200 ? Family.SUCCESSFUL : Family.SERVER_ERROR;
                        }
                    };

                    Mockito.when(response.getStatus()).thenReturn(statusCode);
                    Mockito.when(response.getStatusInfo()).thenReturn(statusType);

                    if (statusCode == 200) {
                        final Translation translation = new Translation();
                        translation.setCode(statusCode);
                        translation.setLang(destLanguage);

                        final List<String> translationList = new ArrayList<>();
                        for (final String original : text) {
                            final String translated = translations.get(original);
                            translationList.add(translated == null ? original : translated);
                        }

                        translation.setText(translationList);

                        Mockito.when(response.getEntity(Translation.class)).thenReturn(translation);
                    }

                    return response;
                }
            }).when(builder).post(ClientResponse.class);
            return builder;
        }
    });
}
 
Example #18
Source File: StorageClient.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
private Builder addAuthenticationHeader(final WebResource webResource)
{
  return webResource.header(StorageConstants.PARAM_AUTHENTICATION_TOKEN, config.getAuthenticationToken());
}
 
Example #19
Source File: ClientBase.java    From conductor with Apache License 2.0 4 votes vote down vote up
private Builder getWebResourceBuilder(URI URI, Object entity) {
    return client.resource(URI).type(MediaType.APPLICATION_JSON).entity(entity).accept(MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON);
}
 
Example #20
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);
}