List of usage examples for org.apache.http.message BasicHttpResponse addHeader
public void addHeader(String str, String str2)
From source file:org.esigate.test.http.HttpResponseBuilder.java
/** * Build the HTTP response using all data previously set on this builder and/or use defaults. * //from w w w . ja va 2s.c o m * @return The HTTP response */ public CloseableHttpResponse build() { BasicHttpResponse response = new BasicHttpResponse(this.protocolVersion, this.status, this.reason); for (Header h : this.headers) { response.addHeader(h.getName(), h.getValue()); } if (this.entity != null) { response.setEntity(this.entity); } return BasicCloseableHttpResponse.adapt(response); }
From source file:com.telefonica.iot.cygnus.backends.hdfs.HDFSBackendImplRESTTest.java
/** * Sets up tests by creating a unique instance of the tested class, and by defining the behaviour of the mocked * classes./*www. ja va2s . c om*/ * * @throws Exception */ @Before public void setUp() throws Exception { // set up the instance of the tested class backend = new HDFSBackendImplREST(hdfsHost, hdfsPort, user, password, token, hiveServerVersion, hiveHost, hivePort, false, null, null, null, null, false, maxConns, maxConnsPerRoute); // set up other instances BasicHttpResponse resp200 = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 200, "OK"); resp200.addHeader("Content-Type", "application/json"); BasicHttpResponse resp201 = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 201, "Created"); resp201.addHeader("Content-Type", "application/json"); BasicHttpResponse resp307 = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 307, "Temporary Redirect"); resp307.addHeader("Content-Type", "application/json"); resp307.addHeader(new BasicHeader("Location", "http://localhost:14000/")); // set up the behaviour of the mocked classes when(mockHttpClientExistsCreateDir.execute(Mockito.any(HttpUriRequest.class))).thenReturn(resp200); when(mockHttpClientCreateFile.execute(Mockito.any(HttpUriRequest.class))).thenReturn(resp307, resp201); when(mockHttpClientAppend.execute(Mockito.any(HttpUriRequest.class))).thenReturn(resp307, resp200); }
From source file:org.syncope.console.commons.HttpResourceStream.java
private HttpResponse buildFakeResponse(final String errorMessage) { ByteArrayInputStream bais = new ByteArrayInputStream(new byte[0]); BasicHttpEntity entity = new BasicHttpEntity(); entity.setContent(bais);//from w w w .jav a 2 s . co m entity.setContentLength(0); entity.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); BasicHttpResponse response = new BasicHttpResponse(new ProtocolVersion("HTTP", 1, 1), 400, "Exception: " + errorMessage); response.setEntity(entity); response.addHeader("Content-Disposition", "attachment; filename=error"); return response; }
From source file:org.callimachusproject.client.HttpClientFactoryTest.java
@Test public void testCookieStored() throws Exception { CookieStore cookieStore = new BasicCookieStore(); do {/* w ww . j a v a2s. co m*/ HttpGet get = new HttpGet("http://example.com/setcookie"); HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); get.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); BasicHttpResponse setcookie = new BasicHttpResponse(_200); setcookie.addHeader("Set-Cookie", "oat=meal"); setcookie.addHeader("Cache-Control", "no-store"); responses.add(setcookie); client.execute(get, new ResponseHandler<Void>() { public Void handleResponse(HttpResponse response) throws ClientProtocolException, IOException { assertEquals(_200.getStatusCode(), response.getStatusLine().getStatusCode()); assertTrue(response.containsHeader("Set-Cookie")); return null; } }, localContext); } while (false); do { HttpGet get = new HttpGet("http://example.com/getcookie"); HttpContext localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); get.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.RFC_2109); BasicHttpResponse getcookie = new BasicHttpResponse(_200); responses.add(getcookie); client.execute(get, new ResponseHandler<Void>() { public Void handleResponse(HttpResponse response) throws ClientProtocolException, IOException { assertEquals(_200.getStatusCode(), response.getStatusLine().getStatusCode()); assertContains("oat=meal", asString(response.getEntity())); return null; } }, localContext); } while (false); }
From source file:org.callimachusproject.auth.DigestPasswordAccessor.java
public HttpResponse getLogoutResponse() { BasicHttpResponse resp = new BasicHttpResponse(_204); resp.addHeader("Set-Cookie", digestNonce + ";Max-Age=0;Path=/;HttpOnly" + digestNonceSecure); return resp;/*w w w.j a va 2s . c o m*/ }
From source file:org.callimachusproject.auth.DigestPasswordAccessor.java
public HttpResponse getPersistentLogin(String iri) throws OpenRDFException, IOException { DetachedRealm realm = realms.getRealm(iri); if (realm == null) return null; String secret = realm.getOriginSecret(); String nonce = nextNonce();/* w ww . j a va 2s. co m*/ BasicHttpResponse resp = new BasicHttpResponse(_200); resp.addHeader("Set-Cookie", digestNonce + nonce + ";Max-Age=" + THREE_MONTHS + ";Path=/;HttpOnly" + digestNonceSecure); resp.addHeader("Cache-Control", "private"); resp.setHeader("Content-Type", "text/plain;charset=UTF-8"); String hash = md5(nonce + ":" + secret); resp.setEntity(new StringEntity(hash, Charset.forName("UTF-8"))); return resp; }
From source file:org.callimachusproject.auth.CookieAuthenticationManager.java
private HttpResponse getLogoutResponse() { BasicHttpResponse resp = new BasicHttpResponse(_204); resp.addHeader("Set-Cookie", sid + ";Max-Age=0;HttpOnly;Path=" + protectedPath + secureCookie); return resp;//w w w . j ava2 s . c o m }
From source file:com.baasbox.android.net.OkClient.java
@Override public HttpResponse execute(HttpRequest request) throws BaasException { String contentType = request.headers.get("Content-Type"); Request.Builder okRequestBuilder = new Request.Builder(); boolean contentLengthSet = false; for (String name : request.headers.keySet()) { if (!contentLengthSet && "Content-Length".equals(name)) { contentLengthSet = true;/*from w w w .ja v a 2s . c o m*/ } okRequestBuilder.addHeader(name, request.headers.get(name)); } if (!contentLengthSet) { okRequestBuilder.addHeader("Content-Length", "0"); } RequestBody rb; switch (request.method) { case HttpRequest.GET: okRequestBuilder.get(); break; case HttpRequest.POST: rb = buildBody(contentType, request.body); //InputRequestBody rb = new InputRequestBody(contentType,request.body); okRequestBuilder.post(rb); break; case HttpRequest.PUT: rb = buildBody(contentType, request.body); okRequestBuilder.put(rb); break; case HttpRequest.DELETE: okRequestBuilder.delete(); break; case HttpRequest.PATCH: rb = buildBody(contentType, request.body); okRequestBuilder.patch(rb); break; } okRequestBuilder.url(request.url); Request okRequest = okRequestBuilder.build(); try { Response resp = mOkHttp.newCall(okRequest).execute(); Protocol protocol = resp.protocol(); ProtocolVersion pv; switch (protocol) { case HTTP_1_0: pv = new ProtocolVersion("HTTP", 1, 0); break; case HTTP_1_1: pv = new ProtocolVersion("HTTP", 1, 1); break; case HTTP_2: pv = new ProtocolVersion("HTTP", 2, 0); break; case SPDY_3: pv = new ProtocolVersion("spdy", 3, 1); break; default: throw new BaasIOException("Invalid protocol"); } StatusLine line = new BasicStatusLine(pv, resp.code(), resp.message()); BasicHttpResponse bresp = new BasicHttpResponse(line); bresp.setEntity(asEntity(resp)); for (String name : resp.headers().names()) { String val = resp.headers().get(name); bresp.addHeader(name, val); } return bresp; } catch (IOException e) { throw new BaasIOException(e); } }
From source file:org.callimachusproject.auth.CookieAuthenticationManager.java
private HttpResponse redirectReturnTo(String url, String parameters) throws IOException { if (url.startsWith(fullname_prefix)) { // if this is a registration URL, update the fullname String fullname = reader.getUserFullName(parameters); String param = "&fullname=" + encode(fullname); if (url.contains("&fullname=")) { url.replaceAll("\\&fullname=[^\\&]*", param); } else if (url.contains("#")) { url.replaceFirst("#", param + "#"); } else {//ww w . j a v a 2 s.c o m url = url + param; } } String username = reader.getUserLogin(parameters); String iri = reader.getUserIdentifier(parameters); String userInfo = getSidCookieValue(username, iri); BasicHttpResponse resp = new BasicHttpResponse(_303); resp.addHeader("Location", url); String encoded = encode(userInfo).replace("%2F", "/").replace("%3A", ":"); resp.addHeader("Set-Cookie", sid + encoded + ";HttpOnly;Path=" + protectedPath + secureCookie); for (String cookie : getUsernameSetCookie(username)) { resp.addHeader("Set-Cookie", cookie); } return resp; }
From source file:org.callimachusproject.auth.CookieAuthenticationManager.java
private HttpResponse getLoginResponse(boolean loggedIn, String method, String url, String[] via, HttpEntity body) throws IOException { String target = url;// w ww . j a v a2s.c om String parameters = null; if (target.startsWith(redirect_prefix)) { String uri = target; String query = ""; int idx = target.indexOf('&', redirect_prefix.length()); if (idx > 0) { uri = target.substring(0, idx); query = target.substring(idx + 1); } target = getValueAfter(target, redirect_prefix); parameters = reader.getParameters(method, uri, query, body); if (reader.isCanncelled(parameters)) throw new BadGateway("Could not login"); if (reader.isLoggingIn(parameters)) { if (verify(parameters, via)) return redirectReturnTo(target, parameters); throw new BadGateway("Invalid login"); } } String location = reader.getLoginPage(redirect_prefix + encode(target), loggedIn, parameters, via); BasicHttpResponse resp = new BasicHttpResponse(_303); resp.addHeader("Location", location); resp.addHeader("Set-Cookie", sid + ";Max-Age=0;HttpOnly;Path=" + protectedPath + secureCookie); return resp; }