com.cosmicpush.gateway.CosmicPushGateway Maven / Gradle / Ivy
/*
* Copyright (c) 2014 Jacob D. Parr
*
* 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.cosmicpush.gateway;
import com.cosmicpush.gateway.common.*;
import com.cosmicpush.gateway.internal.*;
import java.io.IOException;
import javax.ws.rs.client.*;
import javax.ws.rs.core.*;
import org.crazyyak.dev.common.exceptions.*;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.glassfish.jersey.uri.internal.JerseyUriBuilder;
public class CosmicPushGateway {
public static final String PUSH_NOTIFICATION = "/pushes/notification";
public static final String PUSH_USER_EVENT = "/pushes/user-event";
public static final String PUSH_IM = "/pushes/im";
public static final String PUSH_EMAIL = "/pushes/email";
public static final String PUSH_EMAIL_TO_SMS = "/pushes/email-to-sms";
private final String clientName;
private final String clientPassword;
private final String cosmicPushUrl;
private final CpObjectMapper objectMapper = new CpObjectMapper();
public CosmicPushGateway(String clientName, String clientPassword) {
this("http://www.cosmicpush.com/api", clientName, clientPassword);
}
public CosmicPushGateway(String cosmicPushUrl, String clientName, String clientPassword) {
this.clientName = ExceptionUtils.assertNotNull(clientName, "clientName");
this.clientPassword = ExceptionUtils.assertNotNull(clientPassword, "clientPassword");
this.cosmicPushUrl = ExceptionUtils.assertNotNull(cosmicPushUrl, "cosmicPushUrl");
}
public PushResponse push(Push push) {
push.validate(new RequestErrors()).assertNoErrors();
String json = translate(push);
return processRequest(push.getEndPoint(), json);
}
private String translate(Object request) {
try {
return objectMapper.writer().writeValueAsString(request);
} catch (IOException e) {
throw ApiException.internalServerError("IOException translating request to JSON", e);
}
}
private PushResponse processRequest(String url, String json) {
Client client = ClientBuilder.newBuilder().build();
UriBuilder uriBuilder = new JerseyUriBuilder().uri(cosmicPushUrl).path(url);
Response jerseyResponse = client.target(uriBuilder)
.register(HttpAuthenticationFeature.basic(clientName, clientPassword))
.request(MediaType.APPLICATION_JSON_TYPE)
.post(Entity.entity(json, MediaType.APPLICATION_JSON_TYPE));
int status = jerseyResponse.getStatus();
if (status / 100 != 2) {
String msg = String.format("Unexpected response (%s) from Cosmic Push Service.", status);
throw ApiException.internalServerError(msg);
}
try {
json = jerseyResponse.readEntity(String.class);
return objectMapper. readValue(json, PushResponse.class);
} catch (IOException e) {
throw ApiException.internalServerError("IOException translating response from JSON", e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy