List of usage examples for java.io UnsupportedEncodingException getMessage
public String getMessage()
From source file:com.ichi2.anki.AnkiDroidProxy.java
public String getDecks() { // Log.i(AnkiDroidApp.TAG, "getDecks - user = " + username + ", password = " + password); String decksServer = "{}"; try {/*w w w . j a v a 2s.com*/ // FIXME: Client is hardcoded. String data = "p=" + URLEncoder.encode(mPassword, "UTF-8") + "&client=ankidroid-0.4&u=" + URLEncoder.encode(mUsername, "UTF-8") + "&d=None&sources=" + URLEncoder.encode("[]", "UTF-8") + "&libanki=0.9.9.8.6&pversion=5"; // Log.i(AnkiDroidApp.TAG, "Data json = " + data); HttpPost httpPost = new HttpPost(SYNC_URL + "getDecks"); StringEntity entity = new StringEntity(data); httpPost.setEntity(entity); httpPost.setHeader("Accept-Encoding", "identity"); httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(httpPost); Log.i(AnkiDroidApp.TAG, "Response = " + response.toString()); HttpEntity entityResponse = response.getEntity(); Log.i(AnkiDroidApp.TAG, "Entity's response = " + entityResponse.toString()); InputStream content = entityResponse.getContent(); Log.i(AnkiDroidApp.TAG, "Content = " + content.toString()); decksServer = Utils.convertStreamToString(new InflaterInputStream(content)); Log.i(AnkiDroidApp.TAG, "String content = " + decksServer); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { Log.i(AnkiDroidApp.TAG, "ClientProtocolException = " + e.getMessage()); } catch (IOException e) { Log.i(AnkiDroidApp.TAG, "IOException = " + e.getMessage()); } return decksServer; }
From source file:com.microsoft.aad.adal.Oauth2.java
/** * get code and exchange for token.//from www . j a v a 2 s . c o m * * @param code * @return Token in the AuthenticationResult * @throws Exception */ public AuthenticationResult getTokenForCode(String code) throws IOException, AuthenticationException { String requestMessage = null; if (mWebRequestHandler == null) { throw new IllegalArgumentException("webRequestHandler"); } // Token request message try { requestMessage = buildTokenRequestMessage(code); } catch (UnsupportedEncodingException encoding) { Logger.e(TAG, encoding.getMessage(), "", ADALError.ENCODING_IS_NOT_SUPPORTED, encoding); return null; } HashMap<String, String> headers = getRequestHeaders(); return postMessage(requestMessage, headers); }
From source file:com.ichi2.anki.AnkiDroidProxy.java
public void createDeck(String name) { Log.i(AnkiDroidApp.TAG, "createDeck"); // Log.i(AnkiDroidApp.TAG, "user = " + username + ", password = " + password); try {//from w w w . j a v a2 s.c o m String data = "p=" + URLEncoder.encode(mPassword, "UTF-8") + "&u=" + URLEncoder.encode(mUsername, "UTF-8") + "&d=None&name=" + URLEncoder.encode(name, "UTF-8"); // Log.i(AnkiDroidApp.TAG, "Data json = " + data); HttpPost httpPost = new HttpPost(SYNC_URL + "createDeck"); StringEntity entity = new StringEntity(data); httpPost.setEntity(entity); httpPost.setHeader("Accept-Encoding", "identity"); httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(httpPost); Log.i(AnkiDroidApp.TAG, "Response = " + response.toString()); HttpEntity entityResponse = response.getEntity(); Log.i(AnkiDroidApp.TAG, "Entity's response = " + entityResponse.toString()); InputStream content = entityResponse.getContent(); Log.i(AnkiDroidApp.TAG, "Content = " + content.toString()); Log.i(AnkiDroidApp.TAG, "String content = " + Utils.convertStreamToString(new InflaterInputStream(content))); // Add created deck to the list of decks on server mDecks.put(name, new JSONArray("[0,0]")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { Log.i(AnkiDroidApp.TAG, "ClientProtocolException = " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { Log.i(AnkiDroidApp.TAG, "IOException = " + e.getMessage()); e.printStackTrace(); } catch (JSONException e) { Log.i(AnkiDroidApp.TAG, "JSONException = " + e.getMessage()); e.printStackTrace(); } }
From source file:org.drftpd.protocol.speedtest.net.slave.SpeedTestHandler.java
private float getUploadSpeed(String url) { long totalTime = 0L; long totalBytes = 0L; long startTime = System.currentTimeMillis(); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(60000).setConnectTimeout(5000) .setConnectionRequestTimeout(5000).build(); HttpPost httpPost = new HttpPost(url); httpPost.setHeader("content-type", "application/x-www-form-urlencoded"); httpPost.setConfig(requestConfig);//from w w w. ja v a 2s . c om String payload = _payload; // Initial payload StopWatch watch = new StopWatch(); SpeedTestCallable[] speedTestCallables = new SpeedTestCallable[_upThreads]; for (int i = 0; i < _upThreads; i++) { speedTestCallables[i] = new SpeedTestCallable(); } ExecutorService executor = Executors.newFixedThreadPool(_upThreads); List<Future<Long>> threadList; Set<Callable<Long>> callables = new HashSet<Callable<Long>>(); boolean limitReached = false; int i = 2; while (true) { if ((System.currentTimeMillis() - startTime) > _upTime) { break; } List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("content1", payload)); try { httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); } catch (UnsupportedEncodingException e) { logger.error("Unsupported encoding of payload for speedtest upload: " + e.getMessage()); close(executor, callables); return 0; } callables.clear(); for (int k = 0; k < _upThreads; k++) { speedTestCallables[k].setHttpPost(httpPost); callables.add(speedTestCallables[k]); } for (int j = 0; j < _payloadLoop; j++) { try { watch.reset(); Thread.sleep(_sleep); watch.start(); threadList = executor.invokeAll(callables); for (Future<Long> fut : threadList) { Long bytes = fut.get(); totalBytes += bytes; } watch.stop(); totalTime += watch.getTime(); } catch (InterruptedException e) { logger.error(e.getMessage()); close(executor, callables); return 0; } catch (ExecutionException e) { if (e.getMessage().contains("Error code 413")) { limitReached = true; payload = StringUtils.repeat(_payload, i - 2); } else { logger.error(e.getMessage()); close(executor, callables); return 0; } } if ((System.currentTimeMillis() - startTime) > _upTime) { break; } } if (!limitReached) { // Increase payload size if not too big payload = StringUtils.repeat(_payload, i); i++; } } if (totalBytes == 0L || totalTime == 0L) { close(executor, callables); return 0; } close(executor, callables); return (float) (((totalBytes * 8) / totalTime) * 1000) / 1000000; }
From source file:com.microsoft.aad.adal.Oauth2.java
public AuthenticationResult refreshToken(String refreshToken) throws IOException, AuthenticationException { String requestMessage = null; if (mWebRequestHandler == null) { Logger.v(TAG, "Web request is not set correctly"); throw new IllegalArgumentException("webRequestHandler is null."); }/*from ww w . jav a 2s. com*/ // Token request message try { requestMessage = buildRefreshTokenRequestMessage(refreshToken); } catch (UnsupportedEncodingException encoding) { Logger.e(TAG, encoding.getMessage(), "", ADALError.ENCODING_IS_NOT_SUPPORTED, encoding); return null; } HashMap<String, String> headers = getRequestHeaders(); // Refresh token endpoint needs to send header field for device // challenge headers.put(AuthenticationConstants.Broker.CHALLENGE_TLS_INCAPABLE, AuthenticationConstants.Broker.CHALLENGE_TLS_INCAPABLE_VERSION); return postMessage(requestMessage, headers); }
From source file:com.ichi2.anki.AnkiDroidProxy.java
/** * Anki Desktop -> libanki/anki/sync.py, HttpSyncServerProxy - applyPayload * * @param payload/*from w ww.j a va 2 s . co m*/ */ public JSONObject applyPayload(JSONObject payload) { Log.i(AnkiDroidApp.TAG, "applyPayload"); // Log.i(AnkiDroidApp.TAG, "user = " + username + ", password = " + password + ", payload = " + // payload.toString()); JSONObject payloadReply = new JSONObject(); try { // FIXME: Try to do the connection without encoding the payload in Base 64 String data = "p=" + URLEncoder.encode(mPassword, "UTF-8") + "&u=" + URLEncoder.encode(mUsername, "UTF-8") + "&d=" + URLEncoder.encode(mDeckName, "UTF-8") + "&payload=" + URLEncoder.encode(Base64.encodeBytes(Utils.compress(payload.toString().getBytes())), "UTF-8") + "&base64=" + URLEncoder.encode("true", "UTF-8"); // Log.i(AnkiDroidApp.TAG, "Data json = " + data); HttpPost httpPost = new HttpPost(SYNC_URL + "applyPayload"); StringEntity entity = new StringEntity(data); httpPost.setEntity(entity); httpPost.setHeader("Accept-Encoding", "identity"); httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(httpPost); Log.i(AnkiDroidApp.TAG, "Response = " + response.toString()); HttpEntity entityResponse = response.getEntity(); Log.i(AnkiDroidApp.TAG, "Entity's response = " + entityResponse.toString()); InputStream content = entityResponse.getContent(); Log.i(AnkiDroidApp.TAG, "Content = " + content.toString()); String contentString = Utils.convertStreamToString(new InflaterInputStream(content)); Log.i(AnkiDroidApp.TAG, "Payload response = "); payloadReply = new JSONObject(contentString); Utils.printJSONObject(payloadReply, false); Utils.saveJSONObject(payloadReply); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { Log.i(AnkiDroidApp.TAG, "ClientProtocolException = " + e.getMessage()); } catch (IOException e) { Log.i(AnkiDroidApp.TAG, "IOException = " + e.getMessage()); } catch (JSONException e) { Log.i(AnkiDroidApp.TAG, "JSONException = " + e.getMessage()); } return payloadReply; }
From source file:com.ichi2.anki.AnkiDroidProxy.java
/** * Anki Desktop -> libanki/anki/sync.py, HttpSyncServerProxy - summary * //from www. j a v a2 s . c o m * @param lastSync */ public JSONObject summary(double lastSync) { Log.i(AnkiDroidApp.TAG, "Summary Server"); // Log.i(AnkiDroidApp.TAG, "user = " + username + ", password = " + password + ", lastSync = " + lastSync); JSONObject summaryServer = new JSONObject(); try { // FIXME: Try to do the connection without encoding the lastSync in Base 64 String data = "p=" + URLEncoder.encode(mPassword, "UTF-8") + "&u=" + URLEncoder.encode(mUsername, "UTF-8") + "&d=" + URLEncoder.encode(mDeckName, "UTF-8") + "&lastSync=" + URLEncoder.encode( Base64.encodeBytes( Utils.compress(String.format(Utils.ENGLISH_LOCALE, "%f", lastSync).getBytes())), "UTF-8") + "&base64=" + URLEncoder.encode("true", "UTF-8"); // Log.i(AnkiDroidApp.TAG, "Data json = " + data); HttpPost httpPost = new HttpPost(SYNC_URL + "summary"); StringEntity entity = new StringEntity(data); httpPost.setEntity(entity); httpPost.setHeader("Accept-Encoding", "identity"); httpPost.setHeader("Content-type", "application/x-www-form-urlencoded"); DefaultHttpClient httpClient = new DefaultHttpClient(); HttpResponse response = httpClient.execute(httpPost); Log.i(AnkiDroidApp.TAG, "Response = " + response.toString()); HttpEntity entityResponse = response.getEntity(); Log.i(AnkiDroidApp.TAG, "Entity's response = " + entityResponse.toString()); InputStream content = entityResponse.getContent(); Log.i(AnkiDroidApp.TAG, "Content = " + content.toString()); summaryServer = new JSONObject(Utils.convertStreamToString(new InflaterInputStream(content))); Log.i(AnkiDroidApp.TAG, "Summary server = "); Utils.printJSONObject(summaryServer); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { Log.i(AnkiDroidApp.TAG, "ClientProtocolException = " + e.getMessage()); } catch (IOException e) { Log.i(AnkiDroidApp.TAG, "IOException = " + e.getMessage()); } catch (JSONException e) { Log.i(AnkiDroidApp.TAG, "JSONException = " + e.getMessage()); } return summaryServer; }
From source file:org.mycontroller.restclient.core.RestHttpClient.java
protected RestHttpResponse doPut(String url, RestHeader header, String entity, Integer expectedResponseCode) { _logger.debug("Entity: {}", entity); try {//from w ww. ja v a 2 s . co m return doPut(url, header, new StringEntity(entity), expectedResponseCode); } catch (UnsupportedEncodingException ex) { _logger.error("Exception when calling url:[{}], headers:[{}], entity:[{}]", url, header, entity, ex); throw new RuntimeException( MessageFormat.format("Failed to execute, url:{0}, error:{1}", url, ex.getMessage())); } }
From source file:com.puppycrawl.tools.checkstyle.CheckerTest.java
@SuppressWarnings("deprecation") @Test/*from w w w .j a v a 2 s. c om*/ public void testSetters() { // all that is set by reflection, so just make code coverage be happy final Checker checker = new Checker(); checker.setClassLoader(getClass().getClassLoader()); checker.setClassloader(getClass().getClassLoader()); checker.setBasedir("some"); checker.setSeverity("ignore"); final PackageObjectFactory factory = new PackageObjectFactory(new HashSet<String>(), Thread.currentThread().getContextClassLoader()); checker.setModuleFactory(factory); checker.setFileExtensions((String[]) null); checker.setFileExtensions(".java", "xml"); try { checker.setCharset("UNKNOWN-CHARSET"); fail("Exception is expected"); } catch (UnsupportedEncodingException ex) { assertEquals("unsupported charset: 'UNKNOWN-CHARSET'", ex.getMessage()); } }
From source file:edu.harvard.iq.dataverse.MailServiceBean.java
public void sendMail(String host, String from, String to, String subject, String messageText) { Properties props = System.getProperties(); props.put("mail.smtp.host", host); Session session = Session.getDefaultInstance(props, null); try {/*from w w w . java 2s. co m*/ MimeMessage msg = new MimeMessage(session); String[] recipientStrings = to.split(","); InternetAddress[] recipients = new InternetAddress[recipientStrings.length]; try { msg.setFrom(new InternetAddress(from, charset)); for (int i = 0; i < recipients.length; i++) { recipients[i] = new InternetAddress(recipientStrings[i], "", charset); } } catch (UnsupportedEncodingException ex) { logger.severe(ex.getMessage()); } msg.setRecipients(Message.RecipientType.TO, recipients); msg.setSubject(subject, charset); msg.setText(messageText, charset); Transport.send(msg, recipients); } catch (AddressException ae) { ae.printStackTrace(System.out); } catch (MessagingException me) { me.printStackTrace(System.out); } }