Search in sources :

Example 16 with Cookie

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

the class FormResource method processForm.

/**
     * Process the form submission. Produces a table showing the form field
     * values submitted.
     * @return a dynamically generated HTML table.
     * @param formData the data from the form submission
     */
@POST
@Consumes("application/x-www-form-urlencoded")
public String processForm(MultivaluedMap<String, String> formData) {
    StringBuilder buf = new StringBuilder();
    buf.append("<html><head><title>Form results</title></head><body>");
    buf.append("<p>Hello, you entered the following information: </p><table border='1'>");
    for (String key : formData.keySet()) {
        if (key.equals("submit")) {
            continue;
        }
        buf.append("<tr><td>");
        buf.append(key);
        buf.append("</td><td>");
        buf.append(formData.getFirst(key));
        buf.append("</td></tr>");
    }
    for (Cookie c : headers.getCookies().values()) {
        buf.append("<tr><td>Cookie: ");
        buf.append(c.getName());
        buf.append("</td><td>");
        buf.append(c.getValue());
        buf.append("</td></tr>");
    }
    buf.append("</table></body></html>");
    return buf.toString();
}
Also used : NewCookie(javax.ws.rs.core.NewCookie) Cookie(javax.ws.rs.core.Cookie) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 17 with Cookie

use of javax.ws.rs.core.Cookie in project keywhiz by square.

the class CookieRenewingFilter method filter.

/**
   * If the user has a valid session token, set a new session token. The new one should have a later
   * expiration time.
   */
@Override
public void filter(ContainerRequestContext request, ContainerResponseContext response) throws IOException {
    String sessionCookieName = sessionCookieConfig.getName();
    // If the response will be setting a session cookie, don't overwrite it; just let it go.
    if (response.getCookies().containsKey(sessionCookieName)) {
        return;
    }
    // If the request doesn't have a session cookie, we're not going to renew one.
    if (!request.getCookies().containsKey(sessionCookieName)) {
        return;
    }
    Cookie requestCookie = request.getCookies().get(sessionCookieName);
    Optional<User> optionalUser = authenticator.authenticate(requestCookie);
    if (optionalUser.isPresent()) {
        sessionLoginResource.cookiesForUser(optionalUser.get()).forEach(c -> response.getHeaders().add(HttpHeaders.SET_COOKIE, c));
    }
}
Also used : Cookie(javax.ws.rs.core.Cookie) SessionCookie(keywhiz.auth.cookie.SessionCookie) User(keywhiz.auth.User)

Example 18 with Cookie

use of javax.ws.rs.core.Cookie in project ORCID-Source by ORCID.

the class SwaggerJSONResource method getCookies.

protected Map<String, String> getCookies(HttpHeaders headers) {
    Map<String, String> output = new HashMap<String, String>();
    if (headers != null) {
        for (String key : headers.getCookies().keySet()) {
            Cookie cookie = headers.getCookies().get(key);
            output.put(key, cookie.getValue());
        }
    }
    return output;
}
Also used : Cookie(javax.ws.rs.core.Cookie) HashMap(java.util.HashMap)

Example 19 with Cookie

use of javax.ws.rs.core.Cookie in project OpenAM by OpenRock.

the class MultipleResourceRestTest method setup.

@BeforeClass
public void setup() throws Exception {
    PrivilegeManager pm = PrivilegeManager.getInstance(REALM, adminSubject);
    {
        Privilege privilege = Privilege.getNewInstance();
        privilege.setName(PRIVILEGE_NAME + "1");
        Map<String, Boolean> actions = new HashMap<String, Boolean>();
        actions.put("GET", true);
        Entitlement entitlement = new Entitlement(RESOURCE_NAME + "/*", actions);
        privilege.setEntitlement(entitlement);
        EntitlementSubject sbj = new AuthenticatedUsers();
        privilege.setSubject(sbj);
        pm.add(privilege);
    }
    {
        Privilege privilege = Privilege.getNewInstance();
        privilege.setName(PRIVILEGE_NAME + "2");
        Map<String, Boolean> actions = new HashMap<String, Boolean>();
        actions.put("GET", false);
        Entitlement entitlement = new Entitlement(RESOURCE_NAME + "/index.html", actions);
        privilege.setEntitlement(entitlement);
        EntitlementSubject sbj = new AuthenticatedUsers();
        privilege.setSubject(sbj);
        pm.add(privilege);
    }
    String tokenId = adminToken.getTokenID().toString();
    hashedTokenId = Hash.hash(tokenId);
    tokenIdHeader = RestServiceManager.SSOTOKEN_SUBJECT_PREFIX + RestServiceManager.SUBJECT_DELIMITER + tokenId;
    String cookieValue = tokenId;
    if (Boolean.parseBoolean(SystemProperties.get(Constants.AM_COOKIE_ENCODE, "false"))) {
        cookieValue = URLEncoder.encode(tokenId, "UTF-8");
    }
    cookie = new Cookie(SystemProperties.get(Constants.AM_COOKIE_NAME), cookieValue);
    user = IdRepoUtils.createUser(REALM, "MultipleResourceRestTestUser");
    decisionsClient = Client.create().resource(SystemProperties.getServerInstanceName() + "/ws/1/entitlement/decisions");
    entitlementsClient = Client.create().resource(SystemProperties.getServerInstanceName() + "/ws/1/entitlement/entitlements");
}
Also used : EntitlementSubject(com.sun.identity.entitlement.EntitlementSubject) Cookie(javax.ws.rs.core.Cookie) AuthenticatedUsers(org.forgerock.openam.entitlement.conditions.subject.AuthenticatedUsers) PrivilegeManager(com.sun.identity.entitlement.PrivilegeManager) Privilege(com.sun.identity.entitlement.Privilege) JSONEntitlement(com.sun.identity.entitlement.JSONEntitlement) Entitlement(com.sun.identity.entitlement.Entitlement) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) BeforeClass(org.testng.annotations.BeforeClass)

Example 20 with Cookie

use of javax.ws.rs.core.Cookie in project ats-framework by Axway.

the class RestClient method constructInvocationBuilder.

private Invocation.Builder constructInvocationBuilder(String descriptionToken) {
    if (StringUtils.isNullOrEmpty(this.uri)) {
        throw new IllegalArgumentException("Null or empty target URI. Please specify a valid one");
    }
    URL url;
    try {
        url = new URL(this.uri);
    } catch (MalformedURLException e) {
        throw new IllegalArgumentException("Please specify a valid URI. You have provided '" + this.uri + "'");
    }
    // remember these client id keys in order to work around a Jersey memory leak
    List<String> clientIdKeys = new ArrayList<String>();
    clientIdKeys.add("host_base_path=" + url.getProtocol() + "://" + url.getHost() + ":" + url.getPort());
    // create the client builder
    ClientBuilder clientBuilder = ClientBuilder.newBuilder();
    if (this.uri.startsWith("https")) {
        // configure Trust-all SSL context
        SSLContext sslContext = SslUtils.getSSLContext(clientConfigurator.getCertificateFileName(), clientConfigurator.getCertificateFilePassword(), supportedProtocols[0]);
        clientBuilder = clientBuilder.sslContext(sslContext).hostnameVerifier(new SslUtils.DefaultHostnameVerifier());
        clientIdKeys.add("cert_file=" + clientConfigurator.getCertificateFileName());
    }
    // (e.g. features or individual entity providers, filters or interceptors)
    for (Object provider : clientConfigurator.getProviders()) {
        clientBuilder.register(provider);
        clientIdKeys.add(provider.getClass().getName());
    }
    for (Class<?> providerClass : clientConfigurator.getProviderClasses()) {
        clientBuilder.register(providerClass);
        clientIdKeys.add(providerClass.getName());
    }
    // attach any configuration properties
    Map<String, Object> properties = clientConfigurator.getProperties();
    for (Entry<String, Object> propertyEntry : properties.entrySet()) {
        clientBuilder.property(propertyEntry.getKey(), propertyEntry.getValue());
        clientIdKeys.add(propertyEntry.getKey() + "=" + propertyEntry.getValue());
    }
    // basic authorization
    if (username != null) {
        clientIdKeys.add("user=" + username);
        clientIdKeys.add("password=" + password);
        clientBuilder.register(HttpAuthenticationFeature.basic(username, password));
    }
    // now create the client
    client = getClient(clientIdKeys, clientBuilder);
    if (debugLevel != RESTDebugLevel.NONE) {
        RequestFilter requestFilter = new RequestFilter();
        requestFilter.setDebugLevel(debugLevel);
        client.register(requestFilter);
    }
    WebTarget webTarget = client.target(this.uri);
    // navigate to internal resource
    for (String token : resourcePath) {
        webTarget = webTarget.path(token);
    }
    // add request parameters
    for (Entry<String, List<String>> requestParamEntry : requestParameters.entrySet()) {
        for (String requestParamValue : requestParamEntry.getValue()) {
            webTarget = webTarget.queryParam(requestParamEntry.getKey(), requestParamValue);
        }
    }
    log.info("We will " + descriptionToken + " " + webTarget.getUri());
    Invocation.Builder invocationBuilder = webTarget.request();
    // set response media type
    if (!StringUtils.isNullOrEmpty(responseMediaType)) {
        invocationBuilder = invocationBuilder.accept(RestMediaType.toMediaType(responseMediaType, responseMediaCharset));
    }
    // add request headers
    for (Entry<String, Object> requestHeaderEntry : requestHeaders.entrySet()) {
        invocationBuilder.header(requestHeaderEntry.getKey(), requestHeaderEntry.getValue());
    }
    // add request cookies
    for (Cookie cookie : cookies) {
        invocationBuilder.cookie(cookie);
    }
    return invocationBuilder;
}
Also used : Cookie(javax.ws.rs.core.Cookie) MalformedURLException(java.net.MalformedURLException) Invocation(javax.ws.rs.client.Invocation) ArrayList(java.util.ArrayList) SSLContext(javax.net.ssl.SSLContext) URL(java.net.URL) ArrayList(java.util.ArrayList) List(java.util.List) WebTarget(javax.ws.rs.client.WebTarget) ClientRequestFilter(javax.ws.rs.client.ClientRequestFilter) ClientBuilder(javax.ws.rs.client.ClientBuilder)

Aggregations

Cookie (javax.ws.rs.core.Cookie)49 Test (org.junit.Test)28 HashMap (java.util.HashMap)7 ContainerResponse (org.glassfish.jersey.server.ContainerResponse)7 NewCookie (javax.ws.rs.core.NewCookie)6 Entitlement (com.sun.identity.entitlement.Entitlement)4 EntitlementSubject (com.sun.identity.entitlement.EntitlementSubject)4 Privilege (com.sun.identity.entitlement.Privilege)4 PrivilegeManager (com.sun.identity.entitlement.PrivilegeManager)4 ClientResponse (com.sun.jersey.api.client.ClientResponse)4 BeforeClass (org.testng.annotations.BeforeClass)4 SSOToken (com.iplanet.sso.SSOToken)3 Form (javax.ws.rs.core.Form)3 AuthenticatedUsers (org.forgerock.openam.entitlement.conditions.subject.AuthenticatedUsers)3 JSONEntitlement (com.sun.identity.entitlement.JSONEntitlement)2 UniformInterfaceException (com.sun.jersey.api.client.UniformInterfaceException)2 MalformedURLException (java.net.MalformedURLException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Consumes (javax.ws.rs.Consumes)2