Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
DeviceAtlas is the world's fastest, most accurate device detection solution
providing real-time information on all mobile and other devices accessing the web.
/*
* The MIT License (MIT)
*
* Copyright (c) 2016 Afilias Technologies Ltd
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom
* the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.deviceatlas.cloud.deviceidentification.service;
import com.deviceatlas.cloud.deviceidentification.cacheprovider.CacheException;
import com.deviceatlas.cloud.deviceidentification.client.ClientException;
import com.deviceatlas.cloud.deviceidentification.client.ClientConstants;
import com.deviceatlas.cloud.deviceidentification.client.ActionConstants;
import com.deviceatlas.cloud.deviceidentification.parser.JsonException;
import com.deviceatlas.cloud.deviceidentification.parser.JsonParser;
import com.deviceatlas.cloud.deviceidentification.endpoint.EndPoint;
import com.deviceatlas.cloud.deviceidentification.utils.NetworkUtils;
import com.deviceatlas.cloud.deviceidentification.utils.RequestBuilderUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.net.Proxy;
import java.io.UnsupportedEncodingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class EndPointService {
private CacheService cacheService;
private boolean autoServerRanking = true;
private boolean serverListIsManual = false;
private int autoServerLatencyNumRequests = 3;
private int autoServerRankingMaxFailures = 1;
private int cloudServiceTimeout = 3;
private String selfAutoRanking = "n"; // auto ranking by API or user
private String licenceKey = null;
private String errorMessage = "";
// server fail-over and ranking system
private String lastUsedCloudUrl;
/* For tracking ranking mechanism */
protected String rankingStatus;
/* proxy object */
private Proxy proxy;
// to avoid getting stuck in and end-less loop when rankServers() calls getEndPoints()
private boolean getServersRankIfRequired = true;
protected List calledServers; // for test and debug
private List fatalErrors = null; // for test and debug
private byte failoverAction;
private EndPoint[] endPoints = {
new EndPoint("http://region0.deviceatlascloud.com", "80"),
new EndPoint("http://region1.deviceatlascloud.com", "80"),
new EndPoint("http://region2.deviceatlascloud.com", "80"),
new EndPoint("http://region3.deviceatlascloud.com", "80"),
};
private static final Logger LOGGER = LoggerFactory.getLogger(EndPointService.class);
public EndPointService(CacheService cacheService) {
this.cacheService = cacheService;
}
/**
* Gets the current action to take regarding the connection service
*
* @return failoverAction
*/
public byte getFailoverAction() {
return failoverAction;
}
/**
* Sets the action level
*
* @param failoverAction
*/
public void setFailoverAction(byte failoverAction) {
this.failoverAction = failoverAction;
}
/**
* Returns the underlying cache layer
*
* @return cacheService
*/
public CacheService getCacheService() {
return cacheService;
}
/**
* true if the server list is the manual type
*
* @return serverListIsManual
*/
public boolean isServerListManual() {
return serverListIsManual;
}
/**
* Sets the type of server list (manual or not)
*
* @param serverListIsManual
*/
public void setServerListIsManual(boolean serverListIsManual) {
this.serverListIsManual = serverListIsManual;
}
/**
* Gets the last endpoint url used when call the service
*
* @return lastUsedCloudUrl
*/
public String getCloudUrl() {
return lastUsedCloudUrl;
}
/**
* Sets the last endpoint url used when call the service
*
* @param lastUsedCloudUrl
*/
public void setCloudUrl(String lastUsedCloudUrl) {
this.lastUsedCloudUrl = lastUsedCloudUrl;
}
/**
* Returns the cloud service's timeout
*
* @return cloudServiceTimeout
*/
public int getCloudServiceTimeout() {
return cloudServiceTimeout;
}
/**
* Sets the cloud service's timeout
*
* @param cloudServiceTimeout
*/
public void setCloudServiceTimeout(int cloudServiceTimeout) {
this.cloudServiceTimeout = cloudServiceTimeout;
}
/**
* Returns the maximum amount of failures for server ranking's checks
*
* @return autoServerRankingMaxFailures
*/
public int getAutoServerRankingMaxFailures() {
return autoServerRankingMaxFailures;
}
/**
* Sets the maximum amount of failures for server ranking's checks
*
* @param autoServerRankingMaxFailures
*/
public void setAutoServerRankingMaxFailures(int autoServerRankingMaxFailures) {
this.autoServerRankingMaxFailures = autoServerRankingMaxFailures;
}
/**
* Returns the maximum number of requests
*
* @return autoServerLatencyNumRequests
*/
public int getAutoServerRankingNumRequests() {
return autoServerLatencyNumRequests;
}
/**
* Sets the maximum number of requests
*
*/
public void setAutoServerRankingNumRequests(int autoServerLatencyNumRequests) {
this.autoServerLatencyNumRequests = autoServerLatencyNumRequests;
}
/**
* Returns the current ranking status
*
* @return rankingStatus
*/
public String getRankingStatus() {
return rankingStatus;
}
/**
* Sets the current ranking status
*
* @param rankingStatus
*/
public void setRankingStatus(String rankingStatus) {
this.rankingStatus = rankingStatus;
}
/**
* Sets the licence key
*
* @param licenceKey
*/
public void setLicenceKey(String licenceKey) {
this.licenceKey = licenceKey;
}
/**
* Gets the list of ranking endpoints (manual or auto)
*
* @return cachedEndPoints
*/
public EndPoint[] getEndPoints() throws CacheException {
EndPoint[] cachedEndPoints;
if (autoServerRanking) {
selfAutoRanking = "y";
List