Search in sources :

Example 56 with Header

use of org.apache.commons.httpclient.Header in project zm-mailbox by Zimbra.

the class WebDavClient method logResponseInfo.

private void logResponseInfo(HttpMethod method) throws IOException {
    if (!mDebugEnabled) {
        return;
    }
    StringBuilder responseLog = new StringBuilder();
    responseLog.append("WebDAV response:\n").append(method.getStatusLine()).append('\n');
    Header[] headers = method.getResponseHeaders();
    if (headers != null && headers.length > 0) {
        for (Header hdr : headers) {
            String hdrName = hdr.getName();
            responseLog.append(hdrName).append('=');
            if (hdrName.contains("Auth") || (hdrName.contains(HttpHeaders.COOKIE))) {
                responseLog.append("*** REPLACED ***\n");
            } else {
                responseLog.append(hdr.getValue()).append('\n');
            }
        }
    }
    if (method.getResponseBody() == null || !ZimbraLog.dav.isTraceEnabled()) {
        ZimbraLog.dav.debug(responseLog.toString());
    } else {
        ZimbraLog.dav.debug("%s\n%s", responseLog.toString(), new String(method.getResponseBody(), "UTF-8"));
    }
}
Also used : Header(org.apache.commons.httpclient.Header)

Example 57 with Header

use of org.apache.commons.httpclient.Header in project zm-mailbox by Zimbra.

the class FeedManager method retrieveRemoteData.

private static RemoteDataInfo retrieveRemoteData(String url, Folder.SyncData fsd) throws ServiceException, HttpException, IOException {
    assert !Strings.isNullOrEmpty(url);
    HttpClient client = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient();
    HttpProxyUtil.configureProxy(client);
    // cannot set connection timeout because it'll affect all HttpClients associated with the conn mgr.
    // see comments in ZimbraHttpConnectionManager
    // client.setConnectionTimeout(10000);
    HttpMethodParams params = new HttpMethodParams();
    params.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, MimeConstants.P_CHARSET_UTF8);
    params.setSoTimeout(60000);
    GetMethod get = null;
    BufferedInputStream content = null;
    long lastModified = 0;
    String expectedCharset = MimeConstants.P_CHARSET_UTF8;
    int redirects = 0;
    int statusCode = HttpServletResponse.SC_NOT_FOUND;
    try {
        do {
            String lcurl = url.toLowerCase();
            if (lcurl.startsWith("webcal:")) {
                url = "http:" + url.substring(7);
            } else if (lcurl.startsWith("feed:")) {
                url = "http:" + url.substring(5);
            } else if (!lcurl.startsWith("http:") && !lcurl.startsWith("https:")) {
                throw ServiceException.INVALID_REQUEST("url must begin with http: or https:", null);
            }
            // username and password are encoded in the URL as http://user:pass@host/...
            if (url.indexOf('@') != -1) {
                HttpURL httpurl = lcurl.startsWith("https:") ? new HttpsURL(url) : new HttpURL(url);
                if (httpurl.getUser() != null) {
                    String user = httpurl.getUser();
                    if (user.indexOf('%') != -1) {
                        try {
                            user = URLDecoder.decode(user, "UTF-8");
                        } catch (OutOfMemoryError e) {
                            Zimbra.halt("out of memory", e);
                        } catch (Throwable t) {
                        }
                    }
                    UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, httpurl.getPassword());
                    client.getParams().setAuthenticationPreemptive(true);
                    client.getState().setCredentials(AuthScope.ANY, creds);
                }
            }
            try {
                get = new GetMethod(url);
            } catch (OutOfMemoryError e) {
                Zimbra.halt("out of memory", e);
                return null;
            } catch (Throwable t) {
                throw ServiceException.INVALID_REQUEST("invalid url for feed: " + url, t);
            }
            get.setParams(params);
            get.setFollowRedirects(true);
            get.setDoAuthentication(true);
            get.addRequestHeader("User-Agent", HTTP_USER_AGENT);
            get.addRequestHeader("Accept", HTTP_ACCEPT);
            if (fsd != null && fsd.getLastSyncDate() > 0) {
                String lastSyncAt = org.apache.commons.httpclient.util.DateUtil.formatDate(new Date(fsd.getLastSyncDate()));
                get.addRequestHeader("If-Modified-Since", lastSyncAt);
            }
            HttpClientUtil.executeMethod(client, get);
            Header locationHeader = get.getResponseHeader("location");
            if (locationHeader != null) {
                // update our target URL and loop again to do another HTTP GET
                url = locationHeader.getValue();
                get.releaseConnection();
            } else {
                statusCode = get.getStatusCode();
                if (statusCode == HttpServletResponse.SC_OK) {
                    Header contentEncoding = get.getResponseHeader("Content-Encoding");
                    InputStream respInputStream = get.getResponseBodyAsStream();
                    if (contentEncoding != null) {
                        if (contentEncoding.getValue().indexOf("gzip") != -1) {
                            respInputStream = new GZIPInputStream(respInputStream);
                        }
                    }
                    content = new BufferedInputStream(respInputStream);
                    expectedCharset = get.getResponseCharSet();
                    Header lastModHdr = get.getResponseHeader("Last-Modified");
                    if (lastModHdr == null) {
                        lastModHdr = get.getResponseHeader("Date");
                    }
                    if (lastModHdr != null) {
                        try {
                            Date d = org.apache.commons.httpclient.util.DateUtil.parseDate(lastModHdr.getValue());
                            lastModified = d.getTime();
                        } catch (DateParseException e) {
                            ZimbraLog.misc.warn("unable to parse Last-Modified/Date header: " + lastModHdr.getValue(), e);
                            lastModified = System.currentTimeMillis();
                        }
                    } else {
                        lastModified = System.currentTimeMillis();
                    }
                } else if (statusCode == HttpServletResponse.SC_NOT_MODIFIED) {
                    ZimbraLog.misc.debug("Remote data at " + url + " not modified since last sync");
                    return new RemoteDataInfo(statusCode, redirects, null, expectedCharset, lastModified);
                } else {
                    throw ServiceException.RESOURCE_UNREACHABLE(get.getStatusLine().toString(), null);
                }
                break;
            }
        } while (++redirects <= MAX_REDIRECTS);
    } catch (ServiceException ex) {
        if (get != null) {
            get.releaseConnection();
        }
        throw ex;
    } catch (HttpException ex) {
        if (get != null) {
            get.releaseConnection();
        }
        throw ex;
    } catch (IOException ex) {
        if (get != null) {
            get.releaseConnection();
        }
        throw ex;
    }
    RemoteDataInfo rdi = new RemoteDataInfo(statusCode, redirects, content, expectedCharset, lastModified);
    rdi.setGetMethod(get);
    return rdi;
}
Also used : DateParseException(org.apache.commons.httpclient.util.DateParseException) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) HttpURL(org.apache.commons.httpclient.HttpURL) Date(java.util.Date) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) GZIPInputStream(java.util.zip.GZIPInputStream) Header(org.apache.commons.httpclient.Header) ServiceException(com.zimbra.common.service.ServiceException) BufferedInputStream(java.io.BufferedInputStream) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpException(org.apache.commons.httpclient.HttpException) HttpsURL(org.apache.commons.httpclient.HttpsURL)

Example 58 with Header

use of org.apache.commons.httpclient.Header in project zm-mailbox by Zimbra.

the class SoapDebugListener method receiveSoapMessage.

@Override
public void receiveSoapMessage(PostMethod postMethod, Element envelope) {
    if (level == Level.OFF) {
        return;
    }
    System.out.println();
    System.out.println("=== Response ===");
    if (Level.needsHeader(level)) {
        Header[] headers = postMethod.getResponseHeaders();
        for (Header header : headers) {
            // trim the ending crlf
            System.out.println(header.toString().trim());
        }
        System.out.println();
    }
    if (Level.needsBody(level)) {
        System.out.println(envelope.prettyPrint());
    }
}
Also used : Header(org.apache.commons.httpclient.Header)

Example 59 with Header

use of org.apache.commons.httpclient.Header in project zm-mailbox by Zimbra.

the class UserServlet method putMailItem.

public static Pair<Header[], HttpInputStream> putMailItem(ZAuthToken authToken, String url, MailItem item) throws ServiceException, IOException {
    if (item instanceof Document) {
        Document doc = (Document) item;
        StringBuilder u = new StringBuilder(url);
        u.append("?").append(QP_AUTH).append('=').append(AUTH_COOKIE);
        if (doc.getType() == MailItem.Type.WIKI) {
            u.append("&fmt=wiki");
        }
        PutMethod method = new PutMethod(u.toString());
        String contentType = doc.getContentType();
        method.addRequestHeader("Content-Type", contentType);
        method.setRequestEntity(new InputStreamRequestEntity(doc.getContentStream(), doc.getSize(), contentType));
        method = HttpClientUtil.addInputStreamToHttpMethod(method, doc.getContentStream(), doc.getSize(), contentType);
        method.addRequestHeader("X-Zimbra-Description", doc.getDescription());
        method.setRequestEntity(new InputStreamRequestEntity(doc.getContentStream(), doc.getSize(), contentType));
        Pair<Header[], HttpMethod> pair = doHttpOp(authToken, method);
        return new Pair<Header[], HttpInputStream>(pair.getFirst(), new HttpInputStream(pair.getSecond()));
    }
    return putRemoteResource(authToken, url, item.getContentStream(), null);
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) Header(org.apache.commons.httpclient.Header) PutMethod(org.apache.commons.httpclient.methods.PutMethod) Document(com.zimbra.cs.mailbox.Document) HttpMethod(org.apache.commons.httpclient.HttpMethod) Pair(com.zimbra.common.util.Pair)

Example 60 with Header

use of org.apache.commons.httpclient.Header in project zm-mailbox by Zimbra.

the class UserServlet method doHttpOp.

private static Pair<Header[], HttpMethod> doHttpOp(ZAuthToken authToken, HttpMethod method) throws ServiceException {
    // create an HTTP client with the same cookies
    String url = "";
    String hostname = "";
    try {
        url = method.getURI().toString();
        hostname = method.getURI().getHost();
    } catch (IOException e) {
        log.warn("can't parse target URI", e);
    }
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    Map<String, String> cookieMap = authToken.cookieMap(false);
    if (cookieMap != null) {
        HttpState state = new HttpState();
        for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
            state.addCookie(new org.apache.commons.httpclient.Cookie(hostname, ck.getKey(), ck.getValue(), "/", null, false));
        }
        client.setState(state);
        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    }
    if (method instanceof PutMethod) {
        long contentLength = ((PutMethod) method).getRequestEntity().getContentLength();
        if (contentLength > 0) {
            // 100kbps in millis
            int timeEstimate = Math.max(10000, (int) (contentLength / 100));
            // cannot set connection time using our ZimbrahttpConnectionManager,
            // see comments in ZimbrahttpConnectionManager.
            // actually, length of the content to Put should not be a factor for
            // establishing a connection, only read time out matter, which we set
            // client.getHttpConnectionManager().getParams().setConnectionTimeout(timeEstimate);
            method.getParams().setSoTimeout(timeEstimate);
        }
    }
    try {
        int statusCode = HttpClientUtil.executeMethod(client, method);
        if (statusCode == HttpStatus.SC_NOT_FOUND || statusCode == HttpStatus.SC_FORBIDDEN)
            throw MailServiceException.NO_SUCH_ITEM(-1);
        else if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_CREATED && statusCode != HttpStatus.SC_NO_CONTENT)
            throw ServiceException.RESOURCE_UNREACHABLE(method.getStatusText(), null, new ServiceException.InternalArgument(HTTP_URL, url, ServiceException.Argument.Type.STR), new ServiceException.InternalArgument(HTTP_STATUS_CODE, statusCode, ServiceException.Argument.Type.NUM));
        List<Header> headers = new ArrayList<Header>(Arrays.asList(method.getResponseHeaders()));
        headers.add(new Header("X-Zimbra-Http-Status", "" + statusCode));
        return new Pair<Header[], HttpMethod>(headers.toArray(new Header[0]), method);
    } catch (HttpException e) {
        throw ServiceException.RESOURCE_UNREACHABLE("HttpException while fetching " + url, e);
    } catch (IOException e) {
        throw ServiceException.RESOURCE_UNREACHABLE("IOException while fetching " + url, e);
    }
}
Also used : HttpState(org.apache.commons.httpclient.HttpState) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Mountpoint(com.zimbra.cs.mailbox.Mountpoint) AccountServiceException(com.zimbra.cs.account.AccountServiceException) ServiceException(com.zimbra.common.service.ServiceException) MailServiceException(com.zimbra.cs.mailbox.MailServiceException) Header(org.apache.commons.httpclient.Header) HttpClient(org.apache.commons.httpclient.HttpClient) PutMethod(org.apache.commons.httpclient.methods.PutMethod) HttpException(org.apache.commons.httpclient.HttpException) Map(java.util.Map) HashMap(java.util.HashMap) Pair(com.zimbra.common.util.Pair)

Aggregations

Header (org.apache.commons.httpclient.Header)93 GetMethod (org.apache.commons.httpclient.methods.GetMethod)23 Test (org.junit.Test)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)21 IOException (java.io.IOException)20 HttpClient (org.apache.commons.httpclient.HttpClient)20 NameValuePair (org.apache.commons.httpclient.NameValuePair)18 HttpMethod (org.apache.commons.httpclient.HttpMethod)17 ArrayList (java.util.ArrayList)16 InputStream (java.io.InputStream)14 HttpException (org.apache.commons.httpclient.HttpException)13 PutMethod (org.apache.commons.httpclient.methods.PutMethod)10 Account (com.zimbra.cs.account.Account)9 ServiceException (com.zimbra.common.service.ServiceException)6 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)6 HttpTest (org.apache.sling.commons.testing.integration.HttpTest)6 Pair (com.zimbra.common.util.Pair)5 ByteArrayInputStream (java.io.ByteArrayInputStream)5 HashMap (java.util.HashMap)5 DefaultHttpMethodRetryHandler (org.apache.commons.httpclient.DefaultHttpMethodRetryHandler)5