use of java.util.LinkedHashMap in project okhttp by square.
the class JavaApiConverterTest method createOkRequest_nonNullRequestHeaders.
@Test
public void createOkRequest_nonNullRequestHeaders() throws Exception {
URI uri = new URI("https://foo/bar");
Map<String, List<String>> javaRequestHeaders = new LinkedHashMap<>();
javaRequestHeaders.put("Foo", Arrays.asList("Bar"));
Request request = JavaApiConverter.createOkRequest(uri, "POST", javaRequestHeaders);
assertTrue(request.isHttps());
assertEquals(uri, request.url().uri());
Headers okRequestHeaders = request.headers();
assertEquals(1, okRequestHeaders.size());
assertEquals("Bar", okRequestHeaders.get("Foo"));
assertEquals("POST", request.method());
}
use of java.util.LinkedHashMap in project okhttp by square.
the class JavaApiConverterTest method createOkResponseForCacheGet_secure.
@Test
public void createOkResponseForCacheGet_secure() throws Exception {
final String statusLine = "HTTP/1.1 200 Fantastic";
final Principal localPrincipal = LOCAL_CERT.getSubjectX500Principal();
final List<Certificate> localCertificates = Arrays.<Certificate>asList(LOCAL_CERT);
final Principal serverPrincipal = SERVER_CERT.getSubjectX500Principal();
final List<Certificate> serverCertificates = Arrays.<Certificate>asList(SERVER_CERT);
URI uri = new URI("https://foo/bar");
Request request = new Request.Builder().url(uri.toURL()).build();
SecureCacheResponse cacheResponse = new SecureCacheResponse() {
@Override
public Map<String, List<String>> getHeaders() throws IOException {
Map<String, List<String>> headers = new LinkedHashMap<>();
headers.put(null, Collections.singletonList(statusLine));
headers.put("xyzzy", Arrays.asList("bar", "baz"));
return headers;
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("HelloWorld".getBytes(StandardCharsets.UTF_8));
}
@Override
public String getCipherSuite() {
return "SSL_RSA_WITH_NULL_MD5";
}
@Override
public List<Certificate> getLocalCertificateChain() {
return localCertificates;
}
@Override
public List<Certificate> getServerCertificateChain() throws SSLPeerUnverifiedException {
return serverCertificates;
}
@Override
public Principal getPeerPrincipal() throws SSLPeerUnverifiedException {
return serverPrincipal;
}
@Override
public Principal getLocalPrincipal() {
return localPrincipal;
}
};
Response response = JavaApiConverter.createOkResponseForCacheGet(request, cacheResponse);
Request cacheRequest = response.request();
assertEquals(request.url(), cacheRequest.url());
assertEquals(request.method(), cacheRequest.method());
assertEquals(0, request.headers().size());
assertEquals(Protocol.HTTP_1_1, response.protocol());
assertEquals(200, response.code());
assertEquals("Fantastic", response.message());
Headers okResponseHeaders = response.headers();
assertEquals("baz", okResponseHeaders.get("xyzzy"));
assertEquals("HelloWorld", response.body().string());
Handshake handshake = response.handshake();
assertNotNull(handshake);
assertNotNullAndEquals(CipherSuite.TLS_RSA_WITH_NULL_MD5, handshake.cipherSuite());
assertEquals(localPrincipal, handshake.localPrincipal());
assertEquals(serverPrincipal, handshake.peerPrincipal());
assertEquals(serverCertificates, handshake.peerCertificates());
assertEquals(localCertificates, handshake.localCertificates());
}
use of java.util.LinkedHashMap in project okhttp by square.
the class JavaApiConverterTest method createOkResponseForCacheGet_withMissingStatusLine.
/** Test for https://code.google.com/p/android/issues/detail?id=160522 */
@Test
public void createOkResponseForCacheGet_withMissingStatusLine() throws Exception {
URI uri = new URI("http://foo/bar");
Request request = new Request.Builder().url(uri.toURL()).build();
CacheResponse cacheResponse = new CacheResponse() {
@Override
public Map<String, List<String>> getHeaders() throws IOException {
Map<String, List<String>> headers = new LinkedHashMap<>();
// Headers is deliberately missing an entry with a null key.
headers.put("xyzzy", Arrays.asList("bar", "baz"));
return headers;
}
@Override
public InputStream getBody() throws IOException {
// Should never be called
return null;
}
};
try {
JavaApiConverter.createOkResponseForCacheGet(request, cacheResponse);
fail();
} catch (IOException expected) {
}
}
use of java.util.LinkedHashMap in project okhttp by square.
the class JavaApiConverterTest method createOkResponseForCacheGet.
@Test
public void createOkResponseForCacheGet() throws Exception {
final String statusLine = "HTTP/1.1 200 Fantastic";
URI uri = new URI("http://foo/bar");
Request request = new Request.Builder().url(uri.toURL()).build();
CacheResponse cacheResponse = new CacheResponse() {
@Override
public Map<String, List<String>> getHeaders() throws IOException {
Map<String, List<String>> headers = new LinkedHashMap<>();
headers.put(null, Collections.singletonList(statusLine));
headers.put("xyzzy", Arrays.asList("bar", "baz"));
return headers;
}
@Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("HelloWorld".getBytes(StandardCharsets.UTF_8));
}
};
Response response = JavaApiConverter.createOkResponseForCacheGet(request, cacheResponse);
Request cacheRequest = response.request();
assertEquals(request.url(), cacheRequest.url());
assertEquals(request.method(), cacheRequest.method());
assertEquals(0, request.headers().size());
assertEquals(Protocol.HTTP_1_1, response.protocol());
assertEquals(200, response.code());
assertEquals("Fantastic", response.message());
Headers okResponseHeaders = response.headers();
assertEquals("baz", okResponseHeaders.get("xyzzy"));
assertEquals("HelloWorld", response.body().string());
assertNull(response.handshake());
}
use of java.util.LinkedHashMap in project CoreNLP by stanfordnlp.
the class CoreNLPServlet method outputVisualise.
public void outputVisualise(PrintWriter out, Annotation annotation) throws ServletException, IOException {
// Note: A lot of the HTML generation in this method could/should be
// done at a templating level, but as-of-yet I am not entirely sure how
// this should be done in jsp. Also, a lot of the HTML is unnecessary
// for the other outputs such as pretty print and XML.
// Div for potential error messages when fetching the configuration.
out.println("<div id=\"config_error\">");
out.println("</div>");
// Insert divs that will be used for each visualisation type.
final int visualiserDivPxWidth = 700;
Map<String, String> nameByAbbrv = new LinkedHashMap<>();
nameByAbbrv.put("pos", "Part-of-Speech");
nameByAbbrv.put("ner", "Named Entity Recognition");
nameByAbbrv.put("coref", "Coreference");
nameByAbbrv.put("basic_dep", "Basic Dependencies");
//nameByAbbrv.put("collapsed_dep", "Collapsed dependencies");
nameByAbbrv.put("collapsed_ccproc_dep", "Enhanced Dependencies");
for (Map.Entry<String, String> entry : nameByAbbrv.entrySet()) {
out.println("<h2>" + entry.getValue() + ":</h2>");
out.println("<div id=\"" + entry.getKey() + "\" style=\"width:" + visualiserDivPxWidth + "px\">");
out.println(" <div id=\"" + entry.getKey() + "_loading\">");
out.println(" <p>Loading...</p>");
out.println(" </div>");
out.println("</div>");
out.println("");
}
// Time to get the XML data into HTML.
StringWriter xmlOutput = new StringWriter();
pipeline.xmlPrint(annotation, xmlOutput);
xmlOutput.flush();
// Escape the XML to be embeddable into a Javascript string.
String escapedXml = xmlOutput.toString().replaceAll("\\r\\n|\\r|\\n", "").replace("\"", "\\\"");
// Inject the XML results into the HTML to be retrieved by the Javascript.
out.println("<script type=\"text/javascript\">");
out.println("// <![CDATA[");
out.println(" stanfordXML = \"" + escapedXml + "\";");
out.println("// ]]>");
out.println("</script>");
// Relative brat installation location to CoreNLP.
final String bratLocation = "../brat";
// Inject the location variable, we need it in Javascript mode.
out.println("<script type=\"text/javascript\">");
out.println("// <![CDATA[");
out.println(" bratLocation = \"" + bratLocation + "\";");
out.println(" webFontURLs = [\n" + " '" + bratLocation + "/static/fonts/Astloch-Bold.ttf',\n" + " '" + bratLocation + "/static/fonts/PT_Sans-Caption-Web-Regular.ttf',\n" + " '" + bratLocation + "/static/fonts/Liberation_Sans-Regular.ttf'];");
out.println("// ]]>");
out.println("</script>");
// Inject the brat stylesheet (removing this line breaks visualisation).
out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"" + bratLocation + "/style-vis.css\"/>");
// Include the Javascript libraries necessary to run brat.
out.println("<script type=\"text/javascript\" src=\"" + bratLocation + "/client/lib/head.load.min.js\"></script>");
// Main Javascript that hooks into all that we have introduced so far.
out.println("<script type=\"text/javascript\" src=\"brat.js\"></script>");
// Link to brat, I hope this is okay to have here...
out.println("<h>Visualisation provided using the " + "<a href=\"http://brat.nlplab.org/\">brat " + "visualisation/annotation software</a>.</h>");
out.println("<br/>");
}
Aggregations