com.ovh.ws.jsonizer.common.http.JerseyAsyncWebResource Maven / Gradle / Ivy
The newest version!
/**
* Copyright 2012, OVH. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and limitations under the License.
*/
package com.ovh.ws.jsonizer.common.http;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.ovh.ws.common.OvhWsException;
import com.ovh.ws.common.OvhWsException.Type;
import com.sun.jersey.api.client.AsyncWebResource.Builder;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.async.TypeListener;
import com.sun.jersey.core.util.MultivaluedMapImpl;
/* package */class JerseyAsyncWebResource implements AsyncWebResource {
private static final Logger LOG = LoggerFactory.getLogger(JerseyAsyncWebResource.class);
private final com.sun.jersey.api.client.AsyncWebResource resource;
private final Map headers = new HashMap();
private final MultivaluedMap requestParams = new MultivaluedMapImpl();
public JerseyAsyncWebResource(Client client, URI uri) {
resource = client.asyncResource(uri);
}
@Override
public void queryParams(Map params) throws OvhWsException {
for (Map.Entry e : params.entrySet()) {
requestParams.add(e.getKey(), e.getValue());
}
}
@Override
public void headers(Map headers) {
this.headers.putAll(headers);
}
@Override
public void execute(HttpRequestListener listener) {
LOG.debug("Jsonizer : {}", resource);
TypeListener typeListener = createTypeListener(listener);
builder().type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(typeListener, requestParams);
}
private Builder builder() {
Builder builder = resource.getRequestBuilder();
for (Map.Entry e : headers.entrySet()) {
builder = builder.header(e.getKey(), e.getValue());
}
return builder;
}
private TypeListener createTypeListener(final HttpRequestListener listener) {
return new TypeListener(String.class) {
@Override
public void onComplete(Future futureResponse) {
try {
String result = getResult(futureResponse);
listener.onComplete(result);
} catch (OvhWsException e) {
listener.onFailure(e);
}
}
};
}
private String getResult(Future response) throws OvhWsException {
try {
return response.get();
} catch (ExecutionException e) {
throw new OvhWsException(Type.UnrecognizedError, "Error Jersey Client execution", e);
} catch (InterruptedException e) {
throw new OvhWsException(Type.UnrecognizedError, "Error Jersey Client Interrupted", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy