use of org.apache.hc.core5.http.ParseException in project skywalking-java by apache.
the class FrontController method get.
@GetMapping("/get")
public String get() throws Exception {
final HttpGet httpget = new HttpGet("http://127.0.0.1:8080/httpclient-5.x/case/asyncGet");
LOGGER.info("Executing request " + httpget.getMethod() + " " + httpget.getUri());
try (final CloseableHttpResponse response = httpClient.execute(httpget)) {
LOGGER.info(response.getCode() + " -> " + response.getReasonPhrase());
final HttpEntity entity = response.getEntity();
try {
return entity != null ? EntityUtils.toString(entity) : "";
} catch (final ParseException ex) {
throw new ClientProtocolException(ex);
}
}
}
use of org.apache.hc.core5.http.ParseException in project testgrid by wso2.
the class UnixClient method downloadLogs.
@Override
public void downloadLogs(DeploymentCreationResult deploymentCreationResult, TestPlan testPlan) throws TinkererOperationException {
String testType = testPlan.getScenarioConfigs().get(0).getTestType();
logger.info("TestType is : " + testType);
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
for (Agent agent : deploymentCreationResult.getAgents()) {
logger.info("Initiating LOG file download for Agent " + agent.getInstanceName() + "\n agent instance ID " + agent.getInstanceId() + "\n test plan " + testPlan.getId());
String productBasePath = "";
String logLocation = "";
String operationPath = this.getTinkererBase() + "test-plan/" + agent.getTestPlanId() + "/agent/" + agent.getInstanceName() + "/operation";
if (TestGridConstants.TEST_TYPE_FUNCTIONAL.equals(testType)) {
// get the carbon.home from the instance where the product is running
String content = "";
try {
Map<String, Object> payload = new HashMap<>();
payload.put("code", "SHELL");
payload.put("request", "ps -ef | grep 'carbon.home'");
Response execute = Request.Post(operationPath).setHeader(HttpHeaders.CONTENT_TYPE, "application/json").setHeader(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString(StringUtil.concatStrings(this.getTinkererUserName(), ":", this.getTinkererPassword()).getBytes(Charset.defaultCharset()))).bodyString(gson.toJson(payload), ContentType.APPLICATION_JSON).execute();
HttpResponse httpResponse = execute.returnResponse();
if (httpResponse.getCode() != HttpStatus.SC_OK) {
throw new TinkererOperationException("Error occurred while performing tinkerer " + "REST api call for retrieving carbon.home. \nError message :" + EntityUtils.toString(((CloseableHttpResponse) httpResponse).getEntity()));
}
content = EntityUtils.toString(((CloseableHttpResponse) httpResponse).getEntity());
} catch (IOException e) {
throw new TinkererOperationException("Error occurred while retrieving the carbon.home value" + "from agent" + agent.getAgentId() + "\nRunning on instance : " + agent.getInstanceName() + "\nfor test plan " + testPlan.getId(), e);
} catch (ParseException e) {
throw new TinkererOperationException("Error occurred while parsing the response " + "wile retrieving carbon.home from agent" + agent.getAgentId() + "\nRunning on instance : " + agent.getInstanceName() + "\nfor test plan " + testPlan.getId(), e);
}
if (content.contains("carbon.home")) {
String patternString = "carbon\\.home=[a-z\\/0-9-.]*";
Pattern pattern = Pattern.compile(patternString);
Matcher matcher = pattern.matcher(content);
if (matcher.find()) {
productBasePath = matcher.group().split("=")[1];
}
}
logLocation = productBasePath + SCENARIO_LOG_LOCATION;
} else if (TestGridConstants.TEST_TYPE_INTEGRATION.equals(testType)) {
if (testPlan.getJobProperties().containsKey(WORKSPACE_DIR_POSIX)) {
productBasePath = testPlan.getJobProperties().getProperty(WORKSPACE_DIR_POSIX);
} else {
throw new TinkererOperationException("Product workspace path is not present, Please check if" + "entry is present in job-config.yml file : " + "\nfor test plan :" + testPlan.getId());
}
/*If the path contains the ~ character we need to replace it with ~{OS-USER} because,
agent executes as the root user, and it will infer ~ as root.
ex: ~ --replace into--> ~centos*/
productBasePath = productBasePath.replace("~", "~" + agent.getInstanceUser());
logLocation = productBasePath + INTEGRATION_LOG_LOCATION;
}
logger.info("Product base path found " + productBasePath);
Map<String, String> resultMap;
try {
Map<String, Object> payload = new HashMap<>();
payload.put("code", "SHELL");
payload.put("request", "ls " + logLocation);
Response logFiles = Request.Post(operationPath).setHeader(HttpHeaders.CONTENT_TYPE, "application/json").setHeader(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString(StringUtil.concatStrings(this.getTinkererUserName(), ":", this.getTinkererPassword()).getBytes(Charset.defaultCharset()))).bodyString(gson.toJson(payload), ContentType.APPLICATION_JSON).execute();
// check if REST api call was successful
HttpResponse response = logFiles.returnResponse();
if (response.getCode() != HttpStatus.SC_OK) {
throw new TinkererOperationException("Error occurred while performing tinkerer " + "REST api call for getting list of log files. \nError message :" + EntityUtils.toString(((CloseableHttpResponse) response).getEntity()));
}
// create a map of json response
resultMap = new Gson().fromJson(EntityUtils.toString(((CloseableHttpResponse) response).getEntity()), new PropertyType().getType());
} catch (IOException e) {
throw new TinkererOperationException("Error occurred while retrieving the list of log files from the" + "log location : " + logLocation + "\nfrom agent :" + agent.getAgentId() + "\nRunning on instance : " + agent.getInstanceName() + "\nfor test plan :" + testPlan.getId(), e);
} catch (ParseException e) {
throw new TinkererOperationException("Error occurred while parsing the list of log files from the" + "log location : " + logLocation + "\nfrom agent :" + agent.getAgentId() + "\nRunning on instance : " + agent.getInstanceName() + "\nfor test plan :" + testPlan.getId(), e);
}
if (resultMap != null && resultMap.containsKey("response")) {
String response = resultMap.get("response");
// TODO: the response can also contain an error log instead of the actual response.
// so, we need to first make sure the command was successful via exit code.
List<String> logFileNames = Arrays.asList(response.split("\n"));
for (String logFileName : logFileNames) {
try {
logger.info("Downloading log file : " + logFileName);
String key = Base64.getEncoder().encodeToString(FileUtils.readFileToByteArray(new File(testPlan.getKeyFileLocation())));
String source = logLocation + logFileName;
String destination = Paths.get(TestGridUtil.deriveLogDownloadLocation(testPlan), logFileName).toString();
String bastianIP = deploymentCreationResult.getBastianIP();
String downloadPath = this.getTinkererBase() + "test-plan/" + agent.getTestPlanId() + "/agent/" + agent.getInstanceName() + "/stream-file";
Map<String, Object> payload = new HashMap<>();
payload.put("code", "STREAM_FILE");
Map<String, Object> subPayload = new HashMap<>();
subPayload.put("key", key);
subPayload.put("source", source);
subPayload.put("destination", destination);
if (bastianIP != null) {
subPayload.put("bastian-ip", bastianIP);
}
payload.put("data", subPayload);
Response execute = Request.Post(downloadPath).setHeader(HttpHeaders.CONTENT_TYPE, "application/json").setHeader(HttpHeaders.AUTHORIZATION, "Basic " + Base64.getEncoder().encodeToString(StringUtil.concatStrings(this.getTinkererUserName(), ":", this.getTinkererPassword()).getBytes(Charset.defaultCharset()))).bodyString(gson.toJson(payload), ContentType.APPLICATION_JSON).execute();
// check if REST api call was successful
HttpResponse httpResponse = execute.returnResponse();
if (httpResponse.getCode() != HttpStatus.SC_OK) {
throw new TinkererOperationException("Error occurred while" + " performing tinkerer REST api call for log download." + "\nError message :" + EntityUtils.toString(((CloseableHttpResponse) httpResponse).getEntity()));
}
// verify the downloaded files are present in the location
if (!verifyFileDownload(destination)) {
throw new TinkererOperationException("Failed to download the file :" + destination);
}
logger.info("Download Location :" + destination);
} catch (IOException e) {
throw new TinkererOperationException("Error occurred while downloading " + "the log file location : " + logFileName + "\nfrom agent :" + agent.getAgentId() + "\nRunning on instance : " + agent.getInstanceName() + "\nfor test plan :" + testPlan.getId(), e);
} catch (ParseException e) {
throw new TinkererOperationException("Error occurred while parsing the " + "response from download " + "logfile operation : " + logFileName + "\nfrom agent :" + agent.getAgentId() + "\nRunning on instance : " + agent.getInstanceName() + "\nfor test plan :" + testPlan.getId(), e);
} catch (TestGridException e) {
throw new TinkererOperationException("Error occurred deriving the destination path " + "for logfile : " + logFileName + "\nfrom agent :" + agent.getAgentId() + "\nRunning on instance : " + agent.getInstanceName() + "\nfor test plan :" + testPlan.getId(), e);
}
}
logger.info("Successfully downloaded all log files ");
}
}
if (deploymentCreationResult.getAgents().size() == 0) {
logger.warn("No registered agents found!");
}
}
use of org.apache.hc.core5.http.ParseException in project swarm-plugin by jenkinsci.
the class SwarmClient method postLabelAppend.
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE", justification = "False positive for try-with-resources in Java 11")
static synchronized void postLabelAppend(String name, String labels, CloseableHttpClient client, HttpClientContext context, URL url) throws IOException, ParseException, RetryException {
HttpPost post = new HttpPost(url + "plugin/swarm/addSlaveLabels?name=" + name + param("labels", labels));
post.addHeader(HttpHeaders.CONNECTION, "close");
// Add an empty body, as without it, some servers return a HTTP 411 response
// due to the lack of a `Content-Length` header on the request
post.setEntity(new StringEntity(""));
Crumb csrfCrumb = getCsrfCrumb(client, context, url);
if (csrfCrumb != null) {
post.addHeader(csrfCrumb.crumbRequestField, csrfCrumb.crumb);
}
try (CloseableHttpResponse response = client.execute(post, context)) {
if (response.getCode() != HttpStatus.SC_OK) {
throw new RetryException(String.format("Failed to update agent labels. Response code: %s%n%s", response.getCode(), EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8)));
}
}
}
use of org.apache.hc.core5.http.ParseException in project swarm-plugin by jenkinsci.
the class SwarmClient method getJenkinsVersion.
/**
* Get the Jenkins version.
*/
@SuppressFBWarnings(value = "RCN_REDUNDANT_NULLCHECK_WOULD_HAVE_BEEN_A_NPE", justification = "False positive for try-with-resources in Java 11")
VersionNumber getJenkinsVersion(CloseableHttpClient client, HttpClientContext context, URL url) throws IOException, ParseException, RetryException {
logger.fine("getJenkinsVersion() invoked");
HttpGet httpGet = new HttpGet(url + "api");
try (CloseableHttpResponse response = client.execute(httpGet, context)) {
if (response.getCode() != HttpStatus.SC_OK) {
throw new RetryException(String.format("Could not get Jenkins version. Response code: %s%n%s", response.getCode(), EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8)));
}
Header[] headers = response.getHeaders("X-Jenkins");
if (headers.length != 1) {
throw new RetryException("This URL doesn't look like Jenkins.");
}
String versionStr = headers[0].getValue();
try {
return new VersionNumber(versionStr);
} catch (RuntimeException e) {
throw new RetryException("Unexpected Jenkins version: " + versionStr, e);
}
}
}
use of org.apache.hc.core5.http.ParseException in project renfeid by renfei.
the class IcpQueryUtil method post.
private String post(String url, String data, String content, String token) throws IOException, ParseException {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
String vIp = "101." + ThreadLocalRandom.current().nextInt(0, 255) + "." + ThreadLocalRandom.current().nextInt(0, 255) + "." + ThreadLocalRandom.current().nextInt(0, 255);
RequestConfig requestConfig = RequestConfig.DEFAULT;
HttpPost httpPost = new HttpPost("https://hlwicpfwc.miit.gov.cn/icpproject_query/api/" + url);
httpPost.setConfig(requestConfig);
httpPost.setHeaders(buildHeaders(content, token, vIp));
HttpEntity httpEntity = null;
if ("application/x-www-form-urlencoded;charset=UTF-8".equals(content)) {
List<NameValuePair> parameters = new ArrayList<>();
for (String par : data.split("&")) {
parameters.add(new BasicNameValuePair(par.split("=")[0], par.split("=")[1]));
}
httpEntity = EntityBuilder.create().setParameters(parameters).build();
} else {
httpEntity = EntityBuilder.create().setText(data).build();
}
httpPost.setEntity(httpEntity);
CloseableHttpResponse response = httpclient.execute(httpPost);
String responseString = EntityUtils.toString(response.getEntity());
if (response.getCode() == 200) {
return responseString;
}
return null;
}
}
Aggregations