Search in sources :

Example 31 with PostMethod

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();
        }
    }
}
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)

Example 32 with PostMethod

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;
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) PostMethod(org.apache.commons.httpclient.methods.PostMethod) InputStream(java.io.InputStream) HttpClient(org.apache.commons.httpclient.HttpClient) Element(org.jdom.Element) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpMethod(org.apache.commons.httpclient.HttpMethod) JDOMException(org.jdom.JDOMException)

Example 33 with PostMethod

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();
    }
}
Also used : StringRequestEntity(org.apache.commons.httpclient.methods.StringRequestEntity) Language(org.intellij.lang.annotations.Language) PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) Gson(com.google.gson.Gson)

Example 34 with PostMethod

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()]);
}
Also used : XPath(org.jdom.xpath.XPath) SAXBuilder(org.jdom.input.SAXBuilder) TasksIcons(icons.TasksIcons) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException) Tag(com.intellij.util.xmlb.annotations.Tag) StringUtil(com.intellij.openapi.util.text.StringUtil) Date(java.util.Date) NotNullFunction(com.intellij.util.NotNullFunction) BaseRepository(com.intellij.tasks.impl.BaseRepository) PostMethod(org.apache.commons.httpclient.methods.PostMethod) DatatypeFactory(javax.xml.datatype.DatatypeFactory) ContainerUtil(com.intellij.util.containers.ContainerUtil) BaseRepositoryImpl(com.intellij.tasks.impl.BaseRepositoryImpl) XPath(org.jdom.xpath.XPath) Nullable(org.jetbrains.annotations.Nullable) Document(org.jdom.Document) List(java.util.List) Comparing(com.intellij.openapi.util.Comparing) com.intellij.tasks(com.intellij.tasks) PasswordUtil(com.intellij.openapi.util.PasswordUtil) Logger(com.intellij.openapi.diagnostic.Logger) NotNull(org.jetbrains.annotations.NotNull) Element(org.jdom.Element) javax.swing(javax.swing) SAXBuilder(org.jdom.input.SAXBuilder) PostMethod(org.apache.commons.httpclient.methods.PostMethod) Element(org.jdom.Element) List(java.util.List) Document(org.jdom.Document) DatatypeConfigurationException(javax.xml.datatype.DatatypeConfigurationException)

Example 35 with PostMethod

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);
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod)

Aggregations

PostMethod (org.apache.commons.httpclient.methods.PostMethod)203 HttpClient (org.apache.commons.httpclient.HttpClient)114 Test (org.junit.Test)55 IOException (java.io.IOException)32 MultipartRequestEntity (org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity)28 Part (org.apache.commons.httpclient.methods.multipart.Part)25 NameValuePair (org.apache.commons.httpclient.NameValuePair)24 FilePart (org.apache.commons.httpclient.methods.multipart.FilePart)23 Map (java.util.Map)21 GetMethod (org.apache.commons.httpclient.methods.GetMethod)21 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)21 StringPart (org.apache.commons.httpclient.methods.multipart.StringPart)21 HttpMethod (org.apache.commons.httpclient.HttpMethod)17 Header (org.apache.commons.httpclient.Header)16 Note (org.apache.zeppelin.notebook.Note)13 HttpException (org.apache.commons.httpclient.HttpException)12 File (java.io.File)11 InputStream (java.io.InputStream)11 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)11 JSONParser (org.json.simple.parser.JSONParser)11