List of usage examples for org.apache.http.client ClientProtocolException getStackTrace
public StackTraceElement[] getStackTrace()
From source file:de.magicclock.helper.RequestTask.java
@Override protected String doInBackground(String... uri) { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response;/* w ww. j a v a 2 s. co m*/ String responseString = null; try { response = httpclient.execute(new HttpGet(uri[0])); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpStatus.SC_OK) { ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); responseString = out.toString(); } else { //Closes the connection. response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } } catch (ClientProtocolException e) { responseString = "ERROR ClientProtocolException\n" + e.getStackTrace(); } catch (IOException e) { responseString = "ERROR IOException\n" + e.getStackTrace(); } return responseString; }
From source file:org.openhmis.oauth2.OAuth2Utils.java
/** * // w w w .jav a 2s .c o m * @param oauthDetails * @return * @throws IOException * @throws ClientProtocolException */ public String getAccessToken(OAuth2Details oauthDetails) { HttpPost post = new HttpPost(oauthDetails.getAuthenticationServerUrl()); String clientId = oauthDetails.getClientId(); String clientSecret = oauthDetails.getClientSecret(); String scope = oauthDetails.getScope(); List<BasicNameValuePair> parametersBody = new ArrayList<BasicNameValuePair>(); parametersBody.add(new BasicNameValuePair(OAuthConstants.GRANT_TYPE, oauthDetails.getGrantType())); parametersBody.add(new BasicNameValuePair(OAuthConstants.USERNAME, oauthDetails.getUsername())); parametersBody.add(new BasicNameValuePair(OAuthConstants.PASSWORD, oauthDetails.getPassword())); if (isValid(clientId)) { parametersBody.add(new BasicNameValuePair(OAuthConstants.CLIENT_ID, oauthDetails.getClientId())); } if (isValid(clientSecret)) { parametersBody .add(new BasicNameValuePair(OAuthConstants.CLIENT_SECRET, oauthDetails.getClientSecret())); } if (isValid(scope)) { parametersBody.add(new BasicNameValuePair(OAuthConstants.SCOPE, oauthDetails.getScope())); } DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse response = null; String accessToken = null; try { post.setEntity(new UrlEncodedFormEntity(parametersBody, HTTP.UTF_8)); response = httpClient.execute(post); int code = response.getStatusLine().getStatusCode(); if (code >= 400) { log.debug("Authorization server expects Basic authentication"); post.addHeader(OAuthConstants.AUTHORIZATION, getBasicAuthorizationHeader(oauthDetails.getUsername(), oauthDetails.getPassword())); post.releaseConnection(); response = httpClient.execute(post); code = response.getStatusLine().getStatusCode(); if (code >= 400) { log.debug("Retry with client credentials"); post.removeHeaders(OAuthConstants.AUTHORIZATION); post.addHeader(OAuthConstants.AUTHORIZATION, getBasicAuthorizationHeader( oauthDetails.getClientId(), oauthDetails.getClientSecret())); post.releaseConnection(); response = httpClient.execute(post); if (code >= 400) { throw new RuntimeException( "Could not retrieve access token for user: " + oauthDetails.getUsername()); } } } Map<String, String> map = handleResponse(response); accessToken = map.get(OAuthConstants.ACCESS_TOKEN); } catch (ClientProtocolException cpe) { cpe.getStackTrace(); // will remove after testing log.debug(cpe.getMessage()); } catch (IOException ioe) { ioe.getStackTrace(); // will remove after testing log.debug(ioe.getMessage()); } return accessToken; }