Example usage for java.io UnsupportedEncodingException toString

List of usage examples for java.io UnsupportedEncodingException toString

Introduction

In this page you can find the example usage for java.io UnsupportedEncodingException toString.

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:no.ntnu.wifimanager.ServerUtilities.java

public static JSONArray getJSONArray(String url, String userId) {

    InputStream is = null;//from w  w w  .  ja  va 2 s .c o m
    JSONArray jArray = null;
    String json = "";

    // Making HTTP request
    try {
        // defaultHttpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);

        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("user_id", userId));
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jArray = new JSONArray(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    // return JSON String
    return jArray;
}

From source file:eionet.cr.util.Util.java

/**
 * Convenience method for URL-encoding the given string.
 *
 * @param s/* w  ww .  j ava 2s  .c  om*/
 * @return
 */
public static String urlEncode(String s) {
    try {
        return URLEncoder.encode(s, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new CRRuntimeException(e.toString(), e);
    }
}

From source file:eionet.cr.util.Util.java

/**
 * Convenience method for URL-decoding the given string.
 *
 * @param s//  w  w  w  .j  a  va 2 s .  c o m
 * @return
 */
public static String urlDecode(String s) {
    try {
        return URLDecoder.decode(s, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new CRRuntimeException(e.toString(), e);
    }
}

From source file:io.dockstore.webservice.helpers.Helper.java

/**
 * Refreshes user's Bitbucket token./*  w w w.  j  a va2s  . c  o  m*/
 *
 * @param token
 * @param client
 * @param tokenDAO
 * @param bitbucketClientID
 * @param bitbucketClientSecret
 * @return the updated token
 */
public static Token refreshBitbucketToken(Token token, HttpClient client, TokenDAO tokenDAO,
        String bitbucketClientID, String bitbucketClientSecret) {

    String url = BITBUCKET_URL + "site/oauth2/access_token";

    try {
        Optional<String> asString = ResourceUtilities.bitbucketPost(url, null, client, bitbucketClientID,
                bitbucketClientSecret, "grant_type=refresh_token&refresh_token=" + token.getRefreshToken());

        if (asString.isPresent()) {
            String accessToken;
            String refreshToken;
            LOG.info(token.getUsername() + ": RESOURCE CALL: {}", url);
            String json = asString.get();

            Gson gson = new Gson();
            Map<String, String> map = new HashMap<>();
            map = (Map<String, String>) gson.fromJson(json, map.getClass());

            accessToken = map.get("access_token");
            refreshToken = map.get("refresh_token");

            token.setContent(accessToken);
            token.setRefreshToken(refreshToken);

            long create = tokenDAO.create(token);
            return tokenDAO.findById(create);
        } else {
            throw new CustomWebApplicationException("Could not retrieve bitbucket.org token based on code",
                    HttpStatus.SC_INTERNAL_SERVER_ERROR);
        }
    } catch (UnsupportedEncodingException ex) {
        LOG.info(token.getUsername() + ": " + ex.toString());
        throw new CustomWebApplicationException(ex.toString(), HttpStatus.SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:org.apache.felix.webconsole.plugins.upnp.internal.Base64.java

static byte[] getBytesUtf8(String string) {
    if (string == null) {
        return null;
    }//  w  w w  .  j a va  2s  . c o  m
    try {
        return string.getBytes("UTF-8"); //$NON-NLS-1$
    } catch (UnsupportedEncodingException e) {
        throw new IllegalStateException(e.toString());
    }
}

From source file:io.lightlink.output.BinaryDownloadResponseStream.java

@Override
public void writeString(String valueStr) {
    try {//  w ww . j a va  2 s  .co  m
        write(valueStr.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e.toString(), e);
    }
}

From source file:org.wso2.cdm.agent.proxy.ServerApiAccess.java

public static Map<String, String> postData(APIUtilities apiUtilities, Map<String, String> headers) {
    String httpMethod = apiUtilities.getHttpMethod();
    String url = apiUtilities.getEndPoint();
    JSONObject params = apiUtilities.getRequestParams();
    Map<String, String> responseParams = new HashMap<String, String>();
    HttpClient httpclient = getCertifiedHttpClient();

    if (httpMethod.equals("POST")) {
        HttpPost httpPost = new HttpPost(url);
        if (params != null) {
            try {
                httpPost.setEntity(new StringEntity(params.toString()));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();/*  ww  w.  j a  v  a  2s.  c om*/
            }
        } else {
            httpPost.setEntity(null);
        }
        Log.e("url", "" + url);
        HttpPost httpPostWithHeaders = (HttpPost) buildHeaders(httpPost, headers, httpMethod);
        try {
            HttpResponse response = httpclient.execute(httpPostWithHeaders);
            String status = String.valueOf(response.getStatusLine().getStatusCode());
            Log.d(TAG, status);
            responseParams.put("response", getResponseBody(response));
            responseParams.put("status", status);
            return responseParams;
        } catch (ClientProtocolException e) {
            Log.d(TAG, "ClientProtocolException :" + e.toString());
            return null;
        } catch (IOException e) {
            Log.d(TAG, e.toString());
            responseParams.put("response", "Internal Server Error");
            responseParams.put("status", "500");
            return responseParams;
        }
    } else if (httpMethod.equals("GET")) {
        //          if(payload!=null){
        //             url = url+"?"+payload;
        //          }
        HttpGet httpGet = new HttpGet(url);
        HttpGet httpGetWithHeaders = (HttpGet) buildHeaders(httpGet, headers, httpMethod);
        Log.d(TAG, httpGetWithHeaders.toString() + " GET");
        try {
            HttpResponse response = httpclient.execute(httpGetWithHeaders);
            responseParams.put("response", getResponseBody(response));
            responseParams.put("status", String.valueOf(response.getStatusLine().getStatusCode()));
            return responseParams;
        } catch (ClientProtocolException e) {
            Log.d(TAG, "ClientProtocolException :" + e.toString());
            return null;
        } catch (IOException e) {
            Log.d(TAG, e.toString());
            responseParams.put("response", "Internal Server Error");
            responseParams.put("status", "500");
            return responseParams;
        }
    }
    return null;
}

From source file:org.pentaho.common.ui.services.ChartSeriesColorContentGenerator.java

@Override
public void createContent(OutputStream output) throws Exception {

    IParameterProvider params = parameterProviders.get(IParameterProvider.SCOPE_REQUEST);
    String type = params.getStringParameter("type", "mdx"); //$NON-NLS-1$
    type = type.toLowerCase();//from   ww w.  j  a v a  2  s  .  com

    if (!type.equals(TYPE_RELATIONAL) && !type.equals(TYPE_MDX)) {
        throw new IllegalStateException("Unknown chart series color model type: " + type);
    }

    IPluginResourceLoader resLoader = getPluginResourceLoader();
    String json = null;
    try {
        json = resLoader.getResourceAsString(ChartSeriesColorContentGenerator.class,
                "resources/chartseriescolor/" + type + ".json");
        if (json == null) {
            json = "{}"; // Empty
        }
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException(e.toString(), e);
    }
    output.write(json.getBytes());
}

From source file:com.t2.drupalsdk.ServicesClient.java

public void put(String url, JSONObject params, AsyncHttpResponseHandler responseHandler) {
    StringEntity se = null;/*from   ww w .  j ava2  s.c  o  m*/
    try {
        se = new StringEntity(params.toString());
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
    }
    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
    Log.d(TAG, "url = " + getAbsoluteUrl(url));

    mAsyncHttpClient.put(null, getAbsoluteUrl(url), se, "application/json", responseHandler);
}

From source file:org.esigate.MockRequestExecutor.java

private CloseableHttpResponse getResource(String url) throws HttpErrorPage {
    String result = resources.get(url);

    if (result == null) {
        throw new HttpErrorPage(HttpStatus.SC_NOT_FOUND, "Not found", "The page: " + url + " does not exist");
    }/*from ww w. j a  v  a2s  .  c  om*/
    try {
        return new HttpResponseBuilder().status(HttpStatus.SC_OK).reason("OK").entity(result).build();
    } catch (UnsupportedEncodingException e) {
        throw new HttpErrorPage(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.toString(), e.toString());
    }
}