Search in sources :

Example 1 with NewCookie

use of javax.ws.rs.core.NewCookie in project camel by apache.

the class CxfRsProducer method saveCookies.

private void saveCookies(Exchange exchange, Client client, CookieHandler cookieHandler) {
    if (cookieHandler != null) {
        CookieStore cookieStore = cookieHandler.getCookieStore(exchange);
        for (NewCookie newCookie : client.getResponse().getCookies().values()) {
            HttpCookie cookie = new HttpCookie(newCookie.getName(), newCookie.getValue());
            cookie.setComment(newCookie.getComment());
            cookie.setDomain(newCookie.getDomain());
            cookie.setHttpOnly(newCookie.isHttpOnly());
            cookie.setMaxAge(newCookie.getMaxAge());
            cookie.setPath(newCookie.getPath());
            cookie.setSecure(newCookie.isSecure());
            cookie.setVersion(newCookie.getVersion());
            cookieStore.add(client.getCurrentURI(), cookie);
        }
    }
}
Also used : CookieStore(java.net.CookieStore) HttpCookie(java.net.HttpCookie) NewCookie(javax.ws.rs.core.NewCookie)

Example 2 with NewCookie

use of javax.ws.rs.core.NewCookie in project camel by apache.

the class BonitaAuthFilter method filter.

@Override
public void filter(ClientRequestContext requestContext) throws IOException {
    if (requestContext.getCookies().get("JSESSIONID") == null) {
        String username = bonitaApiConfig.getUsername();
        String password = bonitaApiConfig.getPassword();
        String bonitaApiToken = null;
        if (ObjectHelper.isEmpty(username)) {
            throw new IllegalArgumentException("Username provided is null or empty.");
        }
        if (ObjectHelper.isEmpty(password)) {
            throw new IllegalArgumentException("Password provided is null or empty.");
        }
        ClientBuilder clientBuilder = ClientBuilder.newBuilder();
        Client client = clientBuilder.build();
        WebTarget webTarget = client.target(bonitaApiConfig.getBaseBonitaURI()).path("loginservice");
        MultivaluedMap<String, String> form = new MultivaluedHashMap<String, String>();
        form.add("username", username);
        form.add("password", password);
        form.add("redirect", "false");
        Response response = webTarget.request().accept(MediaType.APPLICATION_FORM_URLENCODED).post(Entity.form(form));
        Map<String, NewCookie> cr = response.getCookies();
        ArrayList<Object> cookies = new ArrayList<>();
        for (NewCookie cookie : cr.values()) {
            if ("X-Bonita-API-Token".equals(cookie.getName())) {
                bonitaApiToken = cookie.getValue();
                requestContext.getHeaders().add("X-Bonita-API-Token", bonitaApiToken);
            }
            cookies.add(cookie.toCookie());
        }
        requestContext.getHeaders().put("Cookie", cookies);
    }
}
Also used : ArrayList(java.util.ArrayList) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) ClientBuilder(javax.ws.rs.client.ClientBuilder) NewCookie(javax.ws.rs.core.NewCookie)

Example 3 with NewCookie

use of javax.ws.rs.core.NewCookie in project jersey by jersey.

the class InboundMessageContext method getResponseCookies.

/**
     * Get any new cookies set on the response message.
     *
     * @return a read-only map of cookie name (String) to a {@link javax.ws.rs.core.NewCookie new cookie}.
     */
public Map<String, NewCookie> getResponseCookies() {
    List<String> cookies = this.headers.get(HttpHeaders.SET_COOKIE);
    if (cookies == null || cookies.isEmpty()) {
        return Collections.emptyMap();
    }
    Map<String, NewCookie> result = new HashMap<String, NewCookie>();
    for (String cookie : cookies) {
        if (cookie != null) {
            NewCookie newCookie = HttpHeaderReader.readNewCookie(cookie);
            result.put(newCookie.getName(), newCookie);
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) NewCookie(javax.ws.rs.core.NewCookie)

Example 4 with NewCookie

use of javax.ws.rs.core.NewCookie in project jersey by jersey.

the class CookieImplTest method testNewCookieValueOf.

@Test
public void testNewCookieValueOf() {
    NewCookie cookie = NewCookie.valueOf("fred=flintstone;Version=2");
    assertEquals("fred", cookie.getName());
    assertEquals("flintstone", cookie.getValue());
    assertEquals(2, cookie.getVersion());
    cookie = NewCookie.valueOf("fred=flintstone;Version=1;Max-Age=60");
    assertEquals("fred", cookie.getName());
    assertEquals("flintstone", cookie.getValue());
    assertEquals(1, cookie.getVersion());
    assertEquals(60, cookie.getMaxAge());
    cookie = NewCookie.valueOf("fred=flintstone;Version=1;Comment=\"a modern stonage family\";Max-Age=60;Secure");
    assertEquals("fred", cookie.getName());
    assertEquals("flintstone", cookie.getValue());
    assertEquals("a modern stonage family", cookie.getComment());
    assertEquals(1, cookie.getVersion());
    assertEquals(60, cookie.getMaxAge());
    assertTrue(cookie.isSecure());
}
Also used : NewCookie(javax.ws.rs.core.NewCookie) Test(org.junit.Test)

Example 5 with NewCookie

use of javax.ws.rs.core.NewCookie in project jersey by jersey.

the class ResponseTest method cloneTest.

/*
     * Create an instance of Response using
     * Response.ResponseBuilder.clone()
     * verify that correct status code is returned
     */
@Test
public void cloneTest() throws CloneNotSupportedException {
    StringBuilder sb = new StringBuilder();
    int status = 200;
    List<String> type = Arrays.asList("text/plain", "text/html");
    List<String> encoding = Arrays.asList("gzip", "compress");
    List<String> lang = Arrays.asList("en-US", "en-GB", "zh-CN");
    String name = "name_1";
    String value = "value_1";
    Cookie ck1 = new Cookie(name, value);
    NewCookie nck1 = new NewCookie(ck1);
    List<String> cookies = Arrays.asList(nck1.toString().toLowerCase());
    Response.ResponseBuilder respb1 = Response.status(status).header("Content-type", "text/plain").header("Content-type", "text/html").header("Content-Language", "en-US").header("Content-Language", "en-GB").header("Content-Language", "zh-CN").header("Cache-Control", "no-transform").header("Set-Cookie", "name_1=value_1;version=1");
    Response.ResponseBuilder respb2 = respb1.clone();
    Response resp2 = respb2.build();
    String tmp = verifyResponse(resp2, null, status, encoding, lang, type, null, null, cookies);
    if (tmp.endsWith("false")) {
        System.out.println("### " + sb.toString());
        fail();
    }
    sb.append(tmp).append(newline);
    String content = "TestOnly";
    Response resp1 = respb1.entity(content).cookie((NewCookie) null).build();
    tmp = verifyResponse(resp1, content, status, encoding, lang, type, null, null, null);
    if (tmp.endsWith("false")) {
        System.out.println("### " + sb.toString());
        fail();
    }
    MultivaluedMap<java.lang.String, java.lang.Object> mvp = resp1.getMetadata();
    if (mvp.containsKey("Set-Cookie")) {
        sb.append("Response contains unexpected Set-Cookie: ").append(mvp.getFirst("Set-Cookie").toString()).append(newline);
        System.out.println("### " + sb.toString());
        fail();
    }
    sb.append(tmp).append(newline);
}
Also used : NewCookie(javax.ws.rs.core.NewCookie) Cookie(javax.ws.rs.core.Cookie) Response(javax.ws.rs.core.Response) NewCookie(javax.ws.rs.core.NewCookie) Test(org.junit.Test)

Aggregations

NewCookie (javax.ws.rs.core.NewCookie)22 Test (org.junit.Test)11 Response (javax.ws.rs.core.Response)9 IOException (java.io.IOException)3 Date (java.util.Date)3 HashMap (java.util.HashMap)3 Invocation (javax.ws.rs.client.Invocation)3 SimpleSign (ddf.security.samlp.SimpleSign)2 ValidationException (ddf.security.samlp.ValidationException)2 SecurityServiceException (ddf.security.service.SecurityServiceException)2 AbstractJerseyTest (io.dropwizard.jersey.AbstractJerseyTest)2 GET (javax.ws.rs.GET)2 WebTarget (javax.ws.rs.client.WebTarget)2 Cookie (javax.ws.rs.core.Cookie)2 User (keywhiz.auth.User)2 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)2 AuthnRequest (org.opensaml.saml.saml2.core.AuthnRequest)2 IncompatibleVersionException (com.datatorrent.stram.client.WebServicesVersionConversion.IncompatibleVersionException)1 WebServicesClient (com.datatorrent.stram.util.WebServicesClient)1 ClientHandlerException (com.sun.jersey.api.client.ClientHandlerException)1