use of org.apache.commons.httpclient.methods.PostMethod 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();
}
}
}
use of org.apache.commons.httpclient.methods.PostMethod in project intellij-community by JetBrains.
the class YouTrackRepository method doREST.
HttpMethod doREST(String request, boolean post) throws Exception {
HttpClient client = login(new PostMethod(getUrl() + "/rest/user/login"));
String uri = getUrl() + request;
HttpMethod method = post ? new PostMethod(uri) : new GetMethod(uri);
configureHttpMethod(method);
int status = client.executeMethod(method);
if (status == 400) {
InputStream string = method.getResponseBodyAsStream();
Element element = new SAXBuilder(false).build(string).getRootElement();
TaskUtil.prettyFormatXmlToLog(LOG, element);
if ("error".equals(element.getName())) {
throw new Exception(element.getText());
}
}
return method;
}
use of org.apache.commons.httpclient.methods.PostMethod in project intellij-community by JetBrains.
the class JiraIntegrationTest method createIssueViaRestApi.
// We can use XML-RPC in JIRA 5+ too, but nonetheless it's useful to have REST-based implementation as well
private String createIssueViaRestApi(@NotNull String project, @NotNull String summary) throws Exception {
final HttpClient client = myRepository.getHttpClient();
final PostMethod method = new PostMethod(myRepository.getUrl() + "/rest/api/latest/issue");
try {
// For simplicity assume that project, summary and username don't contain illegal characters
@Language("JSON") final String json = "{\"fields\": {\n" + " \"project\": {\n" + " \"key\": \"" + project + "\"\n" + " },\n" + " \"issuetype\": {\n" + " \"name\": \"Bug\"\n" + " },\n" + " \"assignee\": {\n" + " \"name\": \"" + myRepository.getUsername() + "\"\n" + " },\n" + " \"summary\": \"" + summary + "\"\n" + "}}";
method.setRequestEntity(new StringRequestEntity(json, "application/json", "utf-8"));
client.executeMethod(method);
return new Gson().fromJson(method.getResponseBodyAsString(), JsonObject.class).get("id").getAsString();
} finally {
method.releaseConnection();
}
}
use of org.apache.commons.httpclient.methods.PostMethod in project intellij-community by JetBrains.
the class FogBugzRepository method getCases.
@SuppressWarnings("unchecked")
private Task[] getCases(String q) throws Exception {
loginIfNeeded();
PostMethod method = new PostMethod(getUrl() + "/api.asp");
method.addParameter("token", myToken);
method.addParameter("cmd", "search");
method.addParameter("q", q);
method.addParameter("cols", "sTitle,fOpen,dtOpened,dtLastUpdated,ixCategory");
int status = getHttpClient().executeMethod(method);
if (status != 200) {
throw new Exception("Error listing cases: " + method.getStatusLine());
}
Document document = new SAXBuilder(false).build(method.getResponseBodyAsStream()).getDocument();
List<Element> errorNodes = XPath.newInstance("/response/error").selectNodes(document);
if (!errorNodes.isEmpty()) {
throw new Exception("Error listing cases: " + errorNodes.get(0).getText());
}
final XPath commentPath = XPath.newInstance("events/event");
final List<Element> nodes = (List<Element>) XPath.newInstance("/response/cases/case").selectNodes(document);
final List<Task> tasks = ContainerUtil.mapNotNull(nodes, (NotNullFunction<Element, Task>) element -> createCase(element, commentPath));
return tasks.toArray(new Task[tasks.size()]);
}
use of org.apache.commons.httpclient.methods.PostMethod in project intellij-community by JetBrains.
the class JiraRestApi2 method updateTimeSpend.
@Override
public void updateTimeSpend(@NotNull LocalTask task, @NotNull String timeSpent, String comment) throws Exception {
LOG.debug(String.format("Time spend: %s, comment: %s", timeSpent, comment));
PostMethod method = new PostMethod(myRepository.getRestUrl("issue", task.getId(), "worklog"));
String request;
if (StringUtil.isEmpty(comment)) {
request = String.format("{\"timeSpent\" : \"" + timeSpent + "\"}", timeSpent);
} else {
request = String.format("{\"timeSpent\": \"%s\", \"comment\": \"%s\"}", timeSpent, StringUtil.escapeQuotes(comment));
}
method.setRequestEntity(createJsonEntity(request));
myRepository.executeMethod(method);
}
Aggregations