Search in sources :

Example 26 with ParseException

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);
        }
    }
}
Also used : HttpEntity(org.apache.hc.core5.http.HttpEntity) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) ParseException(org.apache.hc.core5.http.ParseException) ClientProtocolException(org.apache.hc.client5.http.ClientProtocolException) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 27 with ParseException

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!");
    }
}
Also used : Agent(org.wso2.testgrid.common.Agent) TinkererOperationException(org.wso2.testgrid.common.util.tinkerer.exception.TinkererOperationException) Pattern(java.util.regex.Pattern) GsonBuilder(com.google.gson.GsonBuilder) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) Gson(com.google.gson.Gson) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) IOException(java.io.IOException) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) HttpResponse(org.apache.hc.core5.http.HttpResponse) Response(org.apache.hc.client5.http.fluent.Response) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) ParseException(org.apache.hc.core5.http.ParseException) TestGridException(org.wso2.testgrid.common.exception.TestGridException) File(java.io.File)

Example 28 with ParseException

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)));
        }
    }
}
Also used : HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) StringEntity(org.apache.hc.core5.http.io.entity.StringEntity) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 29 with ParseException

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);
        }
    }
}
Also used : Header(org.apache.hc.core5.http.Header) HttpGet(org.apache.hc.client5.http.classic.methods.HttpGet) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse) VersionNumber(org.jenkinsci.remoting.util.VersionNumber) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 30 with ParseException

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;
    }
}
Also used : CloseableHttpClient(org.apache.hc.client5.http.impl.classic.CloseableHttpClient) RequestConfig(org.apache.hc.client5.http.config.RequestConfig) HttpPost(org.apache.hc.client5.http.classic.methods.HttpPost) NameValuePair(org.apache.hc.core5.http.NameValuePair) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) HttpEntity(org.apache.hc.core5.http.HttpEntity) BasicNameValuePair(org.apache.hc.core5.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) CloseableHttpResponse(org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)

Aggregations

ParseException (org.apache.hc.core5.http.ParseException)62 IOException (java.io.IOException)54 SpotifyWebApiException (se.michaelthelin.spotify.exceptions.SpotifyWebApiException)33 CloseableHttpResponse (org.apache.hc.client5.http.impl.classic.CloseableHttpResponse)24 HttpPost (org.apache.hc.client5.http.classic.methods.HttpPost)13 HttpGet (org.apache.hc.client5.http.classic.methods.HttpGet)12 CloseableHttpClient (org.apache.hc.client5.http.impl.classic.CloseableHttpClient)11 HttpEntity (org.apache.hc.core5.http.HttpEntity)10 HashMap (java.util.HashMap)8 Map (java.util.Map)6 StringEntity (org.apache.hc.core5.http.io.entity.StringEntity)6 BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)6 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)5 ArrayList (java.util.ArrayList)5 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)5 Matcher (java.util.regex.Matcher)4 UrlEncodedFormEntity (org.apache.hc.client5.http.entity.UrlEncodedFormEntity)4 AuthorizationCodeCredentials (se.michaelthelin.spotify.model_objects.credentials.AuthorizationCodeCredentials)4 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)3 List (java.util.List)3