Search in sources :

Example 21 with Builder

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

the class AbstractJerseyEurekaHttpClient method deleteStatusOverride.

@Override
public EurekaHttpResponse<Void> deleteStatusOverride(String appName, String id, InstanceInfo info) {
    String urlPath = "apps/" + appName + '/' + id + "/status";
    ClientResponse response = null;
    try {
        Builder requestBuilder = jerseyClient.resource(serviceUrl).path(urlPath).queryParam("lastDirtyTimestamp", info.getLastDirtyTimestamp().toString()).getRequestBuilder();
        addExtraHeaders(requestBuilder);
        response = requestBuilder.delete(ClientResponse.class);
        return anEurekaHttpResponse(response.getStatus()).headers(headersOf(response)).build();
    } finally {
        if (logger.isDebugEnabled()) {
            logger.debug("Jersey HTTP DELETE {}{}; statusCode={}", serviceUrl, urlPath, 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)

Example 22 with Builder

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

the class AbstractJerseyEurekaHttpClient method getApplication.

@Override
public EurekaHttpResponse<Application> getApplication(String appName) {
    String urlPath = "apps/" + appName;
    ClientResponse response = null;
    try {
        Builder requestBuilder = jerseyClient.resource(serviceUrl).path(urlPath).getRequestBuilder();
        addExtraHeaders(requestBuilder);
        response = requestBuilder.accept(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class);
        Application application = null;
        if (response.getStatus() == Status.OK.getStatusCode() && response.hasEntity()) {
            application = response.getEntity(Application.class);
        }
        return anEurekaHttpResponse(response.getStatus(), Application.class).headers(headersOf(response)).entity(application).build();
    } finally {
        if (logger.isDebugEnabled()) {
            logger.debug("Jersey HTTP GET {}{}; statusCode={}", serviceUrl, urlPath, 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) Application(com.netflix.discovery.shared.Application)

Example 23 with Builder

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

the class JerseyReplicationClient method sendHeartBeat.

/**
 * Compared to regular heartbeat, in the replication channel the server may return a more up to date
 * instance copy.
 */
@Override
public EurekaHttpResponse<InstanceInfo> sendHeartBeat(String appName, String id, InstanceInfo info, InstanceStatus overriddenStatus) {
    String urlPath = "apps/" + appName + '/' + id;
    ClientResponse response = null;
    try {
        WebResource webResource = jerseyClient.getClient().resource(serviceUrl).path(urlPath).queryParam("status", info.getStatus().toString()).queryParam("lastDirtyTimestamp", info.getLastDirtyTimestamp().toString());
        if (overriddenStatus != null) {
            webResource = webResource.queryParam("overriddenstatus", overriddenStatus.name());
        }
        Builder requestBuilder = webResource.getRequestBuilder();
        addExtraHeaders(requestBuilder);
        response = requestBuilder.accept(MediaType.APPLICATION_JSON_TYPE).put(ClientResponse.class);
        InstanceInfo infoFromPeer = null;
        if (response.getStatus() == Status.CONFLICT.getStatusCode() && response.hasEntity()) {
            infoFromPeer = response.getEntity(InstanceInfo.class);
        }
        return anEurekaHttpResponse(response.getStatus(), infoFromPeer).type(MediaType.APPLICATION_JSON_TYPE).build();
    } finally {
        if (logger.isDebugEnabled()) {
            logger.debug("[heartbeat] Jersey HTTP PUT {}; statusCode={}", urlPath, response == null ? "N/A" : response.getStatus());
        }
        if (response != null) {
            response.close();
        }
    }
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) Builder(com.sun.jersey.api.client.WebResource.Builder) EurekaJerseyClientBuilder(com.netflix.discovery.shared.transport.jersey.EurekaJerseyClientImpl.EurekaJerseyClientBuilder) WebResource(com.sun.jersey.api.client.WebResource) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Example 24 with Builder

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

the class RestClient method execute.

private HttpResponse execute(HttpRequest.Verb verb, URI uri, Map<String, Collection<String>> headers, Map<String, Collection<String>> params, IClientConfig overriddenClientConfig, Object requestEntity) throws Exception {
    HttpClientResponse thisResponse = null;
    final boolean bbFollowRedirects = Optional.ofNullable(overriddenClientConfig).flatMap(config -> config.getIfSet(CommonClientConfigKey.FollowRedirects)).orElse(bFollowRedirects);
    restClient.setFollowRedirects(bbFollowRedirects);
    if (logger.isDebugEnabled()) {
        logger.debug("RestClient sending new Request(" + verb + ": ) " + uri);
    }
    WebResource xResource = restClient.resource(uri.toString());
    if (params != null) {
        for (Map.Entry<String, Collection<String>> entry : params.entrySet()) {
            String name = entry.getKey();
            for (String value : entry.getValue()) {
                xResource = xResource.queryParam(name, value);
            }
        }
    }
    ClientResponse jerseyResponse;
    Builder b = xResource.getRequestBuilder();
    if (headers != null) {
        for (Map.Entry<String, Collection<String>> entry : headers.entrySet()) {
            String name = entry.getKey();
            for (String value : entry.getValue()) {
                b = b.header(name, value);
            }
        }
    }
    Object entity = requestEntity;
    switch(verb) {
        case GET:
            jerseyResponse = b.get(ClientResponse.class);
            break;
        case POST:
            jerseyResponse = b.post(ClientResponse.class, entity);
            break;
        case PUT:
            jerseyResponse = b.put(ClientResponse.class, entity);
            break;
        case DELETE:
            jerseyResponse = b.delete(ClientResponse.class);
            break;
        case HEAD:
            jerseyResponse = b.head();
            break;
        case OPTIONS:
            jerseyResponse = b.options(ClientResponse.class);
            break;
        default:
            throw new ClientException(ClientException.ErrorType.GENERAL, "You have to one of the REST verbs such as GET, POST etc.");
    }
    thisResponse = new HttpClientResponse(jerseyResponse, uri, overriddenClientConfig);
    if (thisResponse.getStatus() == 503) {
        thisResponse.close();
        throw new ClientException(ClientException.ErrorType.SERVER_THROTTLED);
    }
    return thisResponse;
}
Also used : DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) KeyStoreAwareSocketFactory(com.netflix.http4.ssl.KeyStoreAwareSocketFactory) AbstractSslContextFactory(com.netflix.client.ssl.AbstractSslContextFactory) URLDecoder(java.net.URLDecoder) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) URLSslContextFactory(com.netflix.client.ssl.URLSslContextFactory) LoggerFactory(org.slf4j.LoggerFactory) NFHttpClientFactory(com.netflix.http4.NFHttpClientFactory) ClientException(com.netflix.client.ClientException) BaseLoadBalancer(com.netflix.loadbalancer.BaseLoadBalancer) Map(java.util.Map) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) URI(java.net.URI) IClientConfigKey(com.netflix.client.config.IClientConfigKey) AbstractLoadBalancerAwareClient(com.netflix.client.AbstractLoadBalancerAwareClient) ConnRouteParams(org.apache.http.conn.params.ConnRouteParams) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) SchemeSocketFactory(org.apache.http.conn.scheme.SchemeSocketFactory) Collection(java.util.Collection) IClientConfig(com.netflix.client.config.IClientConfig) KeyStore(java.security.KeyStore) Scheme(org.apache.http.conn.scheme.Scheme) Optional(java.util.Optional) HttpRequest(com.netflix.client.http.HttpRequest) HttpConnectionParams(org.apache.http.params.HttpConnectionParams) ClientResponse(com.sun.jersey.api.client.ClientResponse) ApacheHttpClient4Handler(com.sun.jersey.client.apache4.ApacheHttpClient4Handler) NFHttpClient(com.netflix.http4.NFHttpClient) Property(com.netflix.client.config.Property) SocketException(java.net.SocketException) HttpClient(org.apache.http.client.HttpClient) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SocketTimeoutException(java.net.SocketTimeoutException) Builder(com.sun.jersey.api.client.WebResource.Builder) AbstractHttpClient(org.apache.http.impl.client.AbstractHttpClient) ApacheHttpClient4Config(com.sun.jersey.client.apache4.config.ApacheHttpClient4Config) ILoadBalancer(com.netflix.loadbalancer.ILoadBalancer) WebResource(com.sun.jersey.api.client.WebResource) DefaultApacheHttpClient4Config(com.sun.jersey.client.apache4.config.DefaultApacheHttpClient4Config) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) ClientFactory(com.netflix.client.ClientFactory) NFHttpMethodRetryHandler(com.netflix.http4.NFHttpMethodRetryHandler) Logger(org.slf4j.Logger) ApacheHttpClient4(com.sun.jersey.client.apache4.ApacheHttpClient4) RequestSpecificRetryHandler(com.netflix.client.RequestSpecificRetryHandler) UserTokenHandler(org.apache.http.client.UserTokenHandler) File(java.io.File) CommonClientConfigKey(com.netflix.client.config.CommonClientConfigKey) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Client(com.sun.jersey.api.client.Client) HttpContext(org.apache.http.protocol.HttpContext) HttpParams(org.apache.http.params.HttpParams) HttpResponse(com.netflix.client.http.HttpResponse) Pair(com.netflix.util.Pair) HttpHost(org.apache.http.HttpHost) ClientSslSocketFactoryException(com.netflix.client.ssl.ClientSslSocketFactoryException) ClientResponse(com.sun.jersey.api.client.ClientResponse) Builder(com.sun.jersey.api.client.WebResource.Builder) WebResource(com.sun.jersey.api.client.WebResource) Collection(java.util.Collection) ClientException(com.netflix.client.ClientException) Map(java.util.Map)

Example 25 with Builder

use of com.sun.jersey.api.client.WebResource.Builder in project zookeeper by apache.

the class SessionTest method testCreateEphemeralZNode.

@Test
public void testCreateEphemeralZNode() throws KeeperException, InterruptedException, IOException {
    ZSession session = createSession("30");
    WebResource wr = znodesr.path("/").queryParam("op", "create").queryParam("name", "ephemeral-test").queryParam("ephemeral", "true").queryParam("session", session.id).queryParam("null", "true");
    Builder b = wr.accept(MediaType.APPLICATION_JSON);
    ClientResponse cr = b.post(ClientResponse.class);
    Assert.assertEquals(ClientResponse.Status.CREATED, cr.getClientResponseStatus());
    Stat stat = new Stat();
    zk.getData("/ephemeral-test", false, stat);
    ZooKeeper sessionZK = ZooKeeperService.getClient(CONTEXT_PATH, session.id);
    Assert.assertEquals(stat.getEphemeralOwner(), sessionZK.getSessionId());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) Stat(org.apache.zookeeper.data.Stat) ZooKeeper(org.apache.zookeeper.ZooKeeper) Builder(com.sun.jersey.api.client.WebResource.Builder) WebResource(com.sun.jersey.api.client.WebResource) ZSession(org.apache.zookeeper.server.jersey.jaxb.ZSession) Test(org.junit.Test)

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