List of usage examples for org.apache.http.util EncodingUtils getBytes
public static byte[] getBytes(String str, String str2)
From source file:system.info.reader.java
public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case 0://from ww w . j ava 2 s.c o m switch (tabHost.getCurrentTab()) { case 0: refresh(); break; case 1: serverWeb.reload(); break; } break; case 1: String postData = ""; postData += "processor=" + Properties.processor + "&"; postData += "bogomips=" + Properties.bogomips + "&"; postData += "hardware=" + Properties.hardware + "&"; postData += "memtotal=" + Properties.memtotal + "&"; postData += "resolution=" + Properties.resolution + "&"; postData += "sCamera=" + Properties.sCamera + "&"; postData += "sensors=" + Properties.sensors + "&"; postData += "vendor=" + Properties.vendor + "&"; postData += "product=" + Properties.product + "&"; postData += "sdkversion=" + Properties.sdkversion + "&"; postData += "imei=" + Properties.imei + "&"; postData += "clientVersion=" + "full-version&"; postData += "versionCode=" + versionCode; serverWeb.postUrl(getString(R.string.url) + "/sign", EncodingUtils.getBytes(postData, "BASE64")); tabHost.setCurrentTab(1); break; case 2: showDialog(1); break; case 3: finish(); System.exit(0); break; } return true; }
From source file:info.ajaxplorer.client.http.RestRequest.java
private HttpResponse issueRequest(URI uri, Map<String, String> postParameters, File file, String fileName, AjxpFileBody fileBody) throws Exception { URI originalUri = new URI(uri.toString()); if (RestStateHolder.getInstance().getSECURE_TOKEN() != null) { uri = new URI( uri.toString().concat("&secure_token=" + RestStateHolder.getInstance().getSECURE_TOKEN())); }/* w ww .j av a2 s . co m*/ //Log.d("RestRequest", "Issuing request : " + uri.toString()); HttpResponse response = null; try { HttpRequestBase request; if (postParameters != null || file != null) { request = new HttpPost(); if (file != null) { if (fileBody == null) { if (fileName == null) fileName = file.getName(); fileBody = new AjxpFileBody(file, fileName); ProgressListener origPL = this.uploadListener; Map<String, String> caps = RestStateHolder.getInstance().getServer() .getRemoteCapacities(this); this.uploadListener = origPL; int maxUpload = 0; if (caps != null && caps.containsKey(Server.capacity_UPLOAD_LIMIT)) maxUpload = new Integer(caps.get(Server.capacity_UPLOAD_LIMIT)); maxUpload = Math.min(maxUpload, 60000000); if (maxUpload > 0 && maxUpload < file.length()) { fileBody.chunkIntoPieces(maxUpload); if (uploadListener != null) { uploadListener.partTransferred(fileBody.getCurrentIndex(), fileBody.getTotalChunks()); } } } else { if (uploadListener != null) { uploadListener.partTransferred(fileBody.getCurrentIndex(), fileBody.getTotalChunks()); } } MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); reqEntity.addPart("userfile_0", fileBody); if (fileName != null && !EncodingUtils .getAsciiString(EncodingUtils.getBytes(fileName, "US-ASCII")).equals(fileName)) { reqEntity.addPart("urlencoded_filename", new StringBody(java.net.URLEncoder.encode(fileName, "UTF-8"))); } if (fileBody != null && !fileBody.getFilename().equals(fileBody.getRootFilename())) { reqEntity.addPart("appendto_urlencoded_part", new StringBody(java.net.URLEncoder.encode(fileBody.getRootFilename(), "UTF-8"))); } if (postParameters != null) { Iterator<Map.Entry<String, String>> it = postParameters.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> entry = it.next(); reqEntity.addPart(entry.getKey(), new StringBody(entry.getValue())); } } if (uploadListener != null) { CountingMultipartRequestEntity countingEntity = new CountingMultipartRequestEntity( reqEntity, uploadListener); ((HttpPost) request).setEntity(countingEntity); } else { ((HttpPost) request).setEntity(reqEntity); } } else { List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(postParameters.size()); Iterator<Map.Entry<String, String>> it = postParameters.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> entry = it.next(); nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } ((HttpPost) request).setEntity(new UrlEncodedFormEntity(nameValuePairs)); } } else { request = new HttpGet(); } request.setURI(uri); if (this.httpUser.length() > 0 && this.httpPassword.length() > 0) { request.addHeader("Ajxp-Force-Login", "true"); } response = httpClient.executeInContext(request); if (isAuthenticationRequested(response)) { sendMessageToHandler(MessageListener.MESSAGE_WHAT_STATE, STATUS_REFRESHING_AUTH); this.discardResponse(response); this.authenticate(); if (loginStateChanged) { // RELOAD loginStateChanged = false; sendMessageToHandler(MessageListener.MESSAGE_WHAT_STATE, STATUS_LOADING_DATA); if (fileBody != null) fileBody.resetChunkIndex(); return this.issueRequest(originalUri, postParameters, file, fileName, fileBody); } } else if (fileBody != null && fileBody.isChunked() && !fileBody.allChunksUploaded()) { this.discardResponse(response); this.issueRequest(originalUri, postParameters, file, fileName, fileBody); } } catch (ClientProtocolException e) { sendMessageToHandler(MessageListener.MESSAGE_WHAT_ERROR, e.getMessage()); e.printStackTrace(); } catch (IOException e) { sendMessageToHandler(MessageListener.MESSAGE_WHAT_ERROR, e.getMessage()); e.printStackTrace(); } catch (AuthenticationException e) { sendMessageToHandler(MessageListener.MESSAGE_WHAT_ERROR, e.getMessage()); throw e; } catch (Exception e) { sendMessageToHandler(MessageListener.MESSAGE_WHAT_ERROR, e.getMessage()); e.printStackTrace(); } finally { uploadListener = null; } return response; }
From source file:info.ajaxplorer.client.model.Node.java
public String getPath() { if (path == null) return ""; byte bytes[] = EncodingUtils.getBytes(path, "utf-8"); return new String(bytes); }
From source file:info.ajaxplorer.client.model.Node.java
public String getPath(boolean skipEncoding) { if (skipEncoding) return path; if (path == null) return ""; byte bytes[] = EncodingUtils.getBytes(path, "utf-8"); return new String(bytes); }
From source file:edu.ucsb.eucalyptus.transport.http.Axis2HttpWorker.java
private void handleServicesList(final AxisHttpResponse response, final ConfigurationContext configurationContext) throws IOException { String s = HTTPTransportReceiver.getServicesHTML(configurationContext); response.setStatus(HttpStatus.SC_OK); response.setContentType("text/html"); OutputStream out = response.getOutputStream(); out.write(EncodingUtils.getBytes(s, HTTP.ISO_8859_1)); }
From source file:info.ajaxplorer.client.model.Node.java
public String getIsoLabel() { byte bytes[] = EncodingUtils.getBytes(label, "iso-8859-1"); return new String(bytes); }
From source file:edu.ucsb.eucalyptus.transport.http.Axis2HttpWorker.java
private void handleWSDL(final AxisHttpResponse response) throws IOException { String s = "A sensible response."; response.setStatus(HttpStatus.SC_OK); response.setContentType("text/html"); OutputStream out = response.getOutputStream(); out.write(EncodingUtils.getBytes(s, HTTP.ISO_8859_1)); }
From source file:edu.ucsb.eucalyptus.transport.http.Axis2HttpWorker.java
private boolean handleMetaData(final AxisHttpResponse response, final MessageContext msgContext, final String uri) { try {//from w ww. j av a 2s . c om String newUri = null; if (uri.startsWith("/latest/")) newUri = uri.replaceAll("/latest/", msgContext.getProperty(MessageContext.REMOTE_ADDR) + ":"); else newUri = uri.replaceAll("/\\d\\d\\d\\d-\\d\\d-\\d\\d/", msgContext.getProperty(MessageContext.REMOTE_ADDR) + ":"); LOG.debug("Metadata request: " + newUri); Object reply = Messaging.send("vm://VmMetadata", newUri); if (!(reply instanceof NullPayload)) { response.setStatus(HttpStatus.SC_OK); response.setContentType("text/html"); OutputStream out = response.getOutputStream(); out.write(EncodingUtils.getBytes(((String) reply), HTTP.ISO_8859_1)); } else response.setStatus(HttpStatus.SC_NOT_FOUND); return true; } catch (Exception e) { LOG.error(e, e); } return false; }
From source file:org.odk.collect.android.utilities.EnhancedDigestScheme.java
/** * Creates digest-response header as defined in RFC2617. * //from ww w . ja v a2s.c o m * @param credentials * User credentials * * @return The digest-response as String. */ private Header createDigestHeader(final Credentials credentials) throws AuthenticationException { String uri = getParameter("uri"); String realm = getParameter("realm"); String nonce = getParameter("nonce"); String opaque = getParameter("opaque"); String method = getParameter("methodname"); String algorithm = getParameter("algorithm"); if (uri == null) { throw new IllegalStateException("URI may not be null"); } if (realm == null) { throw new IllegalStateException("Realm may not be null"); } if (nonce == null) { throw new IllegalStateException("Nonce may not be null"); } // TODO: add support for QOP_INT int qop = QOP_UNKNOWN; String qoplist = getParameter("qop"); if (qoplist != null) { StringTokenizer tok = new StringTokenizer(qoplist, ","); while (tok.hasMoreTokens()) { String variant = tok.nextToken().trim(); if (variant.equals("auth")) { qop = QOP_AUTH; break; } } } else { qop = QOP_MISSING; } if (qop == QOP_UNKNOWN) { throw new AuthenticationException("None of the qop methods is supported: " + qoplist); } // If an algorithm is not specified, default to MD5. if (algorithm == null) { algorithm = "MD5"; } // If an charset is not specified, default to ISO-8859-1. String charset = getParameter("charset"); if (charset == null) { charset = "ISO-8859-1"; } String digAlg = algorithm; if (digAlg.equalsIgnoreCase("MD5-sess")) { digAlg = "MD5"; } MessageDigest digester; try { digester = createMessageDigest(digAlg); } catch (UnsupportedDigestAlgorithmException ex) { throw new AuthenticationException("Unsuppported digest algorithm: " + digAlg); } String uname = credentials.getUserPrincipal().getName(); String pwd = credentials.getPassword(); if (nonce.equals(this.lastNonce)) { nounceCount++; } else { nounceCount = 1; cnonce = null; lastNonce = nonce; } StringBuilder sb = new StringBuilder(256); Formatter formatter = new Formatter(sb, Locale.US); formatter.format("%08x", nounceCount); String nc = sb.toString(); if (cnonce == null) { cnonce = createCnonce(); } a1 = null; a2 = null; // 3.2.2.2: Calculating digest if (algorithm.equalsIgnoreCase("MD5-sess")) { // H( unq(username-value) ":" unq(realm-value) ":" passwd ) // ":" unq(nonce-value) // ":" unq(cnonce-value) // calculated one per session sb.setLength(0); sb.append(uname).append(':').append(realm).append(':').append(pwd); String checksum = encode(digester.digest(EncodingUtils.getBytes(sb.toString(), charset))); sb.setLength(0); sb.append(checksum).append(':').append(nonce).append(':').append(cnonce); a1 = sb.toString(); } else { // unq(username-value) ":" unq(realm-value) ":" passwd sb.setLength(0); sb.append(uname).append(':').append(realm).append(':').append(pwd); a1 = sb.toString(); } String hasha1 = encode(digester.digest(EncodingUtils.getBytes(a1, charset))); if (qop == QOP_AUTH) { // Method ":" digest-uri-value a2 = method + ':' + uri; } else if (qop == QOP_AUTH_INT) { // Method ":" digest-uri-value ":" H(entity-body) // TODO: calculate entity hash if entity is repeatable throw new AuthenticationException("qop-int method is not suppported"); } else { a2 = method + ':' + uri; } String hasha2 = encode(digester.digest(EncodingUtils.getBytes(a2, charset))); // 3.2.2.1 String digestValue; if (qop == QOP_MISSING) { sb.setLength(0); sb.append(hasha1).append(':').append(nonce).append(':').append(hasha2); digestValue = sb.toString(); } else { sb.setLength(0); sb.append(hasha1).append(':').append(nonce).append(':').append(nc).append(':').append(cnonce) .append(':').append(qop == QOP_AUTH_INT ? "auth-int" : "auth").append(':').append(hasha2); digestValue = sb.toString(); } String digest = encode(digester.digest(EncodingUtils.getAsciiBytes(digestValue))); CharArrayBuffer buffer = new CharArrayBuffer(128); if (isProxy()) { buffer.append(AUTH.PROXY_AUTH_RESP); } else { buffer.append(AUTH.WWW_AUTH_RESP); } buffer.append(": Digest "); List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>(20); params.add(new BasicNameValuePair("username", uname)); params.add(new BasicNameValuePair("realm", realm)); params.add(new BasicNameValuePair("nonce", nonce)); params.add(new BasicNameValuePair("uri", uri)); params.add(new BasicNameValuePair("response", digest)); if (qop != QOP_MISSING) { params.add(new BasicNameValuePair("qop", qop == QOP_AUTH_INT ? "auth-int" : "auth")); params.add(new BasicNameValuePair("nc", nc)); params.add(new BasicNameValuePair("cnonce", cnonce)); } if (algorithm != null) { params.add(new BasicNameValuePair("algorithm", algorithm)); } if (opaque != null) { params.add(new BasicNameValuePair("opaque", opaque)); } for (int i = 0; i < params.size(); i++) { BasicNameValuePair param = params.get(i); if (i > 0) { buffer.append(", "); } boolean noQuotes = "nc".equals(param.getName()) || "qop".equals(param.getName()); BasicHeaderValueFormatter.DEFAULT.formatNameValuePair(buffer, param, !noQuotes); } return new BufferedHeader(buffer); }