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