Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.phonedeck.gcm4j;
  
  import java.net.URL;
  import java.util.Date;
 import java.util.List;
 
 
 
 public abstract class AbstractDefaultGcm implements Gcm {
 
     private static final String AUTH_KEY = "key=";
 
     private static final Logger LOG = LoggerFactory.getLogger(AbstractDefaultGcm.class);
 
     private final ObjectMapper objectMapper;
 
     private final URL gcmUrl;
 
     private final String authorizationValue;
 
     private final ConnectionFactory connectionFactory;
 
     private final List<GcmFilterfilters;
 
     public AbstractDefaultGcm(GcmConfig gcmConfig) {
         this. = createObjectMapper();
         this. = getConfigEndpoint(gcmConfig.getEndpoint());
         this. = buildAuthString(gcmConfig.getKey());
         this. = gcmConfig.getConnectionFactory() != null ? gcmConfig.getConnectionFactory() : new DefaultConnectionFactory();
         this. = gcmConfig.getFilters() != null ? ImmutableList.copyOf(gcmConfig.getFilters()) : ImmutableList.<GcmFilter>of();
     }
 
     private static String buildAuthString(String key) {
         return  + key;
     }
 
     @Override
     public ListenableFuture<GcmResponsesend(GcmRequest request) {
         return new Chain().next(request);
     }
 
     private static URL getConfigEndpoint(URL configEndpoint) {
         try {
             return configEndpoint != null ? configEndpoint : new URL("https://android.googleapis.com/gcm/send");
         } catch (MalformedURLException ex) {
             // should never happen
             throw new RuntimeException(ex);
         }
     }
 
     private static ObjectMapper createObjectMapper() {
         ObjectMapper objectMapper = new ObjectMapper();
         objectMapper.setSerializationInclusion(.);
         return objectMapper;
     }
 
 
 
     public ObjectMapper getObjectMapper() {
         return ;
     }
 
     public List<GcmFiltergetFilters() {
         return ;
     }
 
     public URL getGcmUrl() {
         return ;
     }
 
         return ;
     }
 
     private String getAuthorization(GcmRequest request) {
         String authToken = request.getKey();
         if (authToken == null) {
             if ( == null) {
                 throw new RuntimeException("Gcm client authorisation token can only be null when requests provide their own"
                         + " token");
             }
             return ;
         }
         else {
             return buildAuthString(authToken);
        }
    }
    protected GcmResponse executeRequest(GcmRequest requestthrows IOException {
        byte[] content = .writeValueAsBytes(request);
        conn.setRequestMethod("POST");
        conn.addRequestProperty("Authorization"getAuthorization(request));
        conn.addRequestProperty("Content-Type""application/json");
        conn.setDoOutput(true);
        conn.setFixedLengthStreamingMode(content.length);
        try (OutputStream os = conn.getOutputStream()) {
            IOUtils.write(contentos);
        }
        catch (Exception ex)
        {
            throw new GcmNetworkException("Error sending HTTP request to GCM"ex);
        }
        GcmResponse response;
        byte[] rsp = null;
        try (InputStream is = conn.getInputStream()) {
            rsp = IOUtils.toByteArray(is);
            response = .readValue(rspGcmResponse.class);
        } catch (IOException ex) {
            try (InputStream es = conn.getErrorStream()) {
                String str = es != null ? IOUtils.toString(es) : "No error details provided";
                int responseCode = conn.getResponseCode();
                if (responseCode < 500)
                {
                    throw new GcmNetworkException(conn.getResponseCode(), str.trim(), ex);
                }
                else
                {
                    Long retryAfter = checkForRetryInResponse(conn);
                    throw new GcmNetworkException(conn.getResponseCode(), str.trim(), retryAfterex);
                }
            }
        }
        response.setRequest(request);
        Long retryAfter = checkForRetryInResponse(conn);
        response.setRetryAfter(retryAfter);
        Iterator<Stringi1 = request.getRegistrationIds().iterator();
        Iterator<Resulti2 = response.getResults().iterator();
        while (i1.hasNext() && i2.hasNext()) {
            i2.next().setRequestedRegistrationId(i1.next());
        }
        if (i1.hasNext()) {
            .warn("Protocol error: Less results than requested registation IDs");
        }
        if (i2.hasNext()) {
            .warn("Protocol error: More results than requested registation IDs");
        }
        return response;
    }
        String retryAfterStr = conn.getHeaderField("Retry-After");
        if (retryAfterStr == null) {
            return null;
        }
        Long delta = Longs.tryParse(retryAfterStr);
        if (delta != null) {
            return delta * 1000;
        } else {
            Date date = DateUtils.parseDate(retryAfterStr);
            long delay = date.getTime() - System.currentTimeMillis();
            return delay < 0 ? 0 : delay;
        }
    }
    protected abstract ListenableFuture<GcmResponseexecuteRequestFuture(final GcmRequest request);


    
Nested class to implement the FilterChain.
    private final class Chain implements FilterChain {
        private final Iterator<GcmFilteri = .iterator();
        @Override
        public ListenableFuture<GcmResponsenext(GcmRequest request) {
            if (.hasNext()) {
                return .next().filter(requestthis);
            } else {
                return executeRequestFuture(request);
            }
        }
        @Override
        public Gcm getGcm() {
            return AbstractDefaultGcm.this;
        }
    }
New to GrepCode? Check out our FAQ X