Search in sources :

Example 66 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project intellij-community by JetBrains.

the class GitlabRepository method fetchProjects.

/**
   * Always forcibly attempts do fetch new projects from server.
   */
@NotNull
public List<GitlabProject> fetchProjects() throws Exception {
    final ResponseHandler<List<GitlabProject>> handler = new GsonMultipleObjectsDeserializer<>(GSON, LIST_OF_PROJECTS_TYPE);
    final String projectUrl = getRestApiUrl("projects");
    final List<GitlabProject> result = new ArrayList<>();
    int pageNum = 1;
    while (true) {
        final URI paginatedProjectsUrl = new URIBuilder(projectUrl).addParameter("page", String.valueOf(pageNum)).addParameter("per_page", "30").build();
        final List<GitlabProject> page = getHttpClient().execute(new HttpGet(paginatedProjectsUrl), handler);
        // Gitlab's REST API doesn't allow to know beforehand how many projects are available
        if (page.isEmpty()) {
            break;
        }
        result.addAll(page);
        pageNum++;
    }
    myProjects = result;
    return Collections.unmodifiableList(myProjects);
}
Also used : GitlabProject(com.intellij.tasks.gitlab.model.GitlabProject) HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) GsonMultipleObjectsDeserializer(com.intellij.tasks.impl.httpclient.TaskResponseUtil.GsonMultipleObjectsDeserializer) ArrayList(java.util.ArrayList) List(java.util.List) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 67 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project intellij-community by JetBrains.

the class GitlabRepository method fetchIssues.

@NotNull
public List<GitlabIssue> fetchIssues(int pageNumber, int pageSize, boolean openedOnly) throws Exception {
    ensureProjectsDiscovered();
    final URIBuilder uriBuilder = new URIBuilder(getIssuesUrl()).addParameter("page", String.valueOf(pageNumber)).addParameter("per_page", String.valueOf(pageSize)).addParameter("order_by", "updated_at");
    if (openedOnly) {
        // Filtering by state was added in v7.3
        uriBuilder.addParameter("state", "opened");
    }
    final ResponseHandler<List<GitlabIssue>> handler = new GsonMultipleObjectsDeserializer<>(GSON, LIST_OF_ISSUES_TYPE);
    return getHttpClient().execute(new HttpGet(uriBuilder.build()), handler);
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) GsonMultipleObjectsDeserializer(com.intellij.tasks.impl.httpclient.TaskResponseUtil.GsonMultipleObjectsDeserializer) ArrayList(java.util.ArrayList) List(java.util.List) URIBuilder(org.apache.http.client.utils.URIBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 68 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project opennms by OpenNMS.

the class WebClient method connect.

@Override
public void connect(InetAddress address, int port, int timeout) throws IOException, Exception {
    final URIBuilder ub = new URIBuilder();
    ub.setScheme(m_schema);
    ub.setHost(InetAddressUtils.str(address));
    ub.setPort(port);
    ub.setPath(m_path);
    if (m_queryString != null && m_queryString.trim().length() > 0) {
        final List<NameValuePair> params = URLEncodedUtils.parse(m_queryString, StandardCharsets.UTF_8);
        if (!params.isEmpty()) {
            ub.setParameters(params);
        }
    }
    m_httpMethod = new HttpGet(ub.build());
    m_httpMethod.setProtocolVersion(m_version);
    m_httpClientWrapper = HttpClientWrapper.create();
    if (m_overrideSSL) {
        try {
            m_httpClientWrapper.trustSelfSigned("https");
        } catch (final Exception e) {
            LOG.warn("Failed to create relaxed SSL client.", e);
        }
    }
    if (m_userAgent != null && !m_userAgent.trim().isEmpty()) {
        m_httpClientWrapper.setUserAgent(m_userAgent);
    }
    if (timeout > 0) {
        m_httpClientWrapper.setConnectionTimeout(timeout);
        m_httpClientWrapper.setSocketTimeout(timeout);
    }
    if (m_virtualHost != null && !m_virtualHost.trim().isEmpty()) {
        m_httpClientWrapper.setVirtualHost(m_virtualHost);
    }
    if (m_userName != null && !m_userName.trim().isEmpty()) {
        m_httpClientWrapper.addBasicCredentials(m_userName, m_password);
    }
    if (m_authPreemptive) {
        m_httpClientWrapper.usePreemptiveAuth();
    }
}
Also used : NameValuePair(org.apache.http.NameValuePair) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 69 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project ats-framework by Axway.

the class HttpClient method constructURI.

private URI constructURI() throws HttpException {
    // navigate to internal resource
    StringBuilder uriBuilder = new StringBuilder(url);
    if (resourcePath.size() > 0) {
        if (!url.endsWith(IoUtils.FORWARD_SLASH)) {
            uriBuilder.append(IoUtils.FORWARD_SLASH);
        }
        for (String token : resourcePath) {
            uriBuilder.append(token);
            uriBuilder.append(IoUtils.FORWARD_SLASH);
        }
        // remove the last slash if added resource path tokens
        actualUrl = uriBuilder.substring(0, uriBuilder.length() - 1);
    } else {
        actualUrl = uriBuilder.toString();
    }
    try {
        URIBuilder builder = new URIBuilder(actualUrl);
        // add request parameters
        for (Entry<String, List<String>> reqParam : requestParameters.entrySet()) {
            for (String value : reqParam.getValue()) {
                builder.setParameter(reqParam.getKey(), value);
            }
        }
        return builder.build();
    } catch (URISyntaxException e) {
        throw new HttpException("Exception occurred when creating URL.", e);
    }
}
Also used : List(java.util.List) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 70 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project intellij-community by JetBrains.

the class EduAdaptiveStepicConnector method getNextRecommendation.

@Nullable
public static Task getNextRecommendation(@NotNull Project project, @NotNull Course course) {
    try {
        final CloseableHttpClient client = EduStepicAuthorizedClient.getHttpClient();
        if (client == null) {
            LOG.warn("Http client is null");
            return null;
        }
        final URI uri = new URIBuilder(EduStepicNames.STEPIC_API_URL + EduStepicNames.RECOMMENDATIONS_URL).addParameter(EduNames.COURSE, String.valueOf(course.getId())).build();
        final HttpGet request = new HttpGet(uri);
        setTimeout(request);
        final CloseableHttpResponse response = client.execute(request);
        final HttpEntity responseEntity = response.getEntity();
        final String responseString = responseEntity != null ? EntityUtils.toString(responseEntity) : "";
        final int statusCode = response.getStatusLine().getStatusCode();
        EntityUtils.consume(responseEntity);
        if (statusCode == HttpStatus.SC_OK) {
            final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
            final StepicWrappers.RecommendationWrapper recomWrapper = gson.fromJson(responseString, StepicWrappers.RecommendationWrapper.class);
            if (recomWrapper.recommendations.length != 0) {
                final StepicWrappers.Recommendation recommendation = recomWrapper.recommendations[0];
                final String lessonId = recommendation.lesson;
                final StepicWrappers.LessonContainer lessonContainer = EduStepicAuthorizedClient.getFromStepic(EduStepicNames.LESSONS + lessonId, StepicWrappers.LessonContainer.class);
                if (lessonContainer != null && lessonContainer.lessons.size() == 1) {
                    final Lesson realLesson = lessonContainer.lessons.get(0);
                    course.getLessons().get(0).setId(Integer.parseInt(lessonId));
                    for (int stepId : realLesson.steps) {
                        final Task taskFromStep = getTask(project, realLesson.getName(), stepId);
                        if (taskFromStep != null)
                            return taskFromStep;
                    }
                } else {
                    LOG.warn("Got unexpected number of lessons: " + (lessonContainer == null ? null : lessonContainer.lessons.size()));
                }
            } else {
                LOG.warn("Got empty recommendation for the task: " + responseString);
            }
        } else {
            throw new IOException("Stepic returned non 200 status code: " + responseString);
        }
    } catch (IOException e) {
        LOG.warn(e.getMessage());
        final String connectionMessages = "Connection problems, Please, try again";
        final Balloon balloon = JBPopupFactory.getInstance().createHtmlTextBalloonBuilder(connectionMessages, MessageType.ERROR, null).createBalloon();
        ApplicationManager.getApplication().invokeLater(() -> {
            if (StudyUtils.getSelectedEditor(project) != null) {
                StudyUtils.showCheckPopUp(project, balloon);
            }
        });
    } catch (URISyntaxException e) {
        LOG.warn(e.getMessage());
    }
    return null;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpEntity(org.apache.http.HttpEntity) GsonBuilder(com.google.gson.GsonBuilder) HttpGet(org.apache.http.client.methods.HttpGet) Gson(com.google.gson.Gson) Balloon(com.intellij.openapi.ui.popup.Balloon) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

URIBuilder (org.apache.http.client.utils.URIBuilder)107 URISyntaxException (java.net.URISyntaxException)42 URI (java.net.URI)37 HttpGet (org.apache.http.client.methods.HttpGet)22 IOException (java.io.IOException)21 NameValuePair (org.apache.http.NameValuePair)13 HttpEntity (org.apache.http.HttpEntity)10 NotNull (org.jetbrains.annotations.NotNull)9 Map (java.util.Map)8 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)8 ArrayList (java.util.ArrayList)7 HashMap (java.util.HashMap)7 HttpResponse (org.apache.http.HttpResponse)7 List (java.util.List)6 HttpClient (org.apache.http.client.HttpClient)5 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)5 Gson (com.google.gson.Gson)4 URL (java.net.URL)4 RequestConfig (org.apache.http.client.config.RequestConfig)4 HttpPost (org.apache.http.client.methods.HttpPost)4