method = doUseProxy(pRequest) ? HttpPost.METHOD_NAME : HttpGet.METHOD_NAME; String queryParams = prepareQueryParameters(pProcessingOptions); if (doUseProxy(pRequest)) { throw new IllegalArgumentException("Proxy mode can only be used with POST requests"); String base = prepareBaseUrl(j4pServerUrl); StringBuilder requestPath = new StringBuilder(base); requestPath.append(pRequest.getType().getValue()); for (String p : parts) { requestPath.append("/"); requestPath.append(escape(p)); return new HttpGet(createRequestURI(requestPath.toString(),queryParams)); JSONObject requestContent = getJsonRequestContent(pRequest); HttpPost postReq = new HttpPost(createRequestURI(j4pServerUrl.getPath(),queryParams)); postReq.setEntity(new StringEntity(requestContent.toJSONString(),"utf-8")); return postReq;
/** * Get base URL for Jolokia requests * * @return the Jolokia URL */ public URI getUri() { return requestHandler.getJ4pServerUrl(); }
/** * Get an HTTP Request for requesting multiples requests at once * * @param pRequests requests to put into a HTTP request * @return HTTP request to send to the server */ public <T extends J4pRequest> HttpUriRequest getHttpRequest(List<T> pRequests,Map<J4pQueryParameter,String> pProcessingOptions) throws UnsupportedEncodingException, URISyntaxException { JSONArray bulkRequest = new JSONArray(); String queryParams = prepareQueryParameters(pProcessingOptions); HttpPost postReq = new HttpPost(createRequestURI(j4pServerUrl.getPath(),queryParams)); for (T request : pRequests) { JSONObject requestContent = getJsonRequestContent(request); bulkRequest.add(requestContent); } postReq.setEntity(new StringEntity(bulkRequest.toJSONString(),"utf-8")); return postReq; }
HttpResponse response = httpClient.execute(requestHandler.getHttpRequest(pRequest,pMethod,pProcessingOptions)); JSONAware jsonResponse = extractJsonResponse(pRequest,response); if (! (jsonResponse instanceof JSONObject)) {
@SuppressWarnings("PMD.PreserveStackTrace") private <REQ extends J4pRequest> JSONAware extractJsonResponse(REQ pRequest, HttpResponse pResponse) throws J4pException { try { return requestHandler.extractJsonResponse(pResponse); } catch (IOException e) { throw new J4pException("IO-Error while reading the response: " + e,e); } catch (ParseException e) { // It's a parse exception. Now, check whether the HTTResponse is // an error and prepare the proper J4pException StatusLine statusLine = pResponse.getStatusLine(); if (HttpStatus.SC_OK != statusLine.getStatusCode()) { throw new J4pRemoteException(pRequest,statusLine.getReasonPhrase(), null, statusLine.getStatusCode(),null, null); } throw new J4pException("Could not parse answer: " + e,e); } }
/** * Constructor using a given Agent URL, HttpClient and a proxy target config. If the HttpClient is null, * a default client is used. If no target config is given, a plain request is performed * * @param pJ4pServerUrl the agent URL for how to contact the server. * @param pHttpClient HTTP client to use for the connecting to the agent * @param pTargetConfig optional target * @param pExtractor response extractor to use */ public J4pClient(String pJ4pServerUrl, HttpClient pHttpClient,J4pTargetConfig pTargetConfig,J4pResponseExtractor pExtractor) { requestHandler = new J4pRequestHandler(pJ4pServerUrl,pTargetConfig); responseExtractor = pExtractor; // Using the default as defined in the client builder if (pHttpClient != null) { httpClient = pHttpClient; } else { J4pClientBuilder builder = new J4pClientBuilder(); httpClient = builder.createHttpClient(); } }
/** * Execute multiple requests at once. All given request will result in a single HTTP request where it gets * dispatched on the agent side. The results are given back in the same order as the arguments provided. * * @param pRequests requests to execute * @param pProcessingOptions processing options to use * @param pResponseExtractor use this for custom extraction handling * @param <RESP> response type * @param <REQ> request type * @return list of responses, one response for each request * @throws J4pException when an communication error occurs */ public <RESP extends J4pResponse<REQ>, REQ extends J4pRequest> List<RESP> execute(List<REQ> pRequests, Map<J4pQueryParameter,String> pProcessingOptions, J4pResponseExtractor pResponseExtractor) throws J4pException { try { HttpResponse response = httpClient.execute(requestHandler.getHttpRequest(pRequests,pProcessingOptions)); JSONAware jsonResponse = extractJsonResponse(null, response); verifyBulkJsonResponse(jsonResponse); return this.<RESP, REQ>extractResponses(jsonResponse, pRequests, pResponseExtractor); } catch (IOException e) { throw mapException(e); } catch (URISyntaxException e) { throw mapException(e); } }
private J4pException mapException(Exception pException) throws J4pException { if (pException instanceof ConnectException) { return new J4pConnectException( "Cannot connect to " + requestHandler.getJ4pServerUrl() + ": " + pException.getMessage(), (ConnectException) pException); } else if (pException instanceof ConnectTimeoutException) { return new J4pTimeoutException( "Read timeout while request " + requestHandler.getJ4pServerUrl() + ": " + pException.getMessage(), (ConnectTimeoutException) pException); } else if (pException instanceof IOException) { return new J4pException("IO-Error while contacting the server: " + pException,pException); } else if (pException instanceof URISyntaxException) { URISyntaxException sExp = (URISyntaxException) pException; return new J4pException("Invalid URI " + sExp.getInput() + ": " + sExp.getReason(),pException); } else { return new J4pException("Exception " + pException,pException); } }