io.sprucehill.mandrill.service.AbstractService Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mandrill-java-api-wrapper Show documentation
Show all versions of mandrill-java-api-wrapper Show documentation
Easier access to Mandrill using Java
The newest version!
/*
Copyright 2013-2014 SpruceHill.io GmbH
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 io.sprucehill.mandrill.service;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.sprucehill.mandrill.data.request.AbstractPayload;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.PostConstruct;
import java.io.IOException;
/**
* @author Michael Duergner
*/
public abstract class AbstractService {
final Logger logger = LoggerFactory.getLogger(getClass());
String baseUrl = "https://mandrillapp.com/api/1.0";
HttpClient httpClient;
ObjectMapper objectMapper;
String key;
/**
*
* @param baseUrl
*/
public void setBaseUrl(String baseUrl) {
this.baseUrl = baseUrl;
}
/**
*
* @param httpClient
*/
public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}
/**
*
* @param objectMapper
*/
public void setObjectMapper(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
}
/**
*
* @param key
*/
public void setKey(String key) {
this.key = key;
}
@PostConstruct
public final void postConstruct() {
onPostConstruct();
}
void onPostConstruct() {
if (null == httpClient) {
httpClient = new DefaultHttpClient(new PoolingClientConnectionManager());
}
if (null == objectMapper) {
objectMapper = new ObjectMapper();
}
}
,U extends AbstractPayload> void integrateDefaultValues(AbstractPayload.Init payloadBuilder) {
if (!payloadBuilder.hasKey() && null != key && !key.isEmpty()) {
payloadBuilder.withKey(key);
}
}
T send(final AbstractPayload payload, TypeReference responseClass, Class errorClass) throws E, IOException {
try {
HttpPost request = new HttpPost(baseUrl+payload.getPath());
String body = objectMapper.writeValueAsString(payload);
logger.info(body);
request.setEntity(new StringEntity(body,"UTF-8"));
HttpResponse response = httpClient.execute(request);
if (200 == response.getStatusLine().getStatusCode()) {
T result = objectMapper.readValue(response.getEntity().getContent(),responseClass);
return result;
}
else {
E error = objectMapper.readValue(response.getEntity().getContent(),errorClass);
logger.debug("Got error {} while calling {}.", error.toString(), payload.getPath());
throw error;
}
}
catch (IOException e) {
logger.debug("Got {} while calling {}.",e.getClass().getSimpleName(),payload.getPath());
throw e;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy