com.ovh.ws.jsonizer.common.http.JerseyWebResource 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 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.Client;
import com.sun.jersey.api.client.ClientHandlerException;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource.Builder;
import com.sun.jersey.core.util.MultivaluedMapImpl;
/* package */class JerseyWebResource implements WebResource {
private static final Logger LOG = LoggerFactory.getLogger(JerseyWebResource.class);
private final com.sun.jersey.api.client.WebResource resource;
private final Map headers = new HashMap();
private final MultivaluedMap requestParams = new MultivaluedMapImpl();
public JerseyWebResource(Client client, URI uri) {
resource = client.resource(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 String execute() throws OvhWsException {
try {
LOG.debug("Jsonizer : {}", resource);
return builder().type(MediaType.APPLICATION_FORM_URLENCODED_TYPE).post(String.class, requestParams);
} catch (ClientHandlerException che) {
throw new OvhWsException(Type.ServerError, "A server error occured, please retry later", che);
} catch (UniformInterfaceException uie) {
throw new OvhWsException(Type.ServerError, "A server error occurred, please retry later", uie);
}
}
private Builder builder() {
Builder builder = resource.getRequestBuilder();
for (Map.Entry e : headers.entrySet()) {
builder = builder.header(e.getKey(), e.getValue());
}
return builder;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy