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.
/*
* Copyright 2015 MasterCard International.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
* Neither the name of the MasterCard International Incorporated nor the names of its
* contributors may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
package com.mastercard.api.core;
import com.mastercard.api.core.exception.*;
import com.mastercard.api.core.model.*;
import com.mastercard.api.core.security.Authentication;
import com.mastercard.api.core.security.CryptographyInterceptor;
import com.mastercard.api.core.security.HttpRequestCryptographyInterceptor;
import org.apache.http.*;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.*;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ApiController {
private static String USER_AGENT = null; // User agent string sent with requests.
private static String HEADER_SEPARATOR = ";";
public static final String ENVIRONMENT_IDENTIFIER = "#env";
/**
*/
public ApiController() {
}
/**
* Append parameter to URL
*
* @param s instance of StringBuilder
* @param stringToAppend e.g. max=10
* @return StringBuilder
*/
private StringBuilder appendToQueryString(StringBuilder s, String stringToAppend) {
if (s.indexOf("?") == -1) {
s.append("?");
}
if (s.indexOf("?") != s.length() - 1) {
s.append("&");
}
s.append(stringToAppend);
return s;
}
String urlEncode(Object stringToEncode) {
try {
return URLEncoder.encode(stringToEncode.toString(), "UTF-8")
.replace("+", "%20")
.replace("*", "%2A")
.replace("%7E", "~");
} catch (UnsupportedEncodingException e) {
return stringToEncode.toString();
}
}
/**
* This is the method which is used to replace {pathid} in the url with the values in the map.
* Once the value in the map is used, it is removed.
*
* @param url - url where the values need to be replaced.
* @param objectMap - map containing the values which can be replace.
* @return formatted string
*/
String getPathWithReplacedPath(String url, Map objectMap) throws IllegalStateException {
String regexToRemovePathParameters = "\\{(.*?)\\}";
Pattern pattern = Pattern.compile(regexToRemovePathParameters);
Matcher matcher = pattern.matcher(url);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
String group = matcher.group(1);
if (objectMap.containsKey(group)) {
matcher.appendReplacement(sb, objectMap.remove(group).toString());
} else {
throw new IllegalStateException("Error: required parameter='"+group+"' was not found in the RequestMap ");
}
}
matcher.appendTail(sb);
String tmpResult = sb.length() > 0 ? sb.toString().replaceAll("//", "/").replaceAll("/$", "") : url;
//arizzini: need to make sure that the correct format is maintained.
if (!tmpResult.startsWith("/"))
tmpResult = "/"+tmpResult;
return tmpResult;
}
private URI getURI(OperationConfig operationConfig, OperationMetadata operationMetadata, RequestMap requestObject) {
URI uri;
//arizzini: if host config or environment config changes betweeen calls
// we need to update the host
String host = operationMetadata.getHost();
try {
new URL(host);
} catch (MalformedURLException e) {
throw new IllegalStateException("Invalid URL supplied for host="+host, e);
}
String resourcePath = operationConfig.getResourcePath();
if (resourcePath.contains(ENVIRONMENT_IDENTIFIER)) {
String context = "";
if (operationMetadata.getContext() != null) {
context = operationMetadata.getContext();
}
resourcePath = resourcePath.replace(ENVIRONMENT_IDENTIFIER, context);
//don't worry of //, they will be removed in the getPathWithReplacedPath
}
//arizzini: need to replace all the path variables
String updatedType = getPathWithReplacedPath(resourcePath, requestObject);
StringBuilder s = new StringBuilder("%s%s");
List