List of usage examples for java.io UnsupportedEncodingException getMessage
public String getMessage()
From source file:com.flicklib.service.cache.HttpCache4J.java
/** {@inheritDoc} */ @Override/*from w w w .j a v a 2 s .co m*/ public Source post(String url, Map<String, String> parameters, Map<String, String> headers) { HTTPRequest request = new HTTPRequest(URI.create(url), HTTPMethod.POST); for (Entry<String, String> header : headers.entrySet()) { request.addHeader(header.getKey(), header.getValue()); } // TODO Enable when new version of httpcache4j is released // // List<FormParameter> params = new ArrayList<FormParameter>(); // for(Entry<String,String> param:parameters.entrySet()){ // params.add(new FormParameter(param.getKey(), param.getValue())); // } // FormDataPayload payload = new FormDataPayload(params); // request = request.payload(payload); ///////////////// TODO replace this part with the code above ///////////////////// StringBuilder builder = new StringBuilder(); // TODO fix when this is fixed: http://jira.codehaus.org/browse/HTCJ-51 // or see in httpclient if we can reuse code for (Entry<String, String> param : parameters.entrySet()) { if (builder.length() > 0) { builder.append("&"); } String encoding = "UTF-8"; try { builder.append(URLEncoder.encode(param.getKey(), encoding)).append("=") .append(URLEncoder.encode(param.getValue(), encoding)); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } } ByteArrayInputStream bis = new ByteArrayInputStream(builder.toString().getBytes()); MIMEType mimeType = new MIMEType("application/x-www-form-urlencoded"); Payload payload = null; try { payload = new ByteArrayPayload(bis, mimeType); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } request = request.payload(payload); ////////////////////////////////////////////////////////////////////////// HTTPResponse response = cache.execute(request, false); String content = payloadToString(response); final String respUrl = response.getHeaders().getFirstHeaderValue("Content-Location"); final String theUrl = (respUrl != null) ? respUrl : url; Source source = new Source(theUrl, content, response.getPayload().getMimeType().toString(), url); return source; }
From source file:com.sammyun.plugin.alipayWap.AlipayWapPlugin.java
@Override public Map<String, Object> getParameterMap(String sn, String description, HttpServletRequest request) { PluginConfig pluginConfig = getPluginConfig(); Payment payment = getPayment(sn);/* w w w.j a va2 s .co m*/ String sellerAccountName = pluginConfig.getAttribute("partner"); String subject = StringUtils.abbreviate(description.replaceAll("[^0-9a-zA-Z\\u4e00-\\u9fa5 ]", ""), 60); String totalFee = payment.getAmount().setScale(2).toString(); String merchantUrl = getMobileNotifyUrl(sn, NotifyMethod.sync); // ? String format = "xml"; // ?? // ? String v = "2.0"; // ? String reqDataToken = "<direct_trade_create_req><notify_url>" + getMobileNotifyUrl(sn, NotifyMethod.async) + "</notify_url><call_back_url>" + getMobileNotifyUrl(sn, NotifyMethod.sync) + "</call_back_url><seller_account_name>" + "moco.tmall@moco-paris.com" + "</seller_account_name><out_trade_no>" + sn + "</out_trade_no><subject>" + subject + "</subject><total_fee>" + totalFee + "</total_fee><merchant_url>" + merchantUrl + "</merchant_url></direct_trade_create_req>"; Map<String, Object> parameterMapToken = new HashMap<String, Object>(); parameterMapToken.put("service", "alipay.wap.trade.create.direct"); parameterMapToken.put("partner", pluginConfig.getAttribute("partner")); parameterMapToken.put("_input_charset", "utf-8"); // ???0001(RSA)?MD5 // ????rsasign_type0001?RSA parameterMapToken.put("sec_id", PluginConfig.SIGN_TYPE); parameterMapToken.put("format", format); parameterMapToken.put("v", v); parameterMapToken.put("req_id", DateUtil.getOrderNum()); parameterMapToken.put("req_data", reqDataToken); String requestToken = ""; // URLDECODE? try { // String sHtmlTextToken = buildRequest(getRequestUrl(), parameterMapToken); // sHtmlTextToken = // sHtmlTextToken.replaceAll("%(?![0-9a-fA-F]{2})", // "%25"); // sHtmlTextToken = sHtmlTextToken.replaceAll("\\+", "%2B"); sHtmlTextToken = URLDecoder.decode(sHtmlTextToken, "utf-8"); requestToken = super.getRequestToken(sHtmlTextToken); } catch (UnsupportedEncodingException e) { e.printStackTrace(); logger.error(e.getMessage()); } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } // String reqData = "<auth_and_execute_req><request_token>" + requestToken + "</request_token></auth_and_execute_req>"; // Map<String, Object> parameterMap = new HashMap<String, Object>(); parameterMap.put("service", "alipay.wap.auth.authAndExecute"); parameterMap.put("partner", sellerAccountName); parameterMap.put("_input_charset", "utf-8"); parameterMap.put("sec_id", PluginConfig.SIGN_TYPE); parameterMap.put("format", format); parameterMap.put("v", v); parameterMap.put("req_data", reqData); return buildRequestPara(parameterMap); }
From source file:mitm.common.security.smime.handler.SMIMEInfoHandlerImpl.java
private void setHeader(String headerName, String headerValue, int level, Part part) throws MessagingException { if (headerValue == null) { headerValue = ""; }//from w w w . j a v a2s . co m headerName = headerName + "-" + Integer.toString(level); headerValue = MiscStringUtils.restrictLength(headerValue, maxHeaderLength); try { /* make sure the header only contains ASCII characters and is folded * when necessary */ headerValue = HeaderUtils.encodeHeaderValue(headerName, headerValue); } catch (UnsupportedEncodingException e) { logger.warn("Header value cannot be encoded. Message: " + e.getMessage()); } part.setHeader(headerName, headerValue); }
From source file:com.telefonica.iot.cygnus.backends.http.HttpBackendTest.java
/** * [HttpBackend.createJsonResponse] -------- A JsonResponse object is * created if the content-type header contains 'application/json' but no * location header./*from w ww . j a v a 2 s . c o m*/ */ @Test public void testCreateJsonResponseNoLocationHeader() { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "-------- A JsonResponse object is created if the content-type header contains 'application/json' " + "but no location header"); HttpResponseFactory factory = new DefaultHttpResponseFactory(); HttpResponse response = factory .newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null), null); String responseStr = "{\"somefield1\":\"somevalue1\",\"somefield2\":\"somevalue2\"," + "\"somefield3\":\"somevalue3\"}"; try { response.setEntity(new StringEntity(responseStr)); } catch (UnsupportedEncodingException e) { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- FAIL - There was some problem when creating the HttpResponse object"); throw new AssertionError(e.getMessage()); } // try catch response.addHeader("Content-Type", "application/json"); HttpBackend httpBackend = new HttpBackendImpl(host, port, false, false, null, null, null, null, maxConns, maxConnsPerRoute); try { JsonResponse jsonRes = httpBackend.createJsonResponse(response); try { assertEquals(null, jsonRes.getLocationHeader()); System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- OK - The JsonResponse object was created with null location header"); } catch (AssertionError e) { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- FAIL - The JsonResponse object was not created with null location header"); throw e; } // try catch } catch (Exception e) { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- FAIL - There was some problem when creating the JsonResponse object"); throw new AssertionError(e.getMessage()); } // try catch }
From source file:com.telefonica.iot.cygnus.backends.http.HttpBackendTest.java
/** * [HttpBackend.createJsonResponse] -------- A JsonResponse object is not * created if the content-type header does not contains 'application/json'. *///w w w .ja va 2s . c o m @Test public void testCreateJsonResponseNoJsonPayload() { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "-------- A JsonResponse object is not created if the content-type header does not contains " + "'application/json'"); HttpResponseFactory factory = new DefaultHttpResponseFactory(); HttpResponse response = factory .newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null), null); String responseStr = "{\"somefield1\":\"somevalue1\",\"somefield2\":\"somevalue2\"," + "\"somefield3\":\"somevalue3\"}"; try { response.setEntity(new StringEntity(responseStr)); } catch (UnsupportedEncodingException e) { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- FAIL - There was some problem when creating the HttpResponse object"); throw new AssertionError(e.getMessage()); } // try catch response.addHeader("Content-Type", "text/html"); response.addHeader("Location", "http://someurl.org"); HttpBackend httpBackend = new HttpBackendImpl(host, port, false, false, null, null, null, null, maxConns, maxConnsPerRoute); try { JsonResponse jsonRes = httpBackend.createJsonResponse(response); try { assertEquals(null, jsonRes.getJsonObject()); System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- OK - The JsonResponse object could not be created with a 'text/html' content type " + "header"); } catch (AssertionError e) { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- FAIL - The JsonResponse object was created with a 'text/html' content type header"); throw e; } // try catch } catch (Exception e) { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- FAIL - There was some problem when creating the JsonResponse object"); throw new AssertionError(e.getMessage()); } // try catch }
From source file:com.telefonica.iot.cygnus.backends.http.HttpBackendTest.java
/** * [HttpBackend.createJsonResponse] -------- A JsonResponse object is * created if the content-type header contains 'application/json' but no * location header./*from w w w.j a v a 2 s . c o m*/ */ @Test public void testCreateJsonResponseHeader() { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "-------- A JsonResponse object is created if the content-type header contains 'application/json' " + "but no location header"); HttpResponseFactory factory = new DefaultHttpResponseFactory(); HttpResponse response = factory .newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null), null); String responseStr = "{\"somefield1\":\"somevalue1\",\"somefield2\":\"somevalue2\"," + "\"somefield3\":\"somevalue3\"}"; try { response.setEntity(new StringEntity(responseStr)); } catch (UnsupportedEncodingException e) { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- FAIL - There was some problem when creating the HttpResponse object"); throw new AssertionError(e.getMessage()); } // try catch response.addHeader("Content-Type", "application/json"); HttpBackend httpBackend = new HttpBackendImpl(host, port, false, false, null, null, null, null, maxConns, maxConnsPerRoute); httpBackend.setAllHeaders(true); try { JsonResponse jsonRes = httpBackend.createJsonResponse(response); try { for (Header header : jsonRes.getHeaders()) { assertEquals("Content-Type", header.getName()); assertEquals("application/json", header.getValue()); } System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- OK - The JsonResponse object was created with headers"); } catch (AssertionError e) { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- FAIL - The JsonResponse object was not created with null location header"); throw e; } // try catch } catch (Exception e) { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- FAIL - There was some problem when creating the JsonResponse object"); throw new AssertionError(e.getMessage()); } // try catch }
From source file:org.apache.sling.discovery.base.connectors.ping.TopologyRequestValidator.java
/** * Generate a signature of the bodyHash and encode it so that it contains * the key number used to generate the signature. * * @param bodyHash a hash//from w w w. j a v a2s . c o m * @return the signature. */ private String createTrustHeader(String bodyHash) { try { int keyNo = getCurrentKey(); return keyNo + "/" + hmac(keyNo, bodyHash); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e.getMessage(), e); } catch (InvalidKeyException e) { throw new RuntimeException(e.getMessage(), e); } catch (IllegalStateException e) { throw new RuntimeException(e.getMessage(), e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:com.telefonica.iot.cygnus.backends.http.HttpBackendTest.java
/** * [HttpBackend.createJsonResponse] -------- A JsonResponse object is * created if the response content-type header is 'application/json' and the * response contains a location header.//w w w .j ava 2 s. co m */ @Test public void testCreateJsonResponseEverythingOK() { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "-------- A JsonResponse object is created if the response content-type header is " + "'application/json' and the response contains a location header"); HttpResponseFactory factory = new DefaultHttpResponseFactory(); HttpResponse response = factory .newHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, null), null); String responseStr = "{\"somefield1\":\"somevalue1\",\"somefield2\":\"somevalue2\"," + "\"somefield3\":\"somevalue3\"}"; try { response.setEntity(new StringEntity(responseStr)); } catch (UnsupportedEncodingException e) { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- FAIL - There was some problem when creating the HttpResponse object"); throw new AssertionError(e.getMessage()); } // try catch response.addHeader("Content-Type", "application/json"); response.addHeader("Location", "http://someurl.org"); HttpBackend httpBackend = new HttpBackendImpl(host, port, false, false, null, null, null, null, maxConns, maxConnsPerRoute); try { JsonResponse jsonRes = httpBackend.createJsonResponse(response); try { assertTrue(jsonRes.getJsonObject() != null); System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- OK - The JsonResponse object has a Json apyload"); } catch (AssertionError e) { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- FAIL - The JsonResponse object has not a Json payload"); throw e; } // try catch try { assertTrue(jsonRes.getLocationHeader() != null); System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- OK - The JsonResponse object has a Location header"); } catch (AssertionError e) { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- FAIL - The JsonResponse object has not a Location header"); throw e; } // try catch } catch (Exception e) { System.out.println(getTestTraceHead("[HttpBackend.createJsonResponse]") + "- FAIL - There was some problem when creating the JsonResponse object"); throw new AssertionError(e.getMessage()); } // try catch }
From source file:org.lightcouch.CouchDbClientBase.java
/** * Sets a JSON String as a request entity. * @param httpRequest The request to set entity. * @param json The JSON String to set./*from ww w. j av a2 s . co m*/ */ protected void setEntity(HttpEntityEnclosingRequestBase httpRequest, String json) { try { StringEntity entity = new StringEntity(json, "UTF-8"); entity.setContentType("application/json"); httpRequest.setEntity(entity); } catch (UnsupportedEncodingException e) { log.error("Error setting request data. " + e.getMessage()); throw new IllegalArgumentException(e); } }