private ClientException newUnmarshalException(Class<?> clazz, String xmlContent, Exception e) { return new ClientException("SDK.UnmarshalFailed", "unmarshal response from xml content failed, clazz = " + clazz.getSimpleName() + ", origin response = " + xmlContent, e); } }
private ClientException newUnmarshalException(Class<?> clazz, String content, Exception e) { return new ClientException("SDK.UnmarshalFailed", "unmarshal response from json content failed, clazz = " + clazz.getSimpleName() + ", origin response = " + content, e); } }
public String getHttpContentString() throws ClientException { String stringContent = ""; if (this.httpContent != null) { try { if (this.encoding == null) { stringContent = new String(this.httpContent); } else { stringContent = new String(this.httpContent, this.encoding); } } catch (UnsupportedEncodingException exp) { throw new ClientException("SDK.UnsupportedEncoding", "Can not parse response due to unsupported encoding."); } } return stringContent; }
@Override public Credentials fetch(int retryTimes) throws ClientException { for (int i = 0; i <= retryTimes; i++) { try { return fetch(); } catch (ClientException e) { if (i == retryTimes) { throw e; } } } throw new ClientException("Failed to connect ECS Metadata Service: Max retry times exceeded."); }
private void checkProductCode(ResolveEndpointRequest request) throws ClientException { boolean productCodeValid = false; for (EndpointResolverBase resolver : endpointResolvers) { if (resolver.isProductCodeValid(request)) { productCodeValid = true; } } if (!productCodeValid) { throw new ClientException(ErrorCodeConstant.SDK_ENDPOINT_RESOLVING_ERROR, String.format(ErrorMessageConstant.ENDPOINT_NO_PRODUCT, request.productCode)); } }
private void checkRegionId(ResolveEndpointRequest request) throws ClientException { boolean regionIdValid = false; for (EndpointResolverBase resolver : endpointResolvers) { if (resolver.isRegionIdValid(request)) { regionIdValid = true; } } if (!regionIdValid) { throw new ClientException(ErrorCodeConstant.SDK_ENDPOINT_RESOLVING_ERROR, String.format(ErrorMessageConstant.INVALID_REGION_ID, request.regionId)); } }
public InstanceProfileCredentials fetch(int retryTimes) throws ClientException { for (int i = 0; i <= retryTimes; i++) { try { return fetch(); } catch (ClientException e) { if (i == retryTimes) { throw e; } } } throw new ClientException("Failed to connect ECS Metadata Service: Max retry times exceeded."); } }
@Override public Credentials fetch(int retryTimes) throws ClientException { for (int i = 0; i <= retryTimes; i++) { try { return fetch(); } catch (ClientException e) { if (i == retryTimes) { throw e; } } } throw new ClientException("Failed to connect ECS Metadata Service: Max retry times exceeded."); }
public InstanceProfileCredentials fetch() throws ClientException { String jsonContent = getMetadata(); JsonObject jsonObject = null; jsonObject = new JsonParser().parse(jsonContent).getAsJsonObject(); if (jsonObject.has("Code") && jsonObject.has("AccessKeyId") && jsonObject.has("AccessKeySecret") && jsonObject .has("SecurityToken") && jsonObject.has("Expiration")) { } else { throw new ClientException("Invalid json got from ECS Metadata service."); } if (!"Success".equals(jsonObject.get("Code").getAsString())) { throw new ClientException(ECS_METADAT_FETCH_ERROR_MSG); } return new InstanceProfileCredentials(jsonObject.get("AccessKeyId").getAsString(), jsonObject.get( "AccessKeySecret").getAsString(), jsonObject.get("SecurityToken").getAsString(), jsonObject.get( "Expiration").getAsString(), DEFAULT_ECS_SESSION_TOKEN_DURATION_SECONDS); }
public Map<String, String> read(String response,String endpoint) throws ClientException{ Element root; try { root = XmlUtils.getRootElementFromString(response); read(root, endpoint, false); } catch (ParserConfigurationException e) { new ClientException("SDK.InvalidXMLParser", e.toString()); } catch (SAXException e) { new ClientException("SDK.InvalidXMLFormat", e.toString()); } catch (IOException e) { new ClientException("SDK.InvalidContent", e.toString()); } return map; }
@Override public Map<String, String> read(String response, String endpoint) throws ClientException { Element root; try { root = XmlUtils.getRootElementFromString(response); read(root, endpoint, false); } catch (ParserConfigurationException e) { new ClientException("SDK.InvalidXMLParser", e.toString()); } catch (SAXException e) { new ClientException("SDK.InvalidXMLFormat", e.toString()); } catch (IOException e) { new ClientException("SDK.InvalidContent", e.toString()); } return map; }
private String getResponseContent(HttpResponse httpResponse) throws ClientException { String stringContent = null; try { if (null == httpResponse.getEncoding()) { stringContent = new String(httpResponse.getContent()); } else { stringContent = new String(httpResponse.getContent(), httpResponse.getEncoding()); } } catch (UnsupportedEncodingException exp) { throw new ClientException("SDK.UnsupportedEncoding", "Can not parse response due to un supported encoding."); } return stringContent; }
private String getResponseContent(HttpResponse httpResponse) throws ClientException { String stringContent = null; try { if (null == httpResponse.getEncoding()) { stringContent = new String(httpResponse.getContent()); } else { stringContent = new String(httpResponse.getContent(), httpResponse.getEncoding()); } } catch (UnsupportedEncodingException exp) { throw new ClientException("SDK.UnsupportedEncoding", "Can not parse response due to un supported encoding."); } return stringContent; } }
public String getMetadata() throws ClientException { HttpRequest request = new HttpRequest(credentialUrl.toString()); request.setSysMethod(MethodType.GET); request.setSysConnectTimeout(connectionTimeoutInMilliseconds); request.setSysReadTimeout(connectionTimeoutInMilliseconds); HttpResponse response; try { response = CompatibleUrlConnClient.compatibleGetResponse(request); } catch (Exception e) { throw new ClientException("Failed to connect ECS Metadata Service: " + e.toString()); } if (response.getStatus() != HttpURLConnection.HTTP_OK) { throw new ClientException(ECS_METADAT_FETCH_ERROR_MSG + " HttpCode=" + response.getStatus()); } return new String(response.getHttpContent()); }
private <T extends AcsResponse> T readResponse(Class<T> clasz, HttpResponse httpResponse, FormatType format) throws ClientException { Reader reader = ReaderFactory.createInstance(format); UnmarshallerContext context = new UnmarshallerContext(); T response = null; String stringContent = getResponseContent(httpResponse); try { response = clasz.newInstance(); } catch (Exception e) { throw new ClientException("SDK.InvalidResponseClass", "Unable to allocate " + clasz.getName() + " class"); } String responseEndpoint = clasz.getName().substring(clasz.getName().lastIndexOf(".") + 1); context.setResponseMap(reader.read(stringContent, responseEndpoint)); context.setHttpResponse(httpResponse); response.getInstance(context); return response; }
@Override public String resolve(ResolveEndpointRequest request) throws ClientException { for (EndpointResolverBase resolver : endpointResolvers) { String endpoint = resolver.resolve(request); if (endpoint != null) { return endpoint; } } checkProductCode(request); checkRegionId(request); throw new ClientException(ErrorCodeConstant.SDK_ENDPOINT_RESOLVING_ERROR, String.format(ErrorMessageConstant.ENDPOINT_NO_REGION, request.regionId, request.productCode, getAvailableRegionsHint(request.productCode))); } }
@Override public CommonResponse getCommonResponse(CommonRequest request) throws ServerException, ClientException { HttpResponse baseResponse = this.doAction(request); String data; try { data = new String(baseResponse.getContent(), "utf-8"); } catch (UnsupportedEncodingException e) { throw new ClientException("SDK.CommonResponseEncodingError", "CommonResponse Encoding UnsupportedEncodingException."); } CommonResponse response = new CommonResponse(); response.setData(data); response.setHttpResponse(baseResponse); return response; }
@Override public Credentials fetch() throws ClientException { URL url = buildUrl(); HttpRequest request = new HttpRequest(url.toString()); request.setMethod(MethodType.GET); request.setConnectTimeout(AuthUtils.DEFAULT_HTTP_SOCKET_TIMEOUT_IN_MILLISECONDS); request.setReadTimeout(AuthUtils.DEFAULT_HTTP_SOCKET_TIMEOUT_IN_MILLISECONDS); HttpResponse response = null; try { response = send(request); } catch (IOException e) { throw new ClientException("CredentialsFetcher.fetch exception: " + e); } return parse(response); }
@Override public Credentials fetch() throws ClientException { URL url = buildUrl(); HttpRequest request = new HttpRequest(url.toString()); request.setMethod(MethodType.GET); request.setConnectTimeout(AuthUtils.DEFAULT_HTTP_SOCKET_TIMEOUT_IN_MILLISECONDS); request.setReadTimeout(AuthUtils.DEFAULT_HTTP_SOCKET_TIMEOUT_IN_MILLISECONDS); HttpResponse response = null; try { response = send(request); } catch (IOException e) { throw new ClientException("CredentialsFetcher.fetch exception: " + e); } return parse(response); }
private <T extends AcsResponse> T parseAcsResponse(Class<T> clasz, HttpResponse baseResponse) throws ServerException, ClientException { FormatType format = baseResponse.getContentType(); if (baseResponse.isSuccess()) { return readResponse(clasz, baseResponse, format); } else { AcsError error = readError(baseResponse, format); if (500 <= baseResponse.getStatus()) { throw new ServerException(error.getErrorCode(), error.getErrorMessage(), error.getRequestId()); } else { throw new ClientException(error.getErrorCode(), error.getErrorMessage(), error.getRequestId()); } } }