Example usage for java.io UnsupportedEncodingException printStackTrace

List of usage examples for java.io UnsupportedEncodingException printStackTrace

Introduction

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

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:com.aqnote.shared.cryptology.util.lang.StreamUtil.java

public static String stream2Bytes(InputStream is, Charset charset) {
    String result = null;/* w  w  w. j  a v  a 2s.  c o  m*/
    try {
        int total = is.available();
        byte[] bs = new byte[total];
        is.read(bs);
        result = new String(bs, charset.name());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.tek271.reverseProxy.servlet.ProxyFilter.java

private static MultipartEntity getMultipartEntity(HttpServletRequest request) {
    MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
    Enumeration<String> en = request.getParameterNames();
    while (en.hasMoreElements()) {
        String name = en.nextElement();
        String value = request.getParameter(name);
        try {//from  ww w .  j  av a 2s . c  o m
            if (name.equals("file")) {
                FileItemFactory factory = new DiskFileItemFactory();
                ServletFileUpload upload = new ServletFileUpload(factory);
                upload.setSizeMax(10000000);// 10 Mo
                List items = upload.parseRequest(request);
                Iterator itr = items.iterator();
                while (itr.hasNext()) {
                    FileItem item = (FileItem) itr.next();
                    File file = new File(item.getName());
                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(item.get());
                    fos.flush();
                    fos.close();
                    entity.addPart(name, new FileBody(file, "application/zip"));
                }
            } else {
                entity.addPart(name, new StringBody(value.toString(), "text/plain", Charset.forName("UTF-8")));
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } catch (FileUploadException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } catch (FileNotFoundException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        } catch (IOException e) {
            e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
        }
    }

    return entity;

}

From source file:com.vmware.vchs.api.samples.services.ServiceController.java

/**
 * Creates an instance of the service provided by the instanceId
 * //from  www . j ava  2 s.  c  om
 * @param hostname
 *            the host URL to make API calls to
 * @param version
 *            the version of the API to call
 * @param token
 *            the authentication token to make API calls with
 * @param planId
 *            the id of the plan to base this instance from
 * @param serviceGroupId
 *            the service group id of the org to create this instance in
 * @return true if successfully created, false otherwise.
 */
public static boolean createInstance(String hostname, String version, String token, String planId,
        String serviceGroupId) {
    HttpPost post = new HttpPost(hostname + "/api/sc/instances");
    post.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + token);
    post.setHeader(HttpHeaders.ACCEPT, SampleConstants.APPLICATION_JSON_VERSION + version
            + ";class=com.vmware.vchs.sc.restapi.model.instancetype");
    post.setHeader(HttpHeaders.CONTENT_TYPE, SampleConstants.APPLICATION_JSON_VERSION + version
            + ";class=com.vmware.vchs.sc.restapi.model.instancespecparamstype");

    InstanceType it = new InstanceType();
    it.setName("NewVDC");
    it.setDescription("A description of new service");
    it.setPlanId(planId);
    it.setServiceGroupId(serviceGroupId);

    Gson g = new Gson();

    String instanceToCreate = g.toJson(it);

    HttpEntity entity;
    try {
        entity = new StringEntity(instanceToCreate);
        post.setEntity(entity);
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    }

    HttpResponse response = HttpUtils.httpInvoke(post);

    if (null != response) {
        // If the response status is 400 - 599
        if (response.getStatusLine().getStatusCode() >= HttpStatus.SC_BAD_REQUEST) {
            // This is here to show when an error occurs, the response should always be
            // an Error instance
            String s = null;

            try {
                s = EntityUtils.toString(response.getEntity());
                Error error = g.fromJson(s, Error.class);
                // Do something with Error, possibly using Error.getCode() value to
                // determine the specific reason for the error.
            } catch (ParseException e) {
                throw new RuntimeException("Error parsing error response: " + e.getMessage());
            } catch (IOException e) {
                throw new RuntimeException("Error with response: " + e.getMessage());
            }
        } else {
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                return true;
            }
        }
    }

    return false;
}

From source file:export.FormCrawler.java

public static String URLEncode(String s) {
    try {/*from  w w w. j  a  v  a  2 s . c om*/
        return URLEncoder.encode(s, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return s;
    }
}

From source file:com.yutong.axxc.parents.view.common.ActivityUtils.java

/**
 * ?//from ww  w .ja  v  a  2 s . c  o m
 * @param QJstr
 * @return
 */
public static final String QBchange(String QJstr) {
    String outStr = "";
    String Tstr = "";
    byte[] b = null;

    for (int i = 0; i < QJstr.length(); i++) {
        try {
            Tstr = QJstr.substring(i, i + 1);
            b = Tstr.getBytes("unicode");
        } catch (java.io.UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (b[3] == -1) {
            b[2] = (byte) (b[2] + 32);
            b[3] = 0;
            try {
                outStr = outStr + new String(b, "unicode");
            } catch (java.io.UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        } else
            outStr = outStr + Tstr;
    }
    return outStr;
}

From source file:co.aurasphere.botmill.util.NetworkUtils.java

/**
 * Post json message.//from  w  w w  . j a  va  2s .  c  om
 *
 * @param jsonData the json data
 * @return the string
 */
public static String postTrainingString(String jsonData) {
    StringEntity stringEntity;
    try {
        stringEntity = new StringEntity(jsonData);
        stringEntity.setContentType("application/json");
        HttpPost post = new HttpPost(
                RasaBotMillContext.getRasaConfig().toString() + NetworkConstants.TRAIN_EP + concatToken());
        post.setEntity(stringEntity);
        return send(post);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:com.robin.reporter.Reporter.java

public static void storeOriginalSysOut() {
    if (origSysOut == null) {
        try {//w  ww  . j a v a2 s  . c  o  m
            origSysOut = new PrintStream(System.out, true, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            origSysOut = new PrintStream(System.out, true);
        }
    }
}

From source file:org.sead.repositories.reference.util.SEADGoogleLogin.java

static void getTokenFromRefreshToken() {
    access_token = null;/*  ww w. j  av a 2s. c o m*/
    expires_in = -1;
    token_start_time = -1;

    if (gProps == null) {
        initGProps();
    }

    /* Try refresh token */
    // Query for token now that user has gone through browser part
    // of
    // flow

    // The method used in getTokensFromCode should work here as well - I
    // think URL encoded Form is the recommended way...
    HttpPost post = new HttpPost(gProps.token_uri);
    post.addHeader("accept", "application/json");
    post.addHeader("Content-Type", "application/x-www-form-urlencoded");

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
    nameValuePairs.add(new BasicNameValuePair("client_id", gProps.client_id));
    nameValuePairs.add(new BasicNameValuePair("client_secret", gProps.client_secret));
    nameValuePairs.add(new BasicNameValuePair("refresh_token", refresh_token));
    nameValuePairs.add(new BasicNameValuePair("grant_type", "refresh_token"));

    try {
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    }

    CloseableHttpClient httpclient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    try {
        try {
            response = httpclient.execute(post);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    String responseJSON = EntityUtils.toString(resEntity);
                    ObjectNode root = (ObjectNode) new ObjectMapper().readTree(responseJSON);
                    access_token = root.get("access_token").asText();
                    // refresh_token =
                    // root.get("refresh_token").asText();
                    token_start_time = System.currentTimeMillis() / 1000;
                    expires_in = root.get("expires_in").asInt();
                }
            } else {
                log.error("Error response from Google: " + response.getStatusLine().getReasonPhrase());
                HttpEntity resEntity = response.getEntity();
            }
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            log.error("Error obtaining access token: " + e.getMessage());
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }
    } catch (IOException io) {
        log.error("Error closing connections: " + io.getMessage());
    }
}

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

public static void postJSON(String url, String JSONString) {

    HttpPost httpPost = new HttpPost(url);
    StringEntity se;/*from w w  w  . j av  a  2  s  . c  o  m*/

    try {

        se = new StringEntity(JSONString);
        se.setContentType("application/json");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        //httpPost.setHeader("Accept", "application/json");
        httpPost.setEntity(se);
        HttpResponse resp = new DefaultHttpClient().execute(httpPost);

    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.huawei.streaming.cql.LocalTaskCommons.java

/**
 * ?//  w  ww.  j  a  v  a 2  s . c  o  m
 *
 */
public static String setDir(String basePath) {
    String classPath = LocalTaskCommons.class.getResource("/").getPath();

    try {
        classPath = URLDecoder.decode(classPath, DEFAULT_CHARSET);
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return classPath + basePath + File.separator;
}