forked from docs/doc-exports
Reviewed-by: gtema <artem.goncharov@gmail.com> Co-authored-by: Yang, Tong <yangtong2@huawei.com> Co-committed-by: Yang, Tong <yangtong2@huawei.com>
800 lines
36 KiB
HTML
800 lines
36 KiB
HTML
<a name="mrs_02_1081"></a><a name="mrs_02_1081"></a>
|
|
|
|
<h1 class="topictitle1">Examples</h1>
|
|
<div id="body32001227"><p id="mrs_02_1081__en-us_topic_0125376203_p8060118">Invoke API demo code.</p>
|
|
<div class="section" id="mrs_02_1081__en-us_topic_0125376203_section6768164511616"><h4 class="sectiontitle">Main.java</h4><p id="mrs_02_1081__en-us_topic_0125376203_p5690224151716">This class is a running entry class for calling APIs. Before running this class, you need to set the following parameters:</p>
|
|
<ol id="mrs_02_1081__en-us_topic_0125376203_ol0690124141717"><li id="mrs_02_1081__en-us_topic_0125376203_li9690624161713">IP address of the cluster management node</li><li id="mrs_02_1081__en-us_topic_0125376203_li166901124171714">Login username and password</li></ol>
|
|
<p id="mrs_02_1081__en-us_topic_0125376203_p66901424131715">Before calling service APIs, call login authentication APIs for performing login authentication first. For details about how to call login authentication APIs, see <strong id="mrs_02_1081__en-us_topic_0125376203_b84235270692549">CASLogin.java</strong>.</p>
|
|
<p id="mrs_02_1081__en-us_topic_0125376203_p18690524161718">The following example describes how to call the GET and POST APIs.</p>
|
|
<ol id="mrs_02_1081__en-us_topic_0125376203_ol136901524191711"><li id="mrs_02_1081__en-us_topic_0125376203_li13690224131713">GET request API<p id="mrs_02_1081__en-us_topic_0125376203_p20690192411179"><a name="mrs_02_1081__en-us_topic_0125376203_li13690224131713"></a><a name="en-us_topic_0125376203_li13690224131713"></a>This example describes how to call the API for querying the installed clusters (/web/v1/clusters).</p>
|
|
<p id="mrs_02_1081__en-us_topic_0125376203_p16690192413179">Before the service API is called, the IP address, port number, and the <strong id="mrs_02_1081__en-us_topic_0125376203_b84235270693248">/web</strong> path are added to the webUrl. Therefore, you need to combine the webUrl with the request API path <strong id="mrs_02_1081__en-us_topic_0125376203_b84235270693438">/v1/clusters</strong> in the operationUrl of the service API.</p>
|
|
</li><li id="mrs_02_1081__en-us_topic_0125376203_li16690124181717">POST request API<p id="mrs_02_1081__en-us_topic_0125376203_p069032471711"><a name="mrs_02_1081__en-us_topic_0125376203_li16690124181717"></a><a name="en-us_topic_0125376203_li16690124181717"></a>This example describes how to call the API for starting a service (/web/v1/maintain/cluster/{cluster_id}/service/{service_name}/start).</p>
|
|
<p id="mrs_02_1081__en-us_topic_0125376203_p12690142418174">In the operationUrl of this API, you need to combine the webUrl with the request API path <strong id="mrs_02_1081__en-us_topic_0125376203_b84235270695250">v1/maintain/cluster/{cluster_id}/service/{service_name}/start</strong>. In the request path URL, enter the current cluster ID and the name of the service to be started.</p>
|
|
<p id="mrs_02_1081__en-us_topic_0125376203_p66901824171714">According to the requirements of the API for starting a service, specified parameters need to be inputted in the request body. These parameters are written in the example <strong id="mrs_02_1081__en-us_topic_0125376203_b84235270695610">StartService.json</strong> file in JSON format, and the file path is specified in <strong id="mrs_02_1081__en-us_topic_0125376203_b84235270695640">jsonFilePath</strong>.</p>
|
|
</li></ol>
|
|
<p id="mrs_02_1081__en-us_topic_0125376203_p116901124121716">You can view information about API calling in the generated <strong id="mrs_02_1081__en-us_topic_0125376203_b84235270695815">Demo.log</strong> file. For details about log configurations, see <strong id="mrs_02_1081__en-us_topic_0125376203_b84235270695834">Log4j.properties</strong>.</p>
|
|
<pre class="screen" id="mrs_02_1081__en-us_topic_0125376203_screen3692102414171">package com.MRS;
|
|
|
|
import org.apache.http.client.HttpClient;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import com.MRS.login.CASLogin;
|
|
import com.MRS.operation.HttpManager;
|
|
|
|
/**
|
|
* Program running entry
|
|
*
|
|
*/
|
|
public class Main
|
|
{
|
|
private static final Logger LOG = LoggerFactory.getLogger(Main.class);
|
|
|
|
// IP address of the cluster management node
|
|
public static final String IP_Address = "xxx.xxx.xxx.xxx";
|
|
|
|
public static void main(String[] args)
|
|
{
|
|
//############# Step 1. Prepare for calling an API.#############
|
|
// Login username and password
|
|
String userName = "xxxxxx";
|
|
String password = "xxxxxxxxx";
|
|
HttpManager httpManager = new HttpManager();
|
|
|
|
// CAS and Web URLs
|
|
String casUrl = "https://" + IP_Address + ":20009/cas/login";
|
|
String webUrl = "https://" + IP_Address + ":28443/web/";
|
|
|
|
// <strong id="mrs_02_1081__en-us_topic_0125376203_b84235270610037">userTLSVersion</strong> is a mandatory parameter. It is an important parameter used for connecting a JDK 1.6 server to a JDK 1.8 server. If you use JDK 1.8, set this parameter to an empty character string.
|
|
String userTLSVersion = "";
|
|
|
|
//############# Step 2. Call an API to complete login authentication.#############
|
|
LOG.info("Begin to get httpclient and login cas.");
|
|
HttpClient httpClient = new CASLogin().login(casUrl, webUrl, userName, password, userTLSVersion);
|
|
|
|
//############# Step 3. Call APIs based on the service.#############
|
|
|
|
// Send a GET request to call the API for querying the installed clusters.
|
|
String operationUrl = webUrl + "/v1/clusters";
|
|
httpManager.sendHttpGetRequest(httpClient, operationUrl);
|
|
|
|
// Send a POST request to call the API for adding a user.
|
|
String jsonFilePath = "./conf/startService.json";
|
|
String operationUrl = webUrl + "/v1/maintain/cluster/{cluster_id}/service/{service_name}/start";
|
|
httpManager.sendHttpPostRequest(httpClient, operationUrl, jsonFilePath);
|
|
|
|
//############# Step 4. Close the connection.#############
|
|
httpClient.getConnectionManager().shutdown();
|
|
}
|
|
}</pre>
|
|
</div>
|
|
<div class="section" id="mrs_02_1081__en-us_topic_0125376203_section17411934161712"><h4 class="sectiontitle">CASLogin.java</h4><p id="mrs_02_1081__en-us_topic_0125376203_p10597621111816">This class allows login authentication with a token.</p>
|
|
<ol id="mrs_02_1081__en-us_topic_0125376203_ol12597142116188"><li id="mrs_02_1081__en-us_topic_0125376203_li165973212187">Build an HttpClient client object.</li><li id="mrs_02_1081__en-us_topic_0125376203_li859715215181">Invoke the <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706101145">getCasLoginPage()</strong> method to obtain the CAS login page, input the casUrl and the HttpClient client object, and obtain the returned <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706101235">casLoginPageResponse</strong> response object.<p id="mrs_02_1081__en-us_topic_0125376203_p959702181817">The method is to send a GET request to the cluster. The request path is https://<em id="mrs_02_1081__en-us_topic_0125376203_i391636235101329">casUrl</em>/login.</p>
|
|
</li><li id="mrs_02_1081__en-us_topic_0125376203_li13597162151812">The HttpClient obtains the <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706101350">casSessionId</strong> and <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706101355">loginTicket</strong> from the <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706101359">casLoginPageResponse</strong> response object.</li><li id="mrs_02_1081__en-us_topic_0125376203_li12597172113182">Invoke the <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706101412">loginCasServer()</strong> method to obtain the username and password for CAS server authentication, input the casUrl, webUrl, username, password, HttpClient object, casSessionId, loginTicket, and userTLSVersion parameters to obtain the <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706101629">loginPostResponse</strong> response object.<p id="mrs_02_1081__en-us_topic_0125376203_p2059714210184">This method is to send a POST login authentication request to the cluster. The request path is https://<em id="mrs_02_1081__en-us_topic_0125376203_i67642599210171">casUrl</em>/login?service= https://<em id="mrs_02_1081__en-us_topic_0125376203_i135161247810171">webUrl</em>/ cas_security_check.htm.</p>
|
|
</li><li id="mrs_02_1081__en-us_topic_0125376203_li1959712116188">The HttpClient obtains the authenticated TGC from the <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706101751">loginPostResponse</strong> response object.</li><li id="mrs_02_1081__en-us_topic_0125376203_li135978215189">Invoke the <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706101822">webLoginCheck()</strong> method to initiate a web application login verification request, input the webUrl and the HttpClient client object, and obtain the <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706101854">webLoginCheck</strong> response object.<p id="mrs_02_1081__en-us_topic_0125376203_p359792113184">The method is to send a GET verification request to the cluster. The request path is https://<em id="mrs_02_1081__en-us_topic_0125376203_i1775884772101916">webUrl</em>/v1/access/login_check.</p>
|
|
</li><li id="mrs_02_1081__en-us_topic_0125376203_li1559772115183">The HttpClient obtains the JSESSIONID of the web request from the <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706101936">webLoginCheck</strong> response object.</li><li id="mrs_02_1081__en-us_topic_0125376203_li1597122113181">Return an available HttpClient client object.</li></ol>
|
|
<pre class="screen" id="mrs_02_1081__en-us_topic_0125376203_screen7597112117189">package com.MRS.login;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.apache.http.Header;
|
|
import org.apache.http.HttpResponse;
|
|
import org.apache.http.client.HttpClient;
|
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.impl.client.DefaultHttpClient;
|
|
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
|
|
import org.apache.http.message.BasicNameValuePair;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import com.MRS.operation.WebClientWrapper;
|
|
import com.MRS.utils.ParamsValidUtil;
|
|
|
|
public class CASLogin
|
|
{
|
|
private static final Logger LOG = LoggerFactory.getLogger(CASLogin.class);
|
|
private static final String SET_COOKIE = "Set-Cookie";
|
|
private static final String CAS_SESSION_ID_STRING = "casSessionId=";
|
|
private static final String CAS_TGC_STRING = "CASTGC=";
|
|
private static final String SEMICOLON_SEPARATOR = ";";
|
|
private static final String DOUBLE_QUOTAYION = "\"";
|
|
private static final String LOGINTICKET_STRING = "name=\"lt\" value=";
|
|
private static final int LOGINTICKET_INDEX = 5;
|
|
private static final String WRONGPASSWORD = "The credentials you provided cannot be determined to be authentic";
|
|
private static final String RESETPASSWORD = "modify_password.html";
|
|
private static final String ENCODING = "UTF-8";
|
|
public String tgc = "";
|
|
|
|
public HttpClient login(String casUrl, String webUrl, String userName, String password, String userTLSVersion)
|
|
{
|
|
if (ParamsValidUtil.isEmpty(casUrl, webUrl, userName, password))
|
|
{
|
|
LOG.error("Invalid input param.");
|
|
return null;
|
|
}
|
|
if (userTLSVersion == null || userTLSVersion.isEmpty())
|
|
{
|
|
userTLSVersion = "TLS";
|
|
}
|
|
LOG.info("Get http client for sending https request, username is {}, casUrl is {}, webUrl is {}.",
|
|
userName, casUrl, webUrl);
|
|
|
|
// Obtain the HttpClient.
|
|
HttpClient httpClient = getHttpClient(userTLSVersion);
|
|
|
|
// Obtain the CAS login page.
|
|
HttpResponse casLoginPageResponse = getCasLoginPage(casUrl, httpClient);
|
|
LOG.info("get cas login page response is :{}.", casLoginPageResponse);
|
|
|
|
// Obtain <strong id="mrs_02_1081__en-us_topic_0125376203_b84235270684958">casSessionId</strong> and <strong id="mrs_02_1081__en-us_topic_0125376203_b8423527068503">loginTicket</strong>.
|
|
String casSessionId = getCasSessionId(casLoginPageResponse);
|
|
String loginTicket = getLoginTicket(casLoginPageResponse);
|
|
LOG.info("casSessionId = {} , loginTicket = {}.", casSessionId, loginTicket);
|
|
if(StringUtils.isBlank(casSessionId) || StringUtils.isBlank(loginTicket))
|
|
{
|
|
LOG.error("Invalid input param.");
|
|
return null;
|
|
}
|
|
|
|
// Authenticate the username and password on the CAS server.
|
|
HttpResponse loginPostResponse =
|
|
loginCasServer(casUrl, webUrl, userName, password, httpClient, casSessionId, loginTicket, userTLSVersion);
|
|
LOG.info("the login post response is: {}.", loginPostResponse);
|
|
|
|
// Obtain the authenticated TGC.
|
|
String casTgc = getCASTGC(loginPostResponse);
|
|
LOG.info("casTgc = {}.", casTgc);
|
|
|
|
// Check the login.
|
|
HttpResponse webLoginCheck = webLoginCheck(webUrl, httpClient);
|
|
LOG.info("web login check response is: {}.", webLoginCheck);
|
|
|
|
// If the login authentication is successful, the available HttpClient is returned.
|
|
return httpClient;
|
|
}
|
|
|
|
/**
|
|
* Obtain the HttpClient.
|
|
* @return http client
|
|
*/
|
|
@SuppressWarnings("deprecation")
|
|
public HttpClient getHttpClient(String userTLSVersion)
|
|
{
|
|
ThreadSafeClientConnManager safeClientConnManager = new ThreadSafeClientConnManager();
|
|
safeClientConnManager.setMaxTotal(100);
|
|
HttpClient httpclient = WebClientWrapper.wrapClient(new DefaultHttpClient(safeClientConnManager), userTLSVersion);
|
|
return httpclient;
|
|
}
|
|
|
|
/**
|
|
* Obtain the CAS login page.
|
|
* @param casUrl CAS homepage address
|
|
* @param httpclient http client
|
|
* @return response Response of the request
|
|
*/
|
|
private HttpResponse getCasLoginPage(String casUrl, HttpClient httpclient)
|
|
{
|
|
LOG.info("Enter getCasLoginPage, the casUrl is: {}", casUrl);
|
|
HttpGet preLoginHttpGet = new HttpGet(casUrl);
|
|
HttpResponse httpResponse = null;
|
|
try
|
|
{
|
|
httpResponse = httpclient.execute(preLoginHttpGet);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LOG.error("Execute cas login failed.");
|
|
}
|
|
return httpResponse;
|
|
}
|
|
|
|
/**
|
|
* Obtain the CAS session ID from the returned login page response.
|
|
* @param preLoginResponse Response of the login page request
|
|
* @return session id Session ID
|
|
*/
|
|
private String getCasSessionId(HttpResponse preLoginResponse)
|
|
{
|
|
Header resHeader = preLoginResponse.getFirstHeader(SET_COOKIE);
|
|
String setCookie = resHeader == null ? "" : resHeader.getValue();
|
|
String casSessionId =
|
|
setCookie.substring(CAS_SESSION_ID_STRING.length(), setCookie.indexOf(SEMICOLON_SEPARATOR));
|
|
return casSessionId;
|
|
}
|
|
|
|
/**
|
|
* Obtain the login ticket from the returned login page response.
|
|
* @param preLoginResponse Response of the login page request
|
|
* @return loginTicket Login authentication ticket
|
|
*/
|
|
private String getLoginTicket(HttpResponse preLoginResponse)
|
|
{
|
|
BufferedReader bufferedReader = null;
|
|
String loginTicket = "";
|
|
try
|
|
{
|
|
InputStream inputStream = preLoginResponse.getEntity().getContent();
|
|
|
|
// When the http response is not empty, the context attribute is not null.
|
|
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
|
String lineContent = "";
|
|
while (null != lineContent)
|
|
{
|
|
lineContent = bufferedReader.readLine();
|
|
if ((null != lineContent) && (lineContent.contains(LOGINTICKET_STRING)))
|
|
{
|
|
// Delete spaces at the beginning and end of the line.
|
|
lineContent = lineContent.trim();
|
|
// If <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706102529">LOGINTICKET_STRING</strong> (name=\"loginTicket\" value=) exists, split it based on the double quotation marks. The array length cannot be less than 6.
|
|
loginTicket = lineContent.split(DOUBLE_QUOTAYION)[LOGINTICKET_INDEX];
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LOG.error("Get loginTicket failed.");
|
|
}
|
|
finally
|
|
{
|
|
if (bufferedReader != null)
|
|
{
|
|
try
|
|
{
|
|
bufferedReader.close();
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
LOG.warn("Close buffer reader failed.");
|
|
}
|
|
}
|
|
}
|
|
return loginTicket;
|
|
}
|
|
|
|
/**
|
|
* Obtain the TGC from the response of the request for logging in to the CAS Server.
|
|
* @param loginPostResponse Response object
|
|
* @return TGC Authenticated ticket
|
|
*/
|
|
private String getCASTGC(HttpResponse loginPostResponse)
|
|
{
|
|
Header header = loginPostResponse.getLastHeader(SET_COOKIE);
|
|
String casTgcHeader = header == null ? "" : header.getValue();
|
|
String tempCasTgc = casTgcHeader.split(SEMICOLON_SEPARATOR)[0];
|
|
if (StringUtils.isEmpty(tempCasTgc))
|
|
{
|
|
return "";
|
|
}
|
|
String casTgc = tempCasTgc.substring(CAS_TGC_STRING.length());
|
|
this.tgc = casTgc;
|
|
return casTgc;
|
|
}
|
|
|
|
/**
|
|
* Log in to the CAS server and verify user information.
|
|
* @param casUrl CAS server address
|
|
* @param webUrl Web application address
|
|
* @param userName Login username
|
|
* @param password Login password
|
|
* @param casSessionId CAS session ID
|
|
* @param loginTicket CAS login ticket
|
|
* @return http client HTTP client
|
|
*/
|
|
private HttpResponse loginCasServer(String casUrl, String webUrl, String userName, String password,
|
|
HttpClient httpClient, String casSessionId, String loginTicket,String userTLSVersion)
|
|
{
|
|
if (ParamsValidUtil.isEmpty(casUrl, webUrl, userName, password, casSessionId, loginTicket))
|
|
{
|
|
LOG.error("Invalid input param.");
|
|
return null;
|
|
}
|
|
String postUrl = generateCasLoginUrl(casUrl, webUrl);
|
|
LOG.info("login cas server URL is : {}.", postUrl);
|
|
|
|
HttpPost httpPost = new HttpPost(postUrl);
|
|
List<BasicNameValuePair> FormData = new ArrayList<BasicNameValuePair>();
|
|
FormData.add(new BasicNameValuePair("username", userName));
|
|
FormData.add(new BasicNameValuePair("password", password));
|
|
FormData.add(new BasicNameValuePair("lt", loginTicket));
|
|
FormData.add(new BasicNameValuePair("_eventId", "submit"));
|
|
FormData.add(new BasicNameValuePair("submit", "Login"));
|
|
HttpResponse response = null;
|
|
BufferedReader bufferedReader = null;
|
|
httpPost.addHeader("Cookie", CAS_SESSION_ID_STRING + casSessionId);
|
|
|
|
try
|
|
{
|
|
httpPost.setEntity(new UrlEncodedFormEntity(FormData, ENCODING));
|
|
response = httpClient.execute(httpPost);
|
|
LOG.info("Login CasServer status is {}", response.getStatusLine());
|
|
|
|
InputStream inputStream = response.getEntity().getContent();
|
|
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
|
String lineContent = "";
|
|
lineContent = bufferedReader.readLine();
|
|
|
|
// Performs corresponding operations based on the message returned. Currently, only two types of results are returned on the CAS login page in Java mode.
|
|
while (lineContent != null)
|
|
{
|
|
// Incorrect username or password
|
|
if (lineContent.contains(WRONGPASSWORD))
|
|
{
|
|
LOG.error("The username or password is wrong");
|
|
}
|
|
// Reset the password upon the first login.
|
|
if (lineContent.contains(RESETPASSWORD))
|
|
{
|
|
LOG.warn("Login first time,please reset password.");
|
|
}
|
|
lineContent = bufferedReader.readLine();
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LOG.error("Login cas server failed.");
|
|
}
|
|
finally
|
|
{
|
|
if (bufferedReader != null)
|
|
{
|
|
try
|
|
{
|
|
bufferedReader.close();
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
LOG.error("Login cas server failed because of IOException.");
|
|
}
|
|
}
|
|
}
|
|
return response;
|
|
}
|
|
|
|
/**
|
|
* Generate a complete URL for logging in to CAS.
|
|
* After the login is successful, the specified CAS security check page is displayed.
|
|
* @param casUrl
|
|
* @param webUrl
|
|
* @return
|
|
*/
|
|
private String generateCasLoginUrl(String casUrl, String webUrl)
|
|
{
|
|
StringBuilder sb = new StringBuilder();
|
|
sb.append(casUrl);
|
|
sb.append("?service=");
|
|
sb.append(webUrl);
|
|
sb.append("/cas_security_check.htm");
|
|
return sb.toString();
|
|
}
|
|
|
|
/**
|
|
* Initiate a request for web application login verification.
|
|
* @param webUrl Web application URL
|
|
* @param httpclient http client
|
|
* @return response Login verification response object
|
|
*/
|
|
private HttpResponse webLoginCheck(String webUrl, HttpClient httpclient)
|
|
{
|
|
// Web login authentication request path
|
|
HttpGet loginCheckHttpGet = new HttpGet(webUrl + "/v1/access/login_check");
|
|
LOG.info("web login check URL is: {}.", (webUrl + "/v1/access/login_check "));
|
|
|
|
HttpResponse response = null;
|
|
BufferedReader bufferedReader = null;
|
|
InputStream inputStream = null;
|
|
try
|
|
{
|
|
response = httpclient.execute(loginCheckHttpGet);
|
|
inputStream = response.getEntity().getContent();
|
|
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
|
String lineContent = "";
|
|
lineContent = bufferedReader.readLine();
|
|
LOG.info("response content is {} : " + lineContent);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LOG.error("Get loginTicket failed.");
|
|
}
|
|
finally
|
|
{
|
|
if (bufferedReader != null)
|
|
{
|
|
try
|
|
{
|
|
bufferedReader.close();
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
LOG.warn("Close bufferedReader failed.");
|
|
}
|
|
}
|
|
if (inputStream != null)
|
|
{
|
|
try
|
|
{
|
|
inputStream.close();
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
LOG.warn("Close inputStream failed.");
|
|
}
|
|
}
|
|
}
|
|
return response;
|
|
}
|
|
}</pre>
|
|
</div>
|
|
<div class="section" id="mrs_02_1081__en-us_topic_0125376203_section1740919566200"><h4 class="sectiontitle">HttpManager.java</h4><p id="mrs_02_1081__en-us_topic_0125376203_p9180203642112">This class provides specific GET and POST request methods.</p>
|
|
<pre class="screen" id="mrs_02_1081__en-us_topic_0125376203_screen0180113610217">package com.MRS.operation;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.File;
|
|
import java.io.FileReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
import org.apache.http.HttpResponse;
|
|
import org.apache.http.client.HttpClient;
|
|
import org.apache.http.client.methods.HttpDelete;
|
|
import org.apache.http.client.methods.HttpGet;
|
|
import org.apache.http.client.methods.HttpPost;
|
|
import org.apache.http.entity.StringEntity;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
public class HttpManager
|
|
{
|
|
private static final String ENCODING = "UTF-8";
|
|
private static final Logger LOG = LoggerFactory.getLogger(HttpManager.class);
|
|
|
|
/**
|
|
* Send an httpGet request based on the input parameters such as <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706103534">httpclient</strong> and <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706103540">url</strong>.
|
|
* @param httpclient
|
|
* @param operationUrl
|
|
*/
|
|
public String sendHttpGetRequest(HttpClient httpClient, String operationUrl)
|
|
{
|
|
if (operationUrl == null || operationUrl.isEmpty())
|
|
{
|
|
LOG.error("The operationUrl is emptey.");
|
|
return null;
|
|
}
|
|
LOG.info("The operationUrl is:{}", operationUrl);
|
|
|
|
BufferedReader bufferedReader = null;
|
|
InputStream inputStream = null;
|
|
StringBuffer lineContent = new StringBuffer();
|
|
try
|
|
{
|
|
HttpGet httpGet = new HttpGet(operationUrl);
|
|
HttpResponse rsp = httpClient.execute(httpGet);
|
|
LOG.info("Operation status is {}.", rsp.getStatusLine());
|
|
inputStream = rsp.getEntity().getContent();
|
|
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
|
|
|
String temp = bufferedReader.readLine();
|
|
while (temp != null)
|
|
{
|
|
lineContent.append(temp);
|
|
temp = bufferedReader.readLine();
|
|
}
|
|
bufferedReader.close();
|
|
inputStream.close();
|
|
LOG.info("The get response lineContent is {}.", lineContent);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LOG.info("Send http get request field..");
|
|
}
|
|
finally
|
|
{
|
|
if (bufferedReader != null)
|
|
{
|
|
try
|
|
{
|
|
bufferedReader.close();
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
LOG.info("Close bufferedReader failed.");
|
|
}
|
|
}
|
|
if (inputStream != null)
|
|
{
|
|
try
|
|
{
|
|
inputStream.close();
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
LOG.info("Close inputStream failed.");
|
|
}
|
|
}
|
|
}
|
|
LOG.info("SendHttpGetMessage completely.");
|
|
return lineContent.toString();
|
|
}
|
|
|
|
/**
|
|
* Send an httpPost request based on the input parameters such as <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706103623">httpclient</strong>, <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706103626">url</strong>, and <strong id="mrs_02_1081__en-us_topic_0125376203_b842352706103631">jsonFilePath</strong>.
|
|
* @param httpclient
|
|
* @param operationUrl
|
|
* @param jsonFilePath
|
|
* @return Return the operation command ID.
|
|
*/
|
|
public void sendHttpPostRequest(HttpClient httpClient, String operationUrl, String jsonFilePath)
|
|
{
|
|
if (jsonFilePath == null || jsonFilePath.isEmpty())
|
|
{
|
|
LOG.error("The jsonFilePath is emptey.");
|
|
}
|
|
|
|
String filePath = jsonFilePath;
|
|
File jsonFile = null;
|
|
BufferedReader br = null;
|
|
InputStream inputStream = null;
|
|
BufferedReader bufferedReader = null;
|
|
try
|
|
{
|
|
jsonFile = new File(filePath);
|
|
List<String> list = new LinkedList<String>();
|
|
br = new BufferedReader(new FileReader(jsonFile));
|
|
String temp = br.readLine();
|
|
// Add the request body in the JSON file to the list. Each line is a complete request. A request may be a line or multiple lines. Multiple HTTP requests need to be sent for multiple lines.
|
|
while (temp != null)
|
|
{
|
|
list.add(temp);
|
|
temp = br.readLine();
|
|
}
|
|
br.close();
|
|
|
|
for (int line = 0; line < list.size(); line++)
|
|
{
|
|
String tempString = list.get(line);
|
|
String json = tempString;
|
|
|
|
if (json == null || json.length() == 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
HttpResponse response = null;
|
|
// Build a request header and a request body.
|
|
HttpPost httpPost = new HttpPost(operationUrl);
|
|
httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
|
|
httpPost.setEntity(new StringEntity(json, "text/plain", ENCODING));
|
|
// Send a POST request.
|
|
response = httpClient.execute(httpPost);
|
|
|
|
String status = response.getStatusLine().toString();
|
|
LOG.info("The status is :{} ", status);
|
|
|
|
inputStream = response.getEntity().getContent();
|
|
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
|
|
String lineContent = "";
|
|
lineContent = bufferedReader.readLine();
|
|
LOG.info("the post response lineContent is {}.", lineContent);
|
|
|
|
bufferedReader.close();
|
|
inputStream.close();
|
|
LOG.info("sendHttpPostRequest completely.");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
LOG.error("Send http post request field.");
|
|
}
|
|
finally
|
|
{
|
|
if (bufferedReader != null)
|
|
{
|
|
try
|
|
{
|
|
bufferedReader.close();
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
LOG.info("Close bufferedReader failed.");
|
|
}
|
|
}
|
|
if (inputStream != null)
|
|
{
|
|
try
|
|
{
|
|
inputStream.close();
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
LOG.info("Close inputStream failed.");
|
|
}
|
|
}
|
|
if (br != null)
|
|
{
|
|
try
|
|
{
|
|
br.close();
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
LOG.info("Close bufferedReader failed.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}</pre>
|
|
</div>
|
|
<div class="section" id="mrs_02_1081__en-us_topic_0125376203_section1238084219215"><h4 class="sectiontitle">MRSSSLSocketFactory.java</h4><pre class="screen" id="mrs_02_1081__en-us_topic_0125376203_screen1643716712212">package com.MRS.operation;
|
|
|
|
import java.io.IOException;
|
|
import java.net.Socket;
|
|
import javax.net.ssl.SSLContext;
|
|
import javax.net.ssl.SSLSocket;
|
|
import org.apache.http.conn.ssl.SSLSocketFactory;
|
|
import org.apache.http.conn.ssl.X509HostnameVerifier;
|
|
import org.apache.http.params.HttpParams;
|
|
|
|
/**
|
|
* Make the TLS version set by the user take effect.
|
|
* Inherit SSLSocketFactory.
|
|
*/
|
|
public class MRSSSLSocketFactory extends SSLSocketFactory
|
|
{
|
|
private static String[] enabelPro = {"TLS"};
|
|
|
|
public MRSSSLSocketFactory(SSLContext sslContext, X509HostnameVerifier hostnameVerifier,String userTLSVersion)
|
|
{
|
|
super(sslContext, hostnameVerifier);
|
|
enabelPro[0] = userTLSVersion;
|
|
}
|
|
|
|
@Override
|
|
public Socket createSocket(HttpParams params)
|
|
throws IOException
|
|
{
|
|
Socket result = super.createSocket(params);
|
|
((SSLSocket)result).setEnabledProtocols(enabelPro);
|
|
return result;
|
|
}
|
|
}</pre>
|
|
</div>
|
|
<div class="section" id="mrs_02_1081__en-us_topic_0125376203_section286319244232"><h4 class="sectiontitle">WebClientWrapper.java</h4><pre class="screen" id="mrs_02_1081__en-us_topic_0125376203_screen91941251242">package com.MRS.operation;
|
|
|
|
import java.security.KeyManagementException;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.SecureRandom;
|
|
import java.security.cert.CertificateException;
|
|
import java.security.cert.X509Certificate;
|
|
import javax.net.ssl.SSLContext;
|
|
import javax.net.ssl.TrustManager;
|
|
import javax.net.ssl.X509TrustManager;
|
|
import org.apache.http.client.HttpClient;
|
|
import org.apache.http.conn.ClientConnectionManager;
|
|
import org.apache.http.conn.scheme.Scheme;
|
|
import org.apache.http.conn.scheme.SchemeRegistry;
|
|
import org.apache.http.conn.ssl.SSLSocketFactory;
|
|
import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
|
public class WebClientWrapper
|
|
{
|
|
/**
|
|
* Extend the HttpClient class to automatically accept all certificates.
|
|
* @param base
|
|
* @param userTLSVersion
|
|
* @return
|
|
*/
|
|
public static HttpClient wrapClient(HttpClient baseHttpClient, String userTLSVersion)
|
|
{
|
|
ClientConnectionManager clientConnectionManager;
|
|
SSLContext sslContext = null;
|
|
try {
|
|
sslContext = SSLContext.getInstance(userTLSVersion);
|
|
}
|
|
catch (NoSuchAlgorithmException e1)
|
|
{
|
|
e1.printStackTrace();
|
|
}
|
|
|
|
if (sslContext == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
X509TrustManager tm = new X509TrustManager()
|
|
{
|
|
public X509Certificate[] getAcceptedIssuers()
|
|
{
|
|
return null;
|
|
}
|
|
|
|
public void checkClientTrusted(X509Certificate ax509certificate[], String s) throws CertificateException
|
|
{
|
|
}
|
|
|
|
public void checkServerTrusted(X509Certificate ax509certificate[], String s) throws CertificateException
|
|
{
|
|
}
|
|
};
|
|
|
|
try
|
|
{
|
|
sslContext.init(null, new TrustManager[] {tm}, new SecureRandom());
|
|
}
|
|
catch (KeyManagementException e)
|
|
{
|
|
e.printStackTrace();
|
|
}
|
|
|
|
SSLSocketFactory sslSocketFactory = null;
|
|
if(userTLSVersion == null || userTLSVersion.isEmpty() || userTLSVersion.equals("TLS"))
|
|
{
|
|
sslSocketFactory = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
|
}
|
|
else
|
|
{
|
|
sslSocketFactory = new MRSSSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER,userTLSVersion);
|
|
}
|
|
|
|
clientConnectionManager = baseHttpClient.getConnectionManager();
|
|
SchemeRegistry schemeRegistry = clientConnectionManager.getSchemeRegistry();
|
|
schemeRegistry.register(new Scheme("https", 443, sslSocketFactory));
|
|
return new DefaultHttpClient(clientConnectionManager, baseHttpClient.getParams());
|
|
}
|
|
}</pre>
|
|
</div>
|
|
<div class="section" id="mrs_02_1081__en-us_topic_0125376203_section1828116520348"><h4 class="sectiontitle">ParamsValidUtil.java</h4><pre class="screen" id="mrs_02_1081__en-us_topic_0125376203_screen123920149356">package com.MRS.utils;
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
public class ParamsValidUtil {
|
|
/**
|
|
* Check whether there are null parameters.
|
|
* @param obj Input parameter set
|
|
* @return Whether there are null parameters.
|
|
*/
|
|
public static boolean isEmpty(String... obj)
|
|
{
|
|
for (int i = 0; i < obj.length; i++)
|
|
{
|
|
if (StringUtils.isEmpty(obj[i]))
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}</pre>
|
|
</div>
|
|
<div class="section" id="mrs_02_1081__en-us_topic_0125376203_section104461019183520"><h4 class="sectiontitle">S<strong id="mrs_02_1081__en-us_topic_0125376203_b1129813563517">tartService.json</strong></h4><pre class="screen" id="mrs_02_1081__en-us_topic_0125376203_screen3298183512353">{"only_self":true}</pre>
|
|
</div>
|
|
<div class="section" id="mrs_02_1081__en-us_topic_0125376203_section15230656113510"><h4 class="sectiontitle">L<strong id="mrs_02_1081__en-us_topic_0125376203_b1074817183616">og4j.properties</strong></h4><pre class="screen" id="mrs_02_1081__en-us_topic_0125376203_screen980132443615">##set log4j DEBUG < INFO < WARN < ERROR < FATAL
|
|
log4j.logger.com.MRS=INFO,A1,A2_plus
|
|
log4j.logger.org.apache.http=INFO,A1,A2_plus
|
|
#print to the console?A1
|
|
log4j.appender.A1=org.apache.log4j.ConsoleAppender
|
|
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
|
|
log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] %m %l%n
|
|
|
|
#A2_plus the size of the rest.log < 10M, delete cyclicly
|
|
log4j.appender.A2_plus=org.wcc.framework.log.SizeRollingZipFileATimeAppender
|
|
log4j.appender.A2_plus.File=./conf/Demo.log
|
|
log4j.appender.A2_plus.MaxFileSize=10MB
|
|
log4j.appender.A2_plus.MaxBackupIndex=20
|
|
log4j.appender.A2_plus.layout=org.apache.log4j.PatternLayout
|
|
log4j.appender.A2_plus.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %-5p [%t] %m %l%n</pre>
|
|
</div>
|
|
</div>
|
|
<div>
|
|
<div class="familylinks">
|
|
<div class="parentlink"><strong>Parent topic:</strong> <a href="mrs_02_1018.html">API Usage Guide</a></div>
|
|
</div>
|
|
</div>
|
|
|