Search in sources :

Example 26 with CookieStore

use of java.net.CookieStore in project j2objc by google.

the class CookiesTest method testCookieStoreRemoveRequiresUri.

public void testCookieStoreRemoveRequiresUri() throws URISyntaxException {
    CookieStore cookieStore = new CookieManager().getCookieStore();
    HttpCookie cookieA = new HttpCookie("a", "android");
    cookieStore.add(new URI("http://android.com/source/"), cookieA);
    assertFalse(// RI6 fails this
    "Expected remove() to take the cookie URI into account.", cookieStore.remove(new URI("http://code.google.com/"), cookieA));
    assertEquals(Arrays.asList(cookieA), cookieStore.getCookies());
}
Also used : CookieStore(java.net.CookieStore) HttpCookie(java.net.HttpCookie) URI(java.net.URI) CookieManager(java.net.CookieManager)

Example 27 with CookieStore

use of java.net.CookieStore in project android-oss by kickstarter.

the class RefTagUtilsTest method testFindRefTagCookieForProject_WhenCookieExists.

@Test
public void testFindRefTagCookieForProject_WhenCookieExists() {
    final CookieManager cookieManager = new CookieManager();
    final CookieStore cookieStore = cookieManager.getCookieStore();
    final Project project = ProjectFactory.project();
    final RefTag refTag = RefTag.recommended();
    // set and retrieve the cookie
    cookieStore.add(null, new HttpCookie("ref_" + project.id(), refTag.tag() + "%3F" + SystemUtils.secondsSinceEpoch()));
    final HttpCookie cookie = RefTagUtils.findRefTagCookieForProject(project, cookieManager, sharedPreferences);
    assertNotNull(cookie);
    assertEquals(RefTagUtils.cookieNameForProject(project), cookie.getName());
    assertEquals(RefTagUtils.cookieValueForRefTag(refTag), cookie.getValue());
}
Also used : CookieStore(java.net.CookieStore) Project(com.kickstarter.models.Project) RefTag(com.kickstarter.libs.RefTag) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Test(org.junit.Test)

Example 28 with CookieStore

use of java.net.CookieStore in project android-oss by kickstarter.

the class RefTagUtils method findRefTagCookieForProject.

/**
   * Finds the ref tag cookie associated with a project. Returns `null` if no cookie has yet been set.
   */
@Nullable
protected static HttpCookie findRefTagCookieForProject(@NonNull final Project project, @NonNull final CookieManager cookieManager, @NonNull final SharedPreferences sharedPreferences) {
    final String cookieName = cookieNameForProject(project);
    // First try finding the cookie in the cookie store
    final CookieStore cookieStore = cookieManager.getCookieStore();
    for (final HttpCookie cookie : cookieStore.getCookies()) {
        if (cookieName.equals(cookie.getName())) {
            return cookie;
        }
    }
    // If we can't find it in the cookie store let's look in shared prefs
    final String cookieValue = sharedPreferences.getString(cookieName, null);
    if (cookieValue != null) {
        return buildCookieWithValueAndProject(cookieValue, project);
    }
    return null;
}
Also used : CookieStore(java.net.CookieStore) HttpCookie(java.net.HttpCookie) Nullable(android.support.annotation.Nullable)

Example 29 with CookieStore

use of java.net.CookieStore in project camel by apache.

the class RestletProducer method storeCookies.

private void storeCookies(Exchange exchange, URI uri, Response response) {
    RestletEndpoint endpoint = (RestletEndpoint) getEndpoint();
    if (endpoint.getCookieHandler() != null) {
        Series<CookieSetting> cookieSettings = response.getCookieSettings();
        CookieStore cookieJar = endpoint.getCookieHandler().getCookieStore(exchange);
        for (CookieSetting s : cookieSettings) {
            HttpCookie cookie = new HttpCookie(s.getName(), s.getValue());
            cookie.setComment(s.getComment());
            cookie.setDomain(s.getDomain());
            cookie.setMaxAge(s.getMaxAge());
            cookie.setPath(s.getPath());
            cookie.setSecure(s.isSecure());
            cookie.setVersion(s.getVersion());
            cookieJar.add(uri, cookie);
        }
    }
}
Also used : CookieSetting(org.restlet.data.CookieSetting) CookieStore(java.net.CookieStore) HttpCookie(java.net.HttpCookie)

Example 30 with CookieStore

use of java.net.CookieStore in project camel by apache.

the class JettyHttpProducer method processInternal.

private void processInternal(Exchange exchange, AsyncCallback callback) throws Exception {
    // creating the url to use takes 2-steps
    String url = HttpHelper.createURL(exchange, getEndpoint());
    URI uri = HttpHelper.createURI(exchange, url, getEndpoint());
    // get the url from the uri
    url = uri.toASCIIString();
    // execute any custom url rewrite
    String rewriteUrl = HttpHelper.urlRewrite(exchange, url, getEndpoint(), this);
    if (rewriteUrl != null) {
        // update url and query string from the rewritten url
        url = rewriteUrl;
    }
    String methodName = HttpHelper.createMethod(exchange, getEndpoint(), exchange.getIn().getBody() != null).name();
    JettyContentExchange httpExchange = getEndpoint().createContentExchange();
    httpExchange.init(exchange, getBinding(), client, callback);
    // url must have scheme
    try {
        uri = new URI(url);
        String scheme = uri.getScheme();
        if (scheme == null) {
            throw new IllegalArgumentException("Url must include scheme: " + url + ". If you are bridging endpoints set bridgeEndpoint=true." + " If you want to call a specific url, then you may need to remove all CamelHttp* headers in the route before this." + " See more details at: http://camel.apache.org/how-to-remove-the-http-protocol-headers-in-the-camel-message.html");
        }
    } catch (URISyntaxException e) {
    // ignore
    }
    // Url has to be set first
    httpExchange.setURL(url);
    httpExchange.setMethod(methodName);
    if (getEndpoint().getHttpClientParameters() != null) {
        // For jetty 9 these parameters can not be set on the client
        // so we need to set them on the httpExchange
        String timeout = (String) getEndpoint().getHttpClientParameters().get("timeout");
        if (timeout != null) {
            httpExchange.setTimeout(new Long(timeout));
        }
        String supportRedirect = (String) getEndpoint().getHttpClientParameters().get("supportRedirect");
        if (supportRedirect != null) {
            httpExchange.setSupportRedirect(Boolean.valueOf(supportRedirect));
        }
    }
    LOG.trace("Using URL: {} with method: {}", url, methodName);
    // if there is a body to send as data
    if (exchange.getIn().getBody() != null) {
        String contentType = ExchangeHelper.getContentType(exchange);
        if (contentType != null) {
            httpExchange.setRequestContentType(contentType);
        }
        if (contentType != null && HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType)) {
            if (getEndpoint().getComponent().isAllowJavaSerializedObject() || getEndpoint().isTransferException()) {
                // serialized java object
                Serializable obj = exchange.getIn().getMandatoryBody(Serializable.class);
                // write object to output stream
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                try {
                    HttpHelper.writeObjectToStream(bos, obj);
                    httpExchange.setRequestContent(bos.toByteArray());
                } finally {
                    IOHelper.close(bos, "body", LOG);
                }
            } else {
                throw new RuntimeCamelException("Content-type " + HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT + " is not allowed");
            }
        } else {
            Object body = exchange.getIn().getBody();
            if (body instanceof String) {
                String data = (String) body;
                // be a bit careful with String as any type can most likely be converted to String
                // so we only do an instanceof check and accept String if the body is really a String
                // do not fallback to use the default charset as it can influence the request
                // (for example application/x-www-form-urlencoded forms being sent)
                String charset = IOHelper.getCharsetName(exchange, false);
                httpExchange.setRequestContent(data, charset);
            } else {
                // then fallback to input stream
                InputStream is = exchange.getContext().getTypeConverter().mandatoryConvertTo(InputStream.class, exchange, exchange.getIn().getBody());
                httpExchange.setRequestContent(is);
                // setup the content length if it is possible
                String length = exchange.getIn().getHeader(Exchange.CONTENT_LENGTH, String.class);
                if (ObjectHelper.isNotEmpty(length)) {
                    httpExchange.addRequestHeader(Exchange.CONTENT_LENGTH, length);
                }
            }
        }
    }
    // if we bridge endpoint then we need to skip matching headers with the HTTP_QUERY to avoid sending
    // duplicated headers to the receiver, so use this skipRequestHeaders as the list of headers to skip
    Map<String, Object> skipRequestHeaders = null;
    if (getEndpoint().isBridgeEndpoint()) {
        exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE);
        String queryString = exchange.getIn().getHeader(Exchange.HTTP_QUERY, String.class);
        if (queryString != null) {
            skipRequestHeaders = URISupport.parseQuery(queryString, false, true);
        }
    }
    // propagate headers as HTTP headers
    Message in = exchange.getIn();
    HeaderFilterStrategy strategy = getEndpoint().getHeaderFilterStrategy();
    for (Map.Entry<String, Object> entry : in.getHeaders().entrySet()) {
        String key = entry.getKey();
        Object headerValue = in.getHeader(key);
        if (headerValue != null) {
            // use an iterator as there can be multiple values. (must not use a delimiter, and allow empty values)
            final Iterator<?> it = ObjectHelper.createIterator(headerValue, null, true);
            // the values to add as a request header
            final List<String> values = new ArrayList<String>();
            // should be combined into a single value
            while (it.hasNext()) {
                String value = exchange.getContext().getTypeConverter().convertTo(String.class, it.next());
                // as then we would duplicate headers on both the endpoint uri, and in HTTP headers as well
                if (skipRequestHeaders != null && skipRequestHeaders.containsKey(key)) {
                    continue;
                }
                if (value != null && strategy != null && !strategy.applyFilterToCamelHeaders(key, value, exchange)) {
                    values.add(value);
                }
            }
            // add the value(s) as a http request header
            if (values.size() > 0) {
                // use the default toString of a ArrayList to create in the form [xxx, yyy]
                // if multi valued, for a single value, then just output the value as is
                String s = values.size() > 1 ? values.toString() : values.get(0);
                httpExchange.addRequestHeader(key, s);
            }
        }
    }
    if (getEndpoint().isConnectionClose()) {
        httpExchange.addRequestHeader("Connection", "close");
    }
    //if this option is set, and the exchange Host header is not null, we will set it's current value on the httpExchange
    if (getEndpoint().isPreserveHostHeader()) {
        String hostHeader = exchange.getIn().getHeader("Host", String.class);
        if (hostHeader != null) {
            //HttpClient 4 will check to see if the Host header is present, and use it if it is, see org.apache.http.protocol.RequestTargetHost in httpcore
            httpExchange.addRequestHeader("Host", hostHeader);
        }
    }
    // set the callback, which will handle all the response logic
    if (LOG.isDebugEnabled()) {
        LOG.debug("Sending HTTP request to: {}", httpExchange.getUrl());
    }
    if (getEndpoint().getCookieHandler() != null) {
        // this will store the cookie in the cookie store
        CookieStore cookieStore = getEndpoint().getCookieHandler().getCookieStore(exchange);
        if (!client.getCookieStore().equals(cookieStore)) {
            client.setCookieStore(cookieStore);
        }
    }
    httpExchange.send(client);
}
Also used : Serializable(java.io.Serializable) Message(org.apache.camel.Message) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) HeaderFilterStrategy(org.apache.camel.spi.HeaderFilterStrategy) URISyntaxException(java.net.URISyntaxException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URI(java.net.URI) CookieStore(java.net.CookieStore) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Map(java.util.Map)

Aggregations

CookieStore (java.net.CookieStore)37 HttpCookie (java.net.HttpCookie)33 CookieManager (java.net.CookieManager)28 URI (java.net.URI)26 RefTag (com.kickstarter.libs.RefTag)2 Project (com.kickstarter.models.Project)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Nullable (android.support.annotation.Nullable)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InputStream (java.io.InputStream)1 Serializable (java.io.Serializable)1 URISyntaxException (java.net.URISyntaxException)1 Map (java.util.Map)1 NewCookie (javax.ws.rs.core.NewCookie)1 Message (org.apache.camel.Message)1 RuntimeCamelException (org.apache.camel.RuntimeCamelException)1 HeaderFilterStrategy (org.apache.camel.spi.HeaderFilterStrategy)1 ContentProvider (org.eclipse.jetty.client.api.ContentProvider)1 HttpFields (org.eclipse.jetty.http.HttpFields)1