Search in sources :

Example 71 with URIBuilder

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

the class EduAdaptiveStepicConnector method getCheckResults.

@NotNull
private static StepicWrappers.ResultSubmissionWrapper getCheckResults(@NotNull CloseableHttpClient client, @NotNull StepicWrappers.ResultSubmissionWrapper wrapper, int attemptId, int userId) {
    try {
        while (wrapper.submissions.length == 1 && wrapper.submissions[0].status.equals("evaluation")) {
            TimeUnit.MILLISECONDS.sleep(500);
            final URI submissionURI = new URIBuilder(EduStepicNames.STEPIC_API_URL + EduStepicNames.SUBMISSIONS).addParameter("attempt", String.valueOf(attemptId)).addParameter("order", "desc").addParameter("user", String.valueOf(userId)).build();
            final HttpGet httpGet = new HttpGet(submissionURI);
            setTimeout(httpGet);
            final CloseableHttpResponse httpResponse = client.execute(httpGet);
            final HttpEntity entity = httpResponse.getEntity();
            final String entityString = EntityUtils.toString(entity);
            EntityUtils.consume(entity);
            wrapper = new Gson().fromJson(entityString, StepicWrappers.ResultSubmissionWrapper.class);
        }
    } catch (InterruptedException | URISyntaxException | IOException e) {
        LOG.warn(e.getMessage());
    }
    return wrapper;
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Gson(com.google.gson.Gson) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 72 with URIBuilder

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

the class EduStepicConnector method addCoursesFromStepic.

private static boolean addCoursesFromStepic(@Nullable StepicUser user, List<CourseInfo> result, int pageNumber) throws IOException {
    final URI url;
    try {
        url = new URIBuilder(EduStepicNames.COURSES).addParameter("is_idea_compatible", "true").addParameter("page", String.valueOf(pageNumber)).build();
    } catch (URISyntaxException e) {
        LOG.error(e.getMessage());
        return false;
    }
    final StepicWrappers.CoursesContainer coursesContainer;
    if (user != null && user.getAccessToken() != null) {
        coursesContainer = EduStepicAuthorizedClient.getFromStepic(url.toString(), StepicWrappers.CoursesContainer.class, user);
    } else {
        coursesContainer = EduStepicClient.getFromStepic(url.toString(), StepicWrappers.CoursesContainer.class);
    }
    addAvailableCourses(result, coursesContainer);
    return coursesContainer.meta.containsKey("has_next") && coursesContainer.meta.get("has_next") == Boolean.TRUE;
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 73 with URIBuilder

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

the class IpnbConnection method login.

private String login(@NotNull String username, @NotNull String password, @NotNull String loginUrl) throws IOException {
    String urlParameters = null;
    try {
        urlParameters = new URIBuilder().addParameter(XSRF_PARAMETER, myXsrf).addParameter(USERNAME_PARAMETER, username).addParameter(PASSWORD_PARAMETER, password).build().toString();
    } catch (URISyntaxException e) {
        LOG.warn(e.getMessage());
    }
    if (urlParameters == null)
        throw new IOException("Unable to login");
    byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8);
    final HttpsURLConnection connection = ObjectUtils.tryCast(configureConnection((HttpURLConnection) new URL(myURI + loginUrl).openConnection(), HTTPMethod.POST.name()), HttpsURLConnection.class);
    if (connection != null) {
        connection.setUseCaches(false);
        connection.setRequestProperty(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.getMimeType());
        connection.setRequestProperty(HttpHeaders.CONTENT_LENGTH, Integer.toString(postData.length));
        connection.setDoOutput(true);
        final OutputStream outputStream = connection.getOutputStream();
        try (DataOutputStream wr = new DataOutputStream(outputStream)) {
            wr.write(postData);
            wr.flush();
        }
        connection.connect();
        final int code = connection.getResponseCode();
        if (code != HttpURLConnection.HTTP_FORBIDDEN && code != HttpURLConnection.HTTP_UNAUTHORIZED) {
            final List<HttpCookie> cookies = myCookieManager.getCookieStore().getCookies();
            if (!cookies.isEmpty()) {
                return cookies.stream().map(cookie -> cookie.getName() + "=" + cookie.getValue()).collect(Collectors.joining(";"));
            }
        }
    }
    String message = connection == null ? "" : connection.getResponseCode() + " " + connection.getResponseMessage();
    throw new IOException(UNABLE_LOGIN_MESSAGE + message);
}
Also used : ServerHandshake(org.java_websocket.handshake.ServerHandshake) IpnbSettings(org.jetbrains.plugins.ipnb.configuration.IpnbSettings) SSLContext(javax.net.ssl.SSLContext) WebSocketClient(org.java_websocket.client.WebSocketClient) HTTPMethod(com.intellij.util.net.HTTPMethod) HashMap(java.util.HashMap) Draft(org.java_websocket.drafts.Draft) CertificateManager(com.intellij.util.net.ssl.CertificateManager) Lists(com.google.common.collect.Lists) SSLSession(javax.net.ssl.SSLSession) java.net(java.net) Map(java.util.Map) Project(com.intellij.openapi.project.Project) Logger(com.intellij.openapi.diagnostic.Logger) HostnameVerifier(javax.net.ssl.HostnameVerifier) HttpHeaders(org.apache.http.HttpHeaders) SM(org.apache.http.cookie.SM) org.jetbrains.plugins.ipnb.format.cells.output(org.jetbrains.plugins.ipnb.format.cells.output) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URIBuilder(org.apache.http.client.utils.URIBuilder) StringUtil(com.intellij.openapi.util.text.StringUtil) ContentType(org.apache.http.entity.ContentType) Draft_17(org.java_websocket.drafts.Draft_17) UUID(java.util.UUID) ClientHandshakeBuilder(org.java_websocket.handshake.ClientHandshakeBuilder) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) java.io(java.io) ObjectUtils(com.intellij.util.ObjectUtils) NotNull(org.jetbrains.annotations.NotNull) com.google.gson(com.google.gson) URIBuilder(org.apache.http.client.utils.URIBuilder) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 74 with URIBuilder

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

the class TransferFsImage method uploadImage.

/*
   * Uploads the imagefile using HTTP PUT method
   */
private static void uploadImage(URL url, Configuration conf, NNStorage storage, NameNodeFile nnf, long txId, Canceler canceler) throws IOException {
    File imageFile = storage.findImageFile(nnf, txId);
    if (imageFile == null) {
        throw new IOException("Could not find image with txid " + txId);
    }
    HttpURLConnection connection = null;
    try {
        URIBuilder uriBuilder = new URIBuilder(url.toURI());
        // write all params for image upload request as query itself.
        // Request body contains the image to be uploaded.
        Map<String, String> params = ImageServlet.getParamsForPutImage(storage, txId, imageFile.length(), nnf);
        for (Entry<String, String> entry : params.entrySet()) {
            uriBuilder.addParameter(entry.getKey(), entry.getValue());
        }
        URL urlWithParams = uriBuilder.build().toURL();
        connection = (HttpURLConnection) connectionFactory.openConnection(urlWithParams, UserGroupInformation.isSecurityEnabled());
        // Set the request to PUT
        connection.setRequestMethod("PUT");
        connection.setDoOutput(true);
        int chunkSize = conf.getInt(DFSConfigKeys.DFS_IMAGE_TRANSFER_CHUNKSIZE_KEY, DFSConfigKeys.DFS_IMAGE_TRANSFER_CHUNKSIZE_DEFAULT);
        if (imageFile.length() > chunkSize) {
            // using chunked streaming mode to support upload of 2GB+ files and to
            // avoid internal buffering.
            // this mode should be used only if more than chunkSize data is present
            // to upload. otherwise upload may not happen sometimes.
            connection.setChunkedStreamingMode(chunkSize);
        }
        setTimeout(connection);
        // set headers for verification
        ImageServlet.setVerificationHeadersForPut(connection, imageFile);
        // Write the file to output stream.
        writeFileToPutRequest(conf, connection, imageFile, canceler);
        int responseCode = connection.getResponseCode();
        if (responseCode != HttpURLConnection.HTTP_OK) {
            throw new HttpPutFailedException(String.format("Image uploading failed, status: %d, url: %s, message: %s", responseCode, urlWithParams, connection.getResponseMessage()), responseCode);
        }
    } catch (AuthenticationException | URISyntaxException e) {
        throw new IOException(e);
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}
Also used : AuthenticationException(org.apache.hadoop.security.authentication.client.AuthenticationException) HttpPutFailedException(org.apache.hadoop.hdfs.server.common.HttpPutFailedException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) URIBuilder(org.apache.http.client.utils.URIBuilder) HttpURLConnection(java.net.HttpURLConnection) File(java.io.File) NameNodeFile(org.apache.hadoop.hdfs.server.namenode.NNStorage.NameNodeFile)

Example 75 with URIBuilder

use of org.apache.http.client.utils.URIBuilder in project jabref by JabRef.

the class ArXiv method callApi.

/**
     * Queries the API.
     *
     * If only {@code searchQuery} is given, then the API will return results for each article that matches the query.
     * If only {@code ids} is given, then the API will return results for each article in the list.
     * If both {@code searchQuery} and {@code ids} are given, then the API will return each article in
     * {@code ids} that matches {@code searchQuery}. This allows the API to act as a results filter.
     *
     * @param searchQuery the search query used to find articles;
     *                    <a href="http://arxiv.org/help/api/user-manual#query_details">details</a>
     * @param ids         a list of arXiv identifiers
     * @param start       the index of the first returned result (zero-based)
     * @param maxResults  the number of maximal results (has to be smaller than 2000)
     * @return the response from the API as a XML document (Atom 1.0)
     * @throws FetcherException if there was a problem while building the URL or the API was not accessible
     */
private Document callApi(String searchQuery, List<ArXivIdentifier> ids, int start, int maxResults) throws FetcherException {
    if (maxResults > 2000) {
        throw new IllegalArgumentException("The arXiv API limits the number of maximal results to be 2000");
    }
    try {
        URIBuilder uriBuilder = new URIBuilder(API_URL);
        // The arXiv API has problems with accents, so we remove them (i.e. Fréchet -> Frechet)
        if (StringUtil.isNotBlank(searchQuery)) {
            uriBuilder.addParameter("search_query", StringUtil.stripAccents(searchQuery));
        }
        if (!ids.isEmpty()) {
            uriBuilder.addParameter("id_list", ids.stream().map(ArXivIdentifier::getNormalized).collect(Collectors.joining(",")));
        }
        uriBuilder.addParameter("start", String.valueOf(start));
        uriBuilder.addParameter("max_results", String.valueOf(maxResults));
        URL url = uriBuilder.build().toURL();
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        if (connection.getResponseCode() == 400) {
            // Bad request error from server, try to get more information
            throw getException(builder.parse(connection.getErrorStream()));
        } else {
            return builder.parse(connection.getInputStream());
        }
    } catch (SAXException | ParserConfigurationException | IOException | URISyntaxException exception) {
        throw new FetcherException("arXiv API request failed", exception);
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) URL(java.net.URL) URIBuilder(org.apache.http.client.utils.URIBuilder) SAXException(org.xml.sax.SAXException) FetcherException(org.jabref.logic.importer.FetcherException) HttpURLConnection(java.net.HttpURLConnection) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ArXivIdentifier(org.jabref.model.entry.identifier.ArXivIdentifier) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

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