Search in sources :

Example 46 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project tdi-studio-se by Talend.

the class MDMTransactionClient method newTransaction.

public static MDMTransaction newTransaction(String url, String username, String password) throws IOException {
    HttpClient client = new HttpClient();
    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    client.getParams().setAuthenticationPreemptive(true);
    PutMethod put = new PutMethod(url);
    put.setDoAuthentication(true);
    String tid;
    String sessionID;
    try {
        client.executeMethod(put);
        tid = put.getResponseBodyAsString();
        sessionID = parseSessionID(put);
    } catch (HttpException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        put.releaseConnection();
    }
    MDMTransaction result = new MDMTransaction();
    result.setUrl(url);
    result.setId(tid);
    result.setUsername(username);
    result.setPassword(password);
    result.setSessionId(sessionID);
    return result;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) PutMethod(org.apache.commons.httpclient.methods.PutMethod) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 47 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project tdi-studio-se by Talend.

the class MDMTransaction method commit.

public void commit() throws IOException {
    HttpClient client = new HttpClient();
    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    HttpMethod method = new PostMethod(url + "/" + id);
    method.setDoAuthentication(true);
    try {
        //$NON-NLS-1$ //$NON-NLS-2$
        method.setRequestHeader("Cookie", getStickySession() + "=" + sessionId);
        client.executeMethod(method);
    } catch (HttpException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        method.releaseConnection();
    }
    int statuscode = method.getStatusCode();
    if (statuscode >= 400) {
        throw new MDMTransactionException("Commit failed. The commit operation has returned the code " + statuscode + ".");
    }
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) HttpMethod(org.apache.commons.httpclient.HttpMethod) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 48 with HttpClient

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

the class LmcMessage method downloadAttachment.

public byte[] downloadAttachment(String partNo, String baseURL, LmcSession session, String cookieDomain, int msTimeout) throws LmcSoapClientException, IOException {
    // set the cookie.
    if (session == null)
        System.err.println(System.currentTimeMillis() + " " + Thread.currentThread() + " LmcMessage.downloadAttachment session=null");
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    String url = baseURL + "?id=" + getID() + "&part=" + partNo;
    GetMethod get = new GetMethod(url);
    ZAuthToken zat = session.getAuthToken();
    Map<String, String> cookieMap = zat.cookieMap(false);
    if (cookieMap != null) {
        HttpState initialState = new HttpState();
        for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
            Cookie cookie = new Cookie(cookieDomain, ck.getKey(), ck.getValue(), "/", -1, false);
            initialState.addCookie(cookie);
        }
        client.setState(initialState);
        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    }
    get.getParams().setSoTimeout(msTimeout);
    int statusCode = -1;
    try {
        statusCode = HttpClientUtil.executeMethod(client, get);
        // parse the response
        if (statusCode == 200) {
            return get.getResponseBody();
        } else {
            throw new LmcSoapClientException("Attachment download failed, status=" + statusCode);
        }
    } catch (IOException e) {
        System.err.println("Attachment download failed");
        e.printStackTrace();
        throw e;
    } finally {
        get.releaseConnection();
    }
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) LmcSoapClientException(com.zimbra.cs.client.soap.LmcSoapClientException) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpState(org.apache.commons.httpclient.HttpState) IOException(java.io.IOException) ZAuthToken(com.zimbra.common.auth.ZAuthToken) Map(java.util.Map)

Example 49 with HttpClient

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

the class LmcSendMsgRequest method postAttachment.

/*
    * Post the attachment represented by File f and return the attachment ID
    */
public String postAttachment(String uploadURL, LmcSession session, File f, // cookie domain e.g. ".example.zimbra.com"
String domain, int msTimeout) throws LmcSoapClientException, IOException {
    String aid = null;
    // set the cookie.
    if (session == null)
        System.err.println(System.currentTimeMillis() + " " + Thread.currentThread() + " LmcSendMsgRequest.postAttachment session=null");
    HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
    PostMethod post = new PostMethod(uploadURL);
    ZAuthToken zat = session.getAuthToken();
    Map<String, String> cookieMap = zat.cookieMap(false);
    if (cookieMap != null) {
        HttpState initialState = new HttpState();
        for (Map.Entry<String, String> ck : cookieMap.entrySet()) {
            Cookie cookie = new Cookie(domain, ck.getKey(), ck.getValue(), "/", -1, false);
            initialState.addCookie(cookie);
        }
        client.setState(initialState);
        client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    }
    post.getParams().setSoTimeout(msTimeout);
    int statusCode = -1;
    try {
        String contentType = URLConnection.getFileNameMap().getContentTypeFor(f.getName());
        Part[] parts = { new FilePart(f.getName(), f, contentType, "UTF-8") };
        post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
        statusCode = HttpClientUtil.executeMethod(client, post);
        // parse the response
        if (statusCode == 200) {
            // paw through the returned HTML and get the attachment id
            String response = post.getResponseBodyAsString();
            //System.out.println("response is\n" + response);
            int lastQuote = response.lastIndexOf("'");
            int firstQuote = response.indexOf("','") + 3;
            if (lastQuote == -1 || firstQuote == -1)
                throw new LmcSoapClientException("Attachment post failed, unexpected response: " + response);
            aid = response.substring(firstQuote, lastQuote);
        } else {
            throw new LmcSoapClientException("Attachment post failed, status=" + statusCode);
        }
    } catch (IOException e) {
        System.err.println("Attachment post failed");
        e.printStackTrace();
        throw e;
    } finally {
        post.releaseConnection();
    }
    return aid;
}
Also used : Cookie(org.apache.commons.httpclient.Cookie) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpState(org.apache.commons.httpclient.HttpState) IOException(java.io.IOException) ZAuthToken(com.zimbra.common.auth.ZAuthToken) HttpClient(org.apache.commons.httpclient.HttpClient) Map(java.util.Map)

Example 50 with HttpClient

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

the class DataSourceManager method refreshOAuthToken.

public static void refreshOAuthToken(DataSource ds) {
    PostMethod postMethod = null;
    try {
        postMethod = new PostMethod(ds.getOauthRefreshTokenUrl());
        postMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        postMethod.addParameter(CLIENT_ID, ds.getOauthClientId());
        postMethod.addParameter(CLIENT_SECRET, ds.getDecryptedOAuthClientSecret());
        postMethod.addParameter(REFRESH_TOKEN, ds.getOauthRefreshToken());
        postMethod.addParameter(GRANT_TYPE, REFRESH_TOKEN);
        HttpClient httpClient = ZimbraHttpConnectionManager.getExternalHttpConnMgr().getDefaultHttpClient();
        int status = httpClient.executeMethod(postMethod);
        if (status == HttpStatus.SC_OK) {
            ZimbraLog.datasource.info("Refreshed oauth token status=%d", status);
            JSONObject response = new JSONObject(postMethod.getResponseBodyAsString());
            String oauthToken = response.getString(ACCESS_TOKEN);
            Map<String, Object> attrs = new HashMap<String, Object>();
            attrs.put(Provisioning.A_zimbraDataSourceOAuthToken, DataSource.encryptData(ds.getId(), oauthToken));
            Provisioning provisioning = Provisioning.getInstance();
            provisioning.modifyAttrs(ds, attrs);
        } else {
            ZimbraLog.datasource.info("Could not refresh oauth token status=%d", status);
        }
    } catch (Exception e) {
        ZimbraLog.datasource.warn("Exception while refreshing oauth token", e);
    } finally {
        if (postMethod != null) {
            postMethod.releaseConnection();
        }
    }
}
Also used : JSONObject(org.json.JSONObject) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HttpClient(org.apache.commons.httpclient.HttpClient) JSONObject(org.json.JSONObject) Provisioning(com.zimbra.cs.account.Provisioning) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException)

Aggregations

HttpClient (org.apache.commons.httpclient.HttpClient)399 GetMethod (org.apache.commons.httpclient.methods.GetMethod)221 PostMethod (org.apache.commons.httpclient.methods.PostMethod)120 HttpMethod (org.apache.commons.httpclient.HttpMethod)98 Test (org.junit.Test)87 IOException (java.io.IOException)82 InputStream (java.io.InputStream)70 HttpException (org.apache.commons.httpclient.HttpException)44 JSONParser (org.json.simple.parser.JSONParser)44 JSONObject (org.json.simple.JSONObject)40 Map (java.util.Map)35 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 HttpState (org.apache.commons.httpclient.HttpState)25 ZMailbox (com.zimbra.client.ZMailbox)23 Account (com.zimbra.cs.account.Account)22 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)22 ServiceException (com.zimbra.common.service.ServiceException)21 JSONArray (org.json.simple.JSONArray)21 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)20