/** * Method returns list of http status codes. * * @param binaryAnnotations zipkin binary annotations * @return http status codes */ public static List<HttpCode> getHttpStatusCodes(List<BinaryAnnotation> binaryAnnotations) { if (binaryAnnotations == null) { return Collections.emptyList(); } List<HttpCode> httpCodes = new ArrayList<>(); for (BinaryAnnotation binaryAnnotation: binaryAnnotations) { if (Constants.ZIPKIN_BIN_ANNOTATION_HTTP_STATUS_CODE.equals(binaryAnnotation.getKey()) && binaryAnnotation.getValue() != null) { String strHttpCode = binaryAnnotation.getValue(); Integer httpCode = toInt(strHttpCode.trim()); if (httpCode != null) { String description = EnglishReasonPhraseCatalog.INSTANCE.getReason(httpCode, Locale.ENGLISH); httpCodes.add(new HttpCode(httpCode, description)); } } } return httpCodes; }
private Response generateResponse(String contentType, int status, byte[] content) { final BasicStatusLine statusLine = new BasicStatusLine( new ProtocolVersion("HTTP", 1, 1), status, EnglishReasonPhraseCatalog.INSTANCE.getReason(status, Locale.ENGLISH)); final BasicHttpResponse httpResponse = new BasicHttpResponse(statusLine); httpResponse.addHeader("Content-Type", contentType); final HttpResponseDecorator httpResponseDecorator = new HttpResponseDecorator(httpResponse, content); final RestAssuredResponseImpl restResponse = new RestAssuredResponseImpl(); restResponse.setStatusCode(status); restResponse.parseResponse( httpResponseDecorator, content, false, new ResponseParserRegistrar() ); return restResponse; }
public void failed(final Exception ex) { synchronized (this.httpExchange) { if (this.completed) { return; } this.completed = true; this.httpExchange.setException(ex); HttpAsyncExchange responseTrigger = this.httpExchange.getResponseTrigger(); if (responseTrigger != null && !responseTrigger.isCompleted()) { System.out.println("[client<-proxy] " + this.httpExchange.getId() + " " + ex); ConsoleFactory.printToConsole("[client<-proxy] " + this.httpExchange.getId() + " " + ex,true); int status = HttpStatus.SC_INTERNAL_SERVER_ERROR; HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_0, status, EnglishReasonPhraseCatalog.INSTANCE.getReason(status, Locale.US)); String message = ex.getMessage(); if (message == null) { message = "Unexpected error"; } response.setEntity(new NStringEntity(message, ContentType.DEFAULT_TEXT)); responseTrigger.submitResponse(new BasicAsyncResponseProducer(response)); } } }
@Test public final void getHttpResponseHeadMethodTest() throws TranslationException, UnsupportedEncodingException { // create the coap response Response coapResponse = new Response(CodeRegistry.RESP_CREATED); String payload = "aaa"; coapResponse.setPayload(payload.getBytes("UTF-8")); // create the http response HttpRequest httpRequest = new BasicHttpRequest("HEAD", "coap://localhost"); // translate the http response HttpResponse httpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, EnglishReasonPhraseCatalog.INSTANCE.getReason(200, Locale.ENGLISH)); HttpTranslator.getHttpResponse(httpRequest, coapResponse, httpResponse); // check assertNotNull(httpResponse); assertTrue(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED); // check the payload assertFalse(httpResponse.containsHeader("content-type")); assertNull(httpResponse.getEntity()); // check the headers assertNotNull(httpResponse.getAllHeaders()); assertEquals(1, httpResponse.getAllHeaders().length); }
/** * <p>buildResponseMethodName.</p> * * @param statusCode a int. * @param mimeType a {@link org.aml.apimodel.MimeType} object. * @return a {@link java.lang.String} object. */ public static String buildResponseMethodName(final int statusCode, final MimeType mimeType) { final String status = EnglishReasonPhraseCatalog.INSTANCE.getReason(statusCode, DEFAULT_LOCALE); String string = getShortMimeType(mimeType) + buildJavaFriendlyName(defaultIfBlank(status, "_" + statusCode)); return "with" + Character.toUpperCase(string.charAt(0))+string.substring(1); }
public static HttpResponse getResponse(int expectedStatus, String expectedBody) { ProtocolVersion version = new ProtocolVersion("HTTP", 1, 1); StatusLine statusLine = new BasicStatusLine(version, expectedStatus, EnglishReasonPhraseCatalog.INSTANCE.getReason(expectedStatus, Locale.US)); HttpResponse response = new BasicHttpResponse(statusLine); response.setEntity(new StringEntity(expectedBody, Charset.forName("UTF-8"))); return response; }
/** * Send simple http response. * * @param httpExchange * the http exchange * @param httpCode * the http code */ private void sendSimpleHttpResponse(HttpAsyncExchange httpExchange, int httpCode) { // get the empty response from the exchange HttpResponse httpResponse = httpExchange.getResponse(); // create and set the status line StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, httpCode, EnglishReasonPhraseCatalog.INSTANCE.getReason(httpCode, Locale.ENGLISH)); httpResponse.setStatusLine(statusLine); // send the error response httpExchange.submitResponse(); }
@Test public final void getCoapResponseNoContentTest() throws TranslationException { // create the response StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_NO_CONTENT, EnglishReasonPhraseCatalog.INSTANCE.getReason(HttpStatus.SC_NO_CONTENT, Locale.ENGLISH)); HttpResponse httpResponse = new BasicHttpResponse(statusLine); // translate the http response Response coapResponse = HttpTranslator.getCoapResponse(httpResponse, new GETRequest()); assertNotNull(coapResponse); assertTrue(coapResponse.getCode() == CodeRegistry.RESP_CHANGED); coapResponse = HttpTranslator.getCoapResponse(httpResponse, new POSTRequest()); assertNotNull(coapResponse); assertTrue(coapResponse.getCode() == CodeRegistry.RESP_CHANGED); coapResponse = HttpTranslator.getCoapResponse(httpResponse, new PUTRequest()); assertNotNull(coapResponse); assertTrue(coapResponse.getCode() == CodeRegistry.RESP_CHANGED); coapResponse = HttpTranslator.getCoapResponse(httpResponse, new DELETERequest()); assertNotNull(coapResponse); assertTrue(coapResponse.getCode() == CodeRegistry.RESP_DELETED); }
@Test public final void getHttpResponseErrorCodeTest() throws TranslationException, IOException { // create the coap response Response coapResponse = new Response(CodeRegistry.RESP_NOT_FOUND); String reason = "custom reason"; coapResponse.setPayload(reason.getBytes("UTF-8")); // create the http response HttpRequest httpRequest = new BasicHttpRequest("GET", "coap://localhost"); // translate the http response HttpResponse httpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, EnglishReasonPhraseCatalog.INSTANCE.getReason(200, Locale.ENGLISH)); HttpTranslator.getHttpResponse(httpRequest, coapResponse, httpResponse); // check assertNotNull(httpResponse); assertTrue(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_NOT_FOUND); // check the payload assertEquals(ContentType.TEXT_PLAIN.toString().toLowerCase(), httpResponse.getFirstHeader("content-type").getValue().toLowerCase()); byte[] byteArrayActual = getByteArray(httpResponse.getEntity().getContent()); byte[] bytesExpected = reason.getBytes("ISO-8859-1"); assertArrayEquals(bytesExpected, byteArrayActual); // check the headers assertNotNull(httpResponse.getAllHeaders()); assertEquals(2, httpResponse.getAllHeaders().length); }
/** * Test method for * {@link ch.ethz.inf.vs.californium.util.HttpTranslator#getHttpResponse(ch.ethz.inf.vs.californium.coap.Response, org.apache.http.HttpResponse)} * . * * @throws IOException */ @Test public final void getHttpResponseTest() throws TranslationException, IOException { // create the coap response Response coapResponse = new Response(CodeRegistry.RESP_CREATED); String payload = "aaa"; coapResponse.setPayload(payload.getBytes("UTF-8")); coapResponse.setContentType(MediaTypeRegistry.TEXT_PLAIN); String etag = "254636899"; coapResponse.setOption(new Option(etag, OptionNumberRegistry.ETAG)); // create the http response HttpRequest httpRequest = new BasicHttpRequest("POST", "coap://localhost"); // translate the http response HttpResponse httpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, EnglishReasonPhraseCatalog.INSTANCE.getReason(200, Locale.ENGLISH)); HttpTranslator.getHttpResponse(httpRequest, coapResponse, httpResponse); // check assertNotNull(httpResponse); assertTrue(httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_CREATED); // check the payload assertEquals(ContentType.TEXT_PLAIN.toString().toLowerCase(), httpResponse.getFirstHeader("content-type").getValue().toLowerCase()); byte[] byteArrayActual = getByteArray(httpResponse.getEntity().getContent()); byte[] bytesExpected = payload.getBytes("ISO-8859-1"); assertArrayEquals(bytesExpected, byteArrayActual); // check the headers assertNotNull(httpResponse.getAllHeaders()); assertEquals(3, httpResponse.getAllHeaders().length); assertEquals(etag, httpResponse.getFirstHeader("etag").getValue().toLowerCase()); }
@Test(expected = TranslationException.class) public final void getHttpResponseWrongCodeTest() throws TranslationException { // create the coap response Response coapResponse = new Response(CodeRegistry.EMPTY_MESSAGE); // create the http response HttpRequest httpRequest = new BasicHttpRequest("POST", "coap://localhost"); // translate the http response HttpResponse httpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, EnglishReasonPhraseCatalog.INSTANCE.getReason(200, Locale.ENGLISH)); HttpTranslator.getHttpResponse(httpRequest, coapResponse, httpResponse); }
/** * Test method for * {@link ch.ethz.inf.vs.californium.util.HttpTranslator#getCoapResponse(org.apache.http.HttpResponse)} * . * * @throws IllegalAccessException * @throws TranslationException */ @Test public final void getCoapResponseTest() throws IllegalAccessException, TranslationException { for (Field field : HttpStatus.class.getDeclaredFields()) { // get the code int httpCode = field.getInt(null); // if(http) // create the response StatusLine statusLine = new BasicStatusLine(HttpVersion.HTTP_1_1, httpCode, EnglishReasonPhraseCatalog.INSTANCE.getReason(httpCode, Locale.ENGLISH)); HttpResponse httpResponse = new BasicHttpResponse(statusLine); // create the entity String contentString = "aaa"; HttpEntity httpEntity = new ByteArrayEntity(contentString.getBytes(Charset.forName("ISO_8859_1")), ContentType.TEXT_PLAIN); httpResponse.setEntity(httpEntity); // set the content-type httpResponse.setHeader("content-type", "text/plain; charset=iso-8859-1"); // create the header String headerName = "if-match"; String headerValue = "\"737060cd8c284d8af7ad3082f209582d\""; Header header = new BasicHeader(headerName, headerValue); httpResponse.addHeader(header); // translate the http response Response coapResponse = HttpTranslator.getCoapResponse(httpResponse, new GETRequest()); assertNotNull(coapResponse); // check the payload assertNotNull(coapResponse.getPayload()); assertArrayEquals(contentString.getBytes(Charset.forName("UTF-8")), coapResponse.getPayload()); // check the option assertFalse(coapResponse.getOptions().isEmpty()); int optionNumber = Integer.parseInt(HttpTranslator.HTTP_TRANSLATION_PROPERTIES.getProperty("http.message.header." + headerName)); assertEquals(coapResponse.getFirstOption(optionNumber).getStringValue(), headerValue); // check the content-type assertEquals(coapResponse.getContentType(), MediaTypeRegistry.TEXT_PLAIN); } }
@Test(expected = IllegalArgumentException.class) public final void getHttpResponseNullTest() throws TranslationException { HttpResponse httpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, EnglishReasonPhraseCatalog.INSTANCE.getReason(200, Locale.ENGLISH)); HttpTranslator.getHttpResponse(null, new Response(0), httpResponse); }
@Test(expected = IllegalArgumentException.class) public final void getHttpResponseNullTest2() throws TranslationException { HttpResponse httpResponse = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, EnglishReasonPhraseCatalog.INSTANCE.getReason(200, Locale.ENGLISH)); HttpTranslator.getHttpResponse(new BasicHttpRequest("get", "http://localhost"), null, httpResponse); }
/** * Creates a new response factory with the default catalog. * The default catalog is {@link EnglishReasonPhraseCatalog}. */ public DefaultHttpResponseFactory() { this(EnglishReasonPhraseCatalog.INSTANCE); }