public static String decode(String s) { try { return URLDecoder.decode(s, UTF_8.name()); } catch (UnsupportedEncodingException e) { Logger.getLogger(BasicServlet.class).fatal(UTF_8.name() + " is not a recognized encoding", e); throw new AssertionError(e); // can't happen with UTF-8 } }
protected void assignContent(HttpEntityEnclosingRequestBase request, Map<String, String> params) throws UnsupportedEncodingException { if (params != null && !params.isEmpty()) { List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size()); for (Map.Entry<String, String> param : params.entrySet()) pairs.add(new BasicNameValuePair(param.getKey(), param.getValue())); StringEntity entity = new StringEntity(URLEncodedUtils.format(pairs, UTF_8.name()), UTF_8.name()); entity.setContentType(CONTENT_TYPE_WWW_FORM_URLENCODED); request.setEntity(entity); } }
public String readFully() { StringBuilder stringBuilder = new StringBuilder(); while (hasNext()) { stringBuilder.append(UTF_8.decode(next().content())); } return stringBuilder.toString(); }
@Override public String format( String projectName, String path, String revision, String abbrRev, ConfigSection cfg, InputStream raw) throws IOException { // Docx4J tries to load some resources dynamically. This fails if the Gerrit // core classloader is used since it doesn't see the resources that are // contained in the plugin jar. To make the resource loading work we // must set a context classloader on the current thread. ClassLoader loader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); WordprocessingMLPackage p = Docx4J.load(raw); HTMLSettings htmlSettings = Docx4J.createHTMLSettings(); htmlSettings.setWmlPackage(p); Docx4jProperties.setProperty("docx4j.Convert.Out.HTML.OutputMethodXML", true); try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { Docx4J.toHTML(htmlSettings, out, Docx4J.FLAG_EXPORT_PREFER_XSL); String html = out.toString(UTF_8.name()); return util.applyCss(html, NAME, projectName); } } catch (Docx4JException e) { throw new IOException(e); } finally { Thread.currentThread().setContextClassLoader(loader); } }
private String createQuery(String url, Map<String, String> params) throws QiitaException { StringBuilder sb = new StringBuilder(url); if (url.contains("?")) { sb.append('&'); } else { sb.append('?'); } try { for (Entry<String, String> entry : params.entrySet()) { String key = URLEncoder.encode(entry.getKey(), UTF_8.name()); String value = URLEncoder.encode(entry.getValue(), UTF_8.name()); sb.append(key).append('=').append(value).append('&'); } } catch (UnsupportedEncodingException e) { throw new QiitaException(e); } return sb.delete(sb.length() - 1, sb.length()).toString(); }
// FIXME Get a 302 in France... @Test(groups = "online", enabled = false) public void testUrlRequestParametersEncoding() throws Exception { try (AsyncHttpClient client = asyncHttpClient()) { String requestUrl2 = URL + URLEncoder.encode(REQUEST_PARAM, UTF_8.name()); logger.info(String.format("Executing request [%s] ...", requestUrl2)); Response response = client.prepareGet(requestUrl2).execute().get(); assertEquals(response.getStatusCode(), 302); } }
@Test public void listCacheNamesTextList() throws Exception { RestResponse r = adminSession.get("/config/server/caches/?format=TEXT_LIST"); assertThat(r.getStatusCode()).isEqualTo(HttpStatus.SC_OK); String result = new String(Base64.decode(r.getEntityContent()), UTF_8.name()); List<String> list = Arrays.asList(result.split("\n")); assertThat(list).contains("accounts"); assertThat(list).contains("projects"); assertThat(Ordering.natural().isOrdered(list)).isTrue(); }
protected String readLogFully(final ClientResponse logs) throws IOException { final LogReader logReader = new LogReader(logs.getEntityInputStream()); StringBuilder stringBuilder = new StringBuilder(); LogMessage logMessage; while ((logMessage = logReader.nextMessage()) != null) { stringBuilder.append(UTF_8.decode(logMessage.content())); } logReader.close(); return stringBuilder.toString(); }
private HttpGet createGetRequest(String urlStr, Map<String, String> params) { StringBuilder bld = new StringBuilder(createURI(urlStr)); if (params != null && !params.isEmpty()) { List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>(); for (Map.Entry<String, String> param : params.entrySet()) pairs.add(new BasicNameValuePair(param.getKey(), param.getValue())); bld.append('?'); bld.append(URLEncodedUtils.format(pairs, UTF_8.name())); } return new HttpGet(URI.create(bld.toString())); }
private void setFormValue(HttpEntityEnclosingRequestBase request, Map<String, String> params) throws QiitaException { List<NameValuePair> nameValuePairs = new ArrayList<>(); for (Entry<String, String> entry : params.entrySet()) { nameValuePairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } try { request.setEntity(new UrlEncodedFormEntity(nameValuePairs, UTF_8.name())); } catch (UnsupportedEncodingException e) { throw new QiitaException(e); } }
@Description("unescape a URL-encoded string") @ScalarFunction @LiteralParameters("x") @SqlType("varchar(x)") public static Slice urlDecode(@SqlType("varchar(x)") Slice value) { try { return slice(URLDecoder.decode(value.toStringUtf8(), UTF_8.name())); } catch (UnsupportedEncodingException e) { throw new AssertionError(e); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, e); } }
private void showDoc( HttpServletRequest req, HttpServletResponse res, GitilesView view, MarkdownConfig cfg, MarkdownToHtml.Builder fmt, MarkdownFile navFile, MarkdownFile srcFile) throws IOException { Map<String, Object> data = new HashMap<>(); Navbar navbar = new Navbar(); if (navFile != null) { navbar.setFormatter(fmt.setFilePath(navFile.path).build()); navbar.setMarkdown(navFile.content); } data.putAll(navbar.toSoyData()); Node doc = GitilesMarkdown.parse(srcFile.content); data.put("pageTitle", pageTitle(doc, srcFile)); if (view.getType() != GitilesView.Type.ROOTED_DOC) { data.put("sourceUrl", GitilesView.show().copyFrom(view).toUrl()); data.put("logUrl", GitilesView.log().copyFrom(view).toUrl()); data.put("blameUrl", GitilesView.blame().copyFrom(view).toUrl()); } if (cfg.analyticsId != null) { data.put("analyticsId", cfg.analyticsId); } data.put("bodyHtml", fmt.setFilePath(srcFile.path).build().toSoyHtml(doc)); String page = renderer.render(SOY_TEMPLATE, data); byte[] raw = page.getBytes(UTF_8); res.setContentType(FormatType.HTML.getMimeType()); res.setCharacterEncoding(UTF_8.name()); setCacheHeaders(res); if (acceptsGzipEncoding(req)) { res.addHeader(HttpHeaders.VARY, HttpHeaders.ACCEPT_ENCODING); res.setHeader(HttpHeaders.CONTENT_ENCODING, "gzip"); raw = gzip(raw); } res.setContentLength(raw.length); res.setStatus(HttpServletResponse.SC_OK); res.getOutputStream().write(raw); }
/** * Execute an HTTP request. * * @return HTTP response object of specified {@code type}. * @throws RetrofitError Thrown if any error occurs during the HTTP request. */ private Object invokeRequest(RestMethodInfo methodDetails, Object[] args) { methodDetails.init(); // Ensure all relevant method information has been loaded. String url = server.getUrl(); try { Request request = new RequestBuilder(converter) // .setApiUrl(server.getUrl()) .setArgs(args) .setHeaders(headers.get()) .setMethodInfo(methodDetails) .build(); url = request.getUrl(); if (!methodDetails.isSynchronous) { // If we are executing asynchronously then update the current thread with a useful name. Thread.currentThread().setName(THREAD_PREFIX + url); } if (LOGGER.isLoggable(Level.FINE)) { logRequest(request); } Object profilerObject = null; if (profiler != null) { profilerObject = profiler.beforeCall(); } long start = System.nanoTime(); Response response = clientProvider.get().execute(request); long elapsedTime = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start); int statusCode = response.getStatus(); if (profiler != null) { RequestInformation requestInfo = getRequestInfo(server, methodDetails, request); profiler.afterCall(requestInfo, elapsedTime, statusCode, profilerObject); } TypedInput body = response.getBody(); if (LOGGER.isLoggable(Level.FINE)) { // Replace the response since the logger needs to consume the entire input stream. body = logResponse(url, response.getStatus(), body, elapsedTime); } List<Header> headers = response.getHeaders(); for (Header header : headers) { if (HTTP.CONTENT_TYPE.equalsIgnoreCase(header.getName()) // && !UTF_8.equalsIgnoreCase(Utils.parseCharset(header.getValue()))) { throw new IOException("Only UTF-8 charset supported."); } } Type type = methodDetails.type; if (statusCode >= 200 && statusCode < 300) { // 2XX == successful request if (type.equals(Response.class)) { return response; } if (body == null) { return null; } try { return converter.fromBody(body, type); } catch (ConversionException e) { throw RetrofitError.conversionError(url, response, converter, type, e); } } throw RetrofitError.httpError(url, response, converter, type); } catch (RetrofitError e) { throw e; // Pass through our own errors. } catch (IOException e) { throw RetrofitError.networkError(url, e); } catch (Throwable t) { throw RetrofitError.unexpectedError(url, t); } }
private static StringEntity jsonStringEntity(String json) { return new StringEntity(json, UTF_8.name()); }
@Override public final RequestLine parse(final ByteBuffer buffer) { // mark the current position of the buffer // if the parse is not complete, we will rewind to the mark()ed position offset = buffer.position(); while (buffer.hasRemaining()) { switch (stateStack.peek()) { case METHOD: byte c = buffer.get(); if (isTokenChar(c)) { // valid char for HTTP method continue; } else if (c == ' ') { int length = buffer.position() - offset; String s = new String(buffer.array(), buffer.arrayOffset() + offset, --length, US_ASCII); this.method = Method.parse(s); offset = buffer.position(); stateStack.pop(); } else { throw new IllegalArgumentException( String.format("Illegal character '%s' in %s", (char) c, stateStack.peek())); } break; case REQUEST_URI: switch (buffer.get()) { case ' ': int length = buffer.position() - offset; String s = new String(buffer.array(), buffer.arrayOffset() + offset, --length, US_ASCII); try { this.url = URLDecoder.decode(s, UTF_8.displayName()); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(e); } offset = buffer.position(); stateStack.pop(); break; default: break; } break; case HTTP_VERSION: final byte p = buffer.get(); switch (p) { case '\r': int length = buffer.position() - offset; String s = new String(buffer.array(), buffer.arrayOffset() + offset, --length, US_ASCII); this.version = Version.parse(s); offset = buffer.position(); stateStack.pop(); break; case 'H': case 'T': case 'P': case '/': case '0': case '1': case '9': case '.': // valid char for HTTP Version break; default: throw new IllegalArgumentException( String.format("Illegal character '%s' in %s", (char) p, stateStack.peek())); } break; case REQUEST_LINE_END: switch (buffer.get()) { case '\n': try { return new RequestLine(method, url, version); } catch (URISyntaxException e) { // TODO Auto-generated catch block e.printStackTrace(); } default: throw new IllegalArgumentException("\\n was not encountered"); } } } buffer.position(offset); return null; }
public void setBody(final String body) { this.body = (body != null) ? body.getBytes(Charset.forName(UTF_8.name())) : null; isBinaryBody = false; }
public String getBody() { return (!isBinaryBody && body != null) ? new String(body, Charset.forName(UTF_8.name())) : null; }
public ResponseDefinition(final int statusCode, final String bodyContent) { this.status = statusCode; this.body = (bodyContent == null) ? null : bodyContent.getBytes(Charset.forName(UTF_8.name())); }
public class ChmConstants { /* Prevents instantiation */ private ChmConstants() {} public static final String DEFAULT_CHARSET = UTF_8.name(); public static final String ITSF = "ITSF"; public static final String ITSP = "ITSP"; public static final String PMGL = "PMGL"; public static final String LZXC = "LZXC"; public static final String CHM_PMGI_MARKER = "PMGI"; public static final int BYTE_ARRAY_LENGHT = 16; public static final int CHM_ITSF_V2_LEN = 0x58; public static final int CHM_ITSF_V3_LEN = 0x60; public static final int CHM_ITSP_V1_LEN = 0x54; public static final int CHM_PMGL_LEN = 0x14; public static final int CHM_PMGI_LEN = 0x08; public static final int CHM_LZXC_RESETTABLE_V1_LEN = 0x28; public static final int CHM_LZXC_MIN_LEN = 0x18; public static final int CHM_LZXC_V2_LEN = 0x1c; public static final int CHM_SIGNATURE_LEN = 4; public static final int CHM_VER_2 = 2; public static final int CHM_VER_3 = 3; public static final int CHM_VER_1 = 1; public static final int CHM_WINDOW_SIZE_BLOCK = 0x8000; /* my hacking */ public static final int START_PMGL = 0xCC; public static final String CONTROL_DATA = "ControlData"; public static final String RESET_TABLE = "ResetTable"; public static final String CONTENT = "Content"; /* some constants defined by the LZX specification */ public static final int LZX_MIN_MATCH = 2; public static final int LZX_MAX_MATCH = 257; public static final int LZX_NUM_CHARS = 256; public static final int LZX_BLOCKTYPE_INVALID = 0; /* * also blocktypes 4-7 * invalid */ public static final int LZX_BLOCKTYPE_VERBATIM = 1; public static final int LZX_BLOCKTYPE_ALIGNED = 2; public static final int LZX_BLOCKTYPE_UNCOMPRESSED = 3; public static final int LZX_PRETREE_NUM_ELEMENTS_BITS = 4; /* ??? */ public static final int LZX_PRETREE_NUM_ELEMENTS = 20; public static final int LZX_ALIGNED_NUM_ELEMENTS = 8; /* * aligned offset tree * #elements */ public static final int LZX_NUM_PRIMARY_LENGTHS = 7; /* * this one missing * from spec! */ public static final int LZX_NUM_SECONDARY_LENGTHS = 249; /* * length tree * #elements */ /* LZX huffman defines: tweak tablebits as desired */ public static final int LZX_PRETREE_MAXSYMBOLS = LZX_PRETREE_NUM_ELEMENTS; public static final int LZX_PRETREE_TABLEBITS = 6; public static final int LZX_MAINTREE_MAXSYMBOLS = LZX_NUM_CHARS + 50 * 8; public static final int LZX_MAIN_MAXSYMBOLS = LZX_NUM_CHARS * 2; public static final int LZX_MAINTREE_TABLEBITS = 12; public static final int LZX_LENGTH_MAXSYMBOLS = LZX_NUM_SECONDARY_LENGTHS + 1; public static final int LZX_LENGTH_TABLEBITS = 12; public static final int LZX_ALIGNED_MAXSYMBOLS = LZX_ALIGNED_NUM_ELEMENTS; public static final int LZX_ALIGNED_TABLEBITS = 7; public static final int LZX_LENTABLE_SAFETY = 64; public static short[] EXTRA_BITS = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 }; public static int[] POSITION_BASE = { 0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256, 384, 512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12288, 16384, 24576, 32768, 49152, 65536, 98304, 131072, 196608, 262144, 393216, 524288, 655360, 786432, 917504, 1048576, 1179648, 1310720, 1441792, 1572864, 1703936, 1835008, 1966080, 2097152 }; }
public String content() { if (content == null) { return null; } return new String(content, Charset.forName(UTF_8.name())); }