Search in sources :

Example 16 with Builder

use of com.sun.jersey.api.client.WebResource.Builder in project winery by eclipse.

the class TopologyTemplateResource method triggerGenerateBuildPlan.

/**
 * @param uriInfo the URI ending with "topologytemplate/" of a service template
 */
@GET
@Produces(MediaType.TEXT_PLAIN)
public Response triggerGenerateBuildPlan(@Context UriInfo uriInfo) {
    String plansURI = uriInfo.getAbsolutePath().resolve("../plans/").toString();
    String csarURI = uriInfo.getAbsolutePath().resolve("../?csar").toString();
    String request = "<generatePlanForTopology><CSARURL>";
    request += csarURI;
    request += "</CSARURL><PLANPOSTURL>";
    request += plansURI;
    request += "</PLANPOSTURL></generatePlanForTopology>";
    Client client = Client.create();
    Builder wr = null;
    if (RestUtils.isResourceAvailable("http://localhost:1339/planbuilder")) {
        wr = client.resource("http://localhost:1339/planbuilder/sync").type(MediaType.APPLICATION_XML);
    } else if (RestUtils.isResourceAvailable("http://localhost:1337/containerapi/planbuilder")) {
        wr = client.resource("http://localhost:1337/containerapi/planbuilder/sync").type(MediaType.APPLICATION_XML);
    }
    try {
        wr.post(String.class, request);
    } catch (com.sun.jersey.api.client.UniformInterfaceException e) {
        return Response.serverError().entity(e.getMessage()).build();
    }
    return Response.ok().build();
}
Also used : Builder(com.sun.jersey.api.client.WebResource.Builder) Client(com.sun.jersey.api.client.Client) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 17 with Builder

use of com.sun.jersey.api.client.WebResource.Builder in project pentaho-platform by pentaho.

the class CommandLineProcessor method performExport.

/**
 * REST Service Export
 *
 * @throws ParseException
 *           --export --url=http://localhost:8080/pentaho --username=admin --password=password
 *           --file-path=c:/temp/export.zip --charset=UTF-8 --path=public/pentaho-solutions/steel-wheels
 *           --logfile=c:/temp/steel-wheels.log --withManifest=true
 * @throws java.io.IOException
 */
private void performExport() throws ParseException, IOException, InitializationException {
    String contextURL = getOptionValue(INFO_OPTION_URL_NAME, true, false);
    String path = getOptionValue(INFO_OPTION_PATH_NAME, true, false);
    String withManifest = getOptionValue(INFO_OPTION_WITH_MANIFEST_NAME, false, true);
    String effPath = RepositoryPathEncoder.encodeURIComponent(RepositoryPathEncoder.encodeRepositoryPath(path));
    if (// remove trailing slash
    effPath.lastIndexOf(":") == effPath.length() - 1 && effPath.length() > 1) {
        // allow user to enter "--path=/"
        effPath = effPath.substring(0, effPath.length() - 1);
    }
    String logFile = getOptionValue(INFO_OPTION_LOGFILE_NAME, false, true);
    String exportURL = contextURL + "/api/repo/files/" + effPath + "/download?withManifest=" + ("false".equals(withManifest) ? "false" : "true");
    // path is validated before executing
    String filepath = getOptionValue(INFO_OPTION_FILEPATH_NAME, true, false);
    if (!isValidExportPath(filepath, logFile)) {
        throw new ParseException(Messages.getInstance().getString("CommandLineProcessor.ERROR_0005_INVALID_FILE_PATH", filepath));
    }
    initRestService();
    WebResource resource = client.resource(exportURL);
    // Response response
    Builder builder = resource.type(MediaType.MULTIPART_FORM_DATA).accept(MediaType.TEXT_HTML_TYPE);
    ClientResponse response = builder.get(ClientResponse.class);
    if (response != null && response.getStatus() == 200) {
        String filename = getOptionValue(INFO_OPTION_FILEPATH_NAME, true, false);
        final InputStream input = response.getEntityInputStream();
        createZipFile(filename, input);
        input.close();
        String message = Messages.getInstance().getString("CommandLineProcessor.INFO_EXPORT_COMPLETED").concat("\n");
        message += Messages.getInstance().getString("CommandLineProcessor.INFO_RESPONSE_STATUS", response.getStatus());
        message += "\n";
        message += Messages.getInstance().getString("CommandLineProcessor.INFO_EXPORT_WRITTEN_TO", filename);
        if (logFile != null && !"".equals(logFile)) {
            System.out.println(message);
            writeFile(message, logFile);
        }
        System.out.println(Messages.getInstance().getString("CommandLineProcessor.INFO_EXPORT_SUCCESSFUL"));
    } else if (response != null && response.getStatus() == 403) {
        errorMessage = Messages.getInstance().getErrorString("CommandLineProcessor.ERROR_0007_FORBIDDEN", path);
        System.out.println(errorMessage);
    } else if (response != null && response.getStatus() == 404) {
        errorMessage = Messages.getInstance().getErrorString("CommandLineProcessor.ERROR_0004_UNKNOWN_SOURCE", path);
        System.out.println(errorMessage);
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Builder(com.sun.jersey.api.client.WebResource.Builder) WebResource(com.sun.jersey.api.client.WebResource) ParseException(org.apache.commons.cli.ParseException)

Example 18 with Builder

use of com.sun.jersey.api.client.WebResource.Builder in project ORCID-Source by ORCID.

the class Orcid12APIClient method fetchPublicProfile.

/**
 * Fetches the profile from the ORCID public API v1.2
 *
 * @param orcid
 * @return
 * @throws LockedRecordException
 * @throws IOException
 */
public byte[] fetchPublicProfile(String orcid, String mediaType) throws LockedRecordException, DeprecatedRecordException, IOException {
    WebResource webResource = jerseyClient.resource(baseUri).path(orcid + "/orcid-profile");
    webResource.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, false);
    Builder builder = webResource.accept(mediaType).header("Authorization", "Bearer " + accessToken);
    ClientResponse response = builder.get(ClientResponse.class);
    if (response.getStatus() != 200) {
        switch(response.getStatus()) {
            case 301:
                OrcidDeprecated orcidDeprecated = response.getEntity(OrcidDeprecated.class);
                throw new DeprecatedRecordException(orcidDeprecated);
            case 409:
                OrcidMessage orcidMessage = response.getEntity(OrcidMessage.class);
                throw new LockedRecordException(orcidMessage);
            default:
                LOG.error("Unable to fetch public record " + orcid + " on API 1.2 HTTP error code: " + response.getStatus());
                throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
        }
    }
    byte[] elements = IOUtils.toByteArray(response.getEntityInputStream());
    response.close();
    return elements;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) LockedRecordException(org.orcid.listener.exception.LockedRecordException) OrcidDeprecated(org.orcid.jaxb.model.message.OrcidDeprecated) DeprecatedRecordException(org.orcid.listener.exception.DeprecatedRecordException) Builder(com.sun.jersey.api.client.WebResource.Builder) OrcidMessage(org.orcid.jaxb.model.message.OrcidMessage) WebResource(com.sun.jersey.api.client.WebResource)

Example 19 with Builder

use of com.sun.jersey.api.client.WebResource.Builder in project ORCID-Source by ORCID.

the class Orcid20APIClient method fetchFunding.

public Funding fetchFunding(String orcid, Long putCode) {
    WebResource webResource = jerseyClient.resource(baseUri).path(orcid + "/funding/" + putCode);
    webResource.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, false);
    Builder builder = webResource.accept(MediaType.APPLICATION_XML).header("Authorization", "Bearer " + accessToken);
    ClientResponse response = builder.get(ClientResponse.class);
    if (response.getStatus() != 200) {
        switch(response.getStatus()) {
            default:
                LOG.error("Unable to fetch funding record " + orcid + "/" + putCode + " on API 2.0 HTTP error code: " + response.getStatus());
                throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
        }
    }
    return response.getEntity(Funding.class);
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) Builder(com.sun.jersey.api.client.WebResource.Builder) CacheBuilder(com.google.common.cache.CacheBuilder) WebResource(com.sun.jersey.api.client.WebResource)

Example 20 with Builder

use of com.sun.jersey.api.client.WebResource.Builder in project eureka by Netflix.

the class AbstractJerseyEurekaHttpClient method register.

@Override
public EurekaHttpResponse<Void> register(InstanceInfo info) {
    String urlPath = "apps/" + info.getAppName();
    ClientResponse response = null;
    try {
        Builder resourceBuilder = jerseyClient.resource(serviceUrl).path(urlPath).getRequestBuilder();
        addExtraHeaders(resourceBuilder);
        response = resourceBuilder.header("Accept-Encoding", "gzip").type(MediaType.APPLICATION_JSON_TYPE).accept(MediaType.APPLICATION_JSON).post(ClientResponse.class, info);
        return anEurekaHttpResponse(response.getStatus()).headers(headersOf(response)).build();
    } finally {
        if (logger.isDebugEnabled()) {
            logger.debug("Jersey HTTP POST {}{} with instance {}; statusCode={}", serviceUrl, urlPath, info.getId(), response == null ? "N/A" : response.getStatus());
        }
        if (response != null) {
            response.close();
        }
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) EurekaHttpResponseBuilder(com.netflix.discovery.shared.transport.EurekaHttpResponse.EurekaHttpResponseBuilder) Builder(com.sun.jersey.api.client.WebResource.Builder)

Aggregations

Builder (com.sun.jersey.api.client.WebResource.Builder)28 ClientResponse (com.sun.jersey.api.client.ClientResponse)24 WebResource (com.sun.jersey.api.client.WebResource)20 EurekaHttpResponseBuilder (com.netflix.discovery.shared.transport.EurekaHttpResponse.EurekaHttpResponseBuilder)8 Test (org.junit.Test)6 Stat (org.apache.zookeeper.data.Stat)4 InstanceInfo (com.netflix.appinfo.InstanceInfo)3 Map (java.util.Map)3 CacheBuilder (com.google.common.cache.CacheBuilder)2 Client (com.sun.jersey.api.client.Client)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 ZSession (org.apache.zookeeper.server.jersey.jaxb.ZSession)2 OrcidError (org.orcid.jaxb.model.error_v2.OrcidError)2 DeprecatedRecordException (org.orcid.listener.exception.DeprecatedRecordException)2 LockedRecordException (org.orcid.listener.exception.LockedRecordException)2 AbstractLoadBalancerAwareClient (com.netflix.client.AbstractLoadBalancerAwareClient)1 ClientException (com.netflix.client.ClientException)1 ClientFactory (com.netflix.client.ClientFactory)1 RequestSpecificRetryHandler (com.netflix.client.RequestSpecificRetryHandler)1