Search in sources :

Example 6 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project redisson by redisson.

the class RedissonSessionManagerTest method testExpire.

@Test
public void testExpire() throws Exception {
    Initializer.CONFIG_CLASS = ConfigTimeout.class;
    // start the server at http://localhost:8080/myapp
    TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
    server.start();
    WebApplicationContext wa = WebApplicationContextUtils.getRequiredWebApplicationContext(server.getServletContext());
    SessionEventsListener listener = wa.getBean(SessionEventsListener.class);
    Executor executor = Executor.newInstance();
    BasicCookieStore cookieStore = new BasicCookieStore();
    executor.use(cookieStore);
    write(executor, "test", "1234");
    Cookie cookie = cookieStore.getCookies().get(0);
    Assert.assertEquals(1, listener.getSessionCreatedEvents());
    Assert.assertEquals(0, listener.getSessionExpiredEvents());
    Executor.closeIdleConnections();
    Thread.sleep(6000);
    Assert.assertEquals(1, listener.getSessionCreatedEvents());
    Assert.assertEquals(1, listener.getSessionExpiredEvents());
    executor = Executor.newInstance();
    cookieStore = new BasicCookieStore();
    cookieStore.addCookie(cookie);
    executor.use(cookieStore);
    read(executor, "test", "null");
    Assert.assertEquals(2, listener.getSessionCreatedEvents());
    write(executor, "test", "1234");
    Thread.sleep(3000);
    read(executor, "test", "1234");
    Thread.sleep(3000);
    Assert.assertEquals(1, listener.getSessionExpiredEvents());
    Thread.sleep(1000);
    Assert.assertEquals(1, listener.getSessionExpiredEvents());
    Thread.sleep(3000);
    Assert.assertEquals(2, listener.getSessionExpiredEvents());
    Executor.closeIdleConnections();
    server.stop();
}
Also used : Cookie(org.apache.http.cookie.Cookie) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Executor(org.apache.http.client.fluent.Executor) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 7 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project redisson by redisson.

the class RedissonSessionManagerTest method testSwitchServer.

@Test
public void testSwitchServer() throws Exception {
    // start the server at http://localhost:8080/myapp
    TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
    server.start();
    Executor executor = Executor.newInstance();
    BasicCookieStore cookieStore = new BasicCookieStore();
    executor.use(cookieStore);
    write(executor, "test", "1234");
    Cookie cookie = cookieStore.getCookies().get(0);
    Executor.closeIdleConnections();
    server.stop();
    server = new TomcatServer("myapp", 8080, "src/test/");
    server.start();
    executor = Executor.newInstance();
    cookieStore = new BasicCookieStore();
    cookieStore.addCookie(cookie);
    executor.use(cookieStore);
    read(executor, "test", "1234");
    remove(executor, "test", "null");
    Executor.closeIdleConnections();
    server.stop();
}
Also used : Cookie(org.apache.http.cookie.Cookie) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Executor(org.apache.http.client.fluent.Executor) Test(org.junit.Test)

Example 8 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project redisson by redisson.

the class RedissonSessionManagerTest method testInvalidate.

@Test
public void testInvalidate() throws Exception {
    // start the server at http://localhost:8080/myapp
    TomcatServer server = new TomcatServer("myapp", 8080, "src/test/");
    server.start();
    WebApplicationContext wa = WebApplicationContextUtils.getRequiredWebApplicationContext(server.getServletContext());
    SessionEventsListener listener = wa.getBean(SessionEventsListener.class);
    Executor executor = Executor.newInstance();
    BasicCookieStore cookieStore = new BasicCookieStore();
    executor.use(cookieStore);
    write(executor, "test", "1234");
    Cookie cookie = cookieStore.getCookies().get(0);
    Assert.assertEquals(1, listener.getSessionCreatedEvents());
    Assert.assertEquals(0, listener.getSessionDeletedEvents());
    invalidate(executor);
    Assert.assertEquals(1, listener.getSessionCreatedEvents());
    Assert.assertEquals(1, listener.getSessionDeletedEvents());
    Executor.closeIdleConnections();
    executor = Executor.newInstance();
    cookieStore = new BasicCookieStore();
    cookieStore.addCookie(cookie);
    executor.use(cookieStore);
    read(executor, "test", "null");
    Executor.closeIdleConnections();
    server.stop();
}
Also used : Cookie(org.apache.http.cookie.Cookie) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) Executor(org.apache.http.client.fluent.Executor) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.Test)

Example 9 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project androidquery by androidquery.

the class AbstractAjaxCallback method httpDo.

private void httpDo(HttpUriRequest hr, String url, AjaxStatus status) throws ClientProtocolException, IOException {
    DefaultHttpClient client = getClient();
    if (proxyHandle != null) {
        proxyHandle.applyProxy(this, hr, client);
    }
    if (AGENT != null) {
        hr.addHeader("User-Agent", AGENT);
    } else if (AGENT == null && GZIP) {
        hr.addHeader("User-Agent", "gzip");
    }
    if (headers != null) {
        for (String name : headers.keySet()) {
            hr.addHeader(name, headers.get(name));
        }
    }
    if (GZIP && (headers == null || !headers.containsKey("Accept-Encoding"))) {
        hr.addHeader("Accept-Encoding", "gzip");
    }
    if (ah != null) {
        ah.applyToken(this, hr);
    }
    String cookie = makeCookie();
    if (cookie != null) {
        hr.addHeader("Cookie", cookie);
    }
    HttpParams hp = hr.getParams();
    if (proxy != null)
        hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    if (timeout > 0) {
        hp.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
        hp.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
    }
    if (!redirect) {
        hp.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
    }
    HttpContext context = new BasicHttpContext();
    CookieStore cookieStore = new BasicCookieStore();
    context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    request = hr;
    if (abort) {
        throw new IOException("Aborted");
    }
    if (SIMULATE_ERROR) {
        throw new IOException("Simulated Error");
    }
    HttpResponse response = null;
    try {
        //response = client.execute(hr, context);
        response = execute(hr, client, context);
    } catch (HttpHostConnectException e) {
        //if proxy is used, automatically retry without proxy
        if (proxy != null) {
            AQUtility.debug("proxy failed, retrying without proxy");
            hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
            //response = client.execute(hr, context);
            response = execute(hr, client, context);
        } else {
            throw e;
        }
    }
    byte[] data = null;
    String redirect = url;
    int code = response.getStatusLine().getStatusCode();
    String message = response.getStatusLine().getReasonPhrase();
    String error = null;
    HttpEntity entity = response.getEntity();
    File file = null;
    File tempFile = null;
    if (code < 200 || code >= 300) {
        InputStream is = null;
        try {
            if (entity != null) {
                is = entity.getContent();
                byte[] s = toData(getEncoding(entity), is);
                error = new String(s, "UTF-8");
                AQUtility.debug("error", error);
            }
        } catch (Exception e) {
            AQUtility.debug(e);
        } finally {
            AQUtility.close(is);
        }
    } else {
        HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        redirect = currentHost.toURI() + currentReq.getURI();
        int size = Math.max(32, Math.min(1024 * 64, (int) entity.getContentLength()));
        OutputStream os = null;
        InputStream is = null;
        try {
            file = getPreFile();
            if (file == null) {
                os = new PredefinedBAOS(size);
            } else {
                //file.createNewFile();
                tempFile = makeTempFile(file);
                os = new BufferedOutputStream(new FileOutputStream(tempFile));
            }
            is = entity.getContent();
            boolean gzip = "gzip".equalsIgnoreCase(getEncoding(entity));
            if (gzip) {
                is = new GZIPInputStream(is);
            }
            int contentLength = (int) entity.getContentLength();
            //AQUtility.debug("gzip response", entity.getContentEncoding());
            copy(is, os, contentLength, tempFile, file);
            if (file == null) {
                data = ((PredefinedBAOS) os).toByteArray();
            } else {
                if (!file.exists() || file.length() == 0) {
                    file = null;
                }
            }
        } finally {
            AQUtility.close(is);
            AQUtility.close(os);
        }
    }
    AQUtility.debug("response", code);
    if (data != null) {
        AQUtility.debug(data.length, url);
    }
    status.code(code).message(message).error(error).redirect(redirect).time(new Date()).data(data).file(file).client(client).context(context).headers(response.getAllHeaders());
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) DataOutputStream(java.io.DataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) GZIPInputStream(java.util.zip.GZIPInputStream) HttpHost(org.apache.http.HttpHost) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) PredefinedBAOS(com.androidquery.util.PredefinedBAOS) BufferedOutputStream(java.io.BufferedOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) Date(java.util.Date) CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 10 with BasicCookieStore

use of org.apache.http.impl.client.BasicCookieStore in project tdi-studio-se by Talend.

the class RestClient method loginAs.

public void loginAs(String username, String password) {
    try {
        CookieStore cookieStore = new BasicCookieStore();
        httpContext = new BasicHttpContext();
        httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
        String loginURL = "/loginservice";
        // If you misspell a parameter you will get a HTTP 500 error
        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters.add(new BasicNameValuePair("username", username));
        urlParameters.add(new BasicNameValuePair("password", password));
        urlParameters.add(new BasicNameValuePair("redirect", "false"));
        // UTF-8 is mandatory otherwise you get a NPE
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(urlParameters, "utf-8");
        executePostRequest(loginURL, entity);
    } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }
}
Also used : CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) ClientProtocolException(org.apache.http.client.ClientProtocolException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) IOException(java.io.IOException)

Aggregations

BasicCookieStore (org.apache.http.impl.client.BasicCookieStore)33 CookieStore (org.apache.http.client.CookieStore)12 HttpResponse (org.apache.http.HttpResponse)10 Test (org.junit.Test)10 IOException (java.io.IOException)9 HttpGet (org.apache.http.client.methods.HttpGet)9 Cookie (org.apache.http.cookie.Cookie)9 BasicHttpContext (org.apache.http.protocol.BasicHttpContext)7 Header (org.apache.http.Header)6 ClientProtocolException (org.apache.http.client.ClientProtocolException)6 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)6 HttpContext (org.apache.http.protocol.HttpContext)6 TestHttpClient (io.undertow.testutils.TestHttpClient)5 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)5 HttpString (io.undertow.util.HttpString)4 URL (java.net.URL)4 HttpEntity (org.apache.http.HttpEntity)4 HttpClient (org.apache.http.client.HttpClient)4 Executor (org.apache.http.client.fluent.Executor)4 BasicClientCookie (org.apache.http.impl.cookie.BasicClientCookie)4