Package com.cosmicpush.gateway

Source Code of com.cosmicpush.gateway.CosmicPushGateway

/*
* 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.pub.common.*;
import com.cosmicpush.pub.internal.RequestErrors;
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 {

  private final String clientName;
  private final String clientPassword;
  private final String cosmicPushUrl;
  private final CpGatewayObjectMapper objectMapper = new CpGatewayObjectMapper();

  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);
    }
  }
}
TOP

Related Classes of com.cosmicpush.gateway.CosmicPushGateway

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.