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;
}
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 + ".");
}
}
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();
}
}
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;
}
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();
}
}
}
Aggregations