/** * * This is part of the internal API. * This is a POST request. * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/issues/edit_comment">Further information about this action online (including a response example)</a> * @since 3.6 */ public String editComment(EditCommentRequest request) { return call( new PostRequest(path("edit_comment")) .setParam("comment", request.getComment()) .setParam("text", request.getText()) .setMediaType(MediaTypes.JSON)).content(); }
/** * * This is part of the internal API. * This is a POST request. * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/system/restart">Further information about this action online (including a response example)</a> * @since 4.3 */ public void restart() { call( new PostRequest(path("restart")) .setMediaType(MediaTypes.JSON) ).content(); }
/** * * This is part of the internal API. * This is a POST request. * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/project_analyses/create_event">Further information about this action online (including a response example)</a> * @since 6.3 */ public CreateEventResponse createEvent(CreateEventRequest request) { return call( new PostRequest(path("create_event")) .setParam("analysis", request.getAnalysis()) .setParam("category", request.getCategory()) .setParam("name", request.getName()), CreateEventResponse.parser()); }
long startTime = System.currentTimeMillis(); PostRequest.Part filePart = new PostRequest.Part(MediaTypes.ZIP, report); PostRequest post = new PostRequest("api/ce/submit") .setMediaType(MediaTypes.PROTOBUF) .setParam("organization", properties.organizationKey().orElse(null)) .setParam("projectKey", moduleHierarchy.root().key()) .setParam("projectName", moduleHierarchy.root().getOriginalName()) .setParam("projectBranch", moduleHierarchy.root().getBranch()) .setPart("report", filePart); post.setParam(CHARACTERISTIC, "branch=" + branchName); post.setParam(CHARACTERISTIC, "branchType=" + branchConfiguration.branchType().name()); } else { post.setParam(CHARACTERISTIC, "pullRequest=" + branchConfiguration.pullRequestKey());
@Test public void add_part() throws IOException { PostRequest request = new PostRequest("api/issues/search"); File reportFile = temp.newFile(); request.setPart("report", new PostRequest.Part(MediaTypes.JSON, reportFile)); assertThat(request.getParts()).hasSize(1); PostRequest.Part part = request.getParts().get("report"); assertThat(part.getMediaType()).isEqualTo(MediaTypes.JSON); assertThat(part.getFile()).isSameAs(reportFile); } }
@Test public void empty_parts_and_params_by_default() { PostRequest request = new PostRequest("api/issues/search"); assertThat(request.getParts()).isEmpty(); assertThat(request.getParams()).isEmpty(); }
@Test public void override_timeout_on_post() { underTest = HttpConnector.newBuilder().url(serverUrl).build(); // Headers are not affected by setBodyDelay, let's throttle the answer server.enqueue(new MockResponse().throttleBody(1,100, TimeUnit.MILLISECONDS).setBody("Hello delayed")); expectedException.expect(IllegalStateException.class); expectedException.expectCause(IsInstanceOf.instanceOf(SocketTimeoutException.class)); WsResponse call = underTest.call(new PostRequest("/").setTimeOutInMs(5)); assertThat(call.content()).equals("Hello delayed"); }
@Test public void post_is_post() { PostRequest request = new PostRequest("api/issues/search"); assertThat(request.getMethod()).isEqualTo(WsRequest.Method.POST); }
@Test public void add_header_to_POST_request() throws Exception { answerHelloWorld(); PostRequest request = new PostRequest("api/issues/search") .setHeader("X-Foo", "fooz") .setHeader("X-Bar", "barz"); underTest = HttpConnector.newBuilder().url(serverUrl).build(); underTest.call(request); RecordedRequest recordedRequest = server.takeRequest(); assertThat(recordedRequest.getHeader("X-Foo")).isEqualTo("fooz"); assertThat(recordedRequest.getHeader("X-Bar")).isEqualTo("barz"); }
private WsResponse post(PostRequest postRequest) { HttpUrl.Builder urlBuilder = prepareUrlBuilder(postRequest); RequestBody body; Map<String, PostRequest.Part> parts = postRequest.getParts(); if (parts.isEmpty()) { // parameters are defined in the body (application/x-www-form-urlencoded) FormBody.Builder formBody = new FormBody.Builder(); postRequest.getParameters().getKeys() .forEach(key -> postRequest.getParameters().getValues(key) .forEach(value -> formBody.add(key, value))); body = formBody.build(); } else { // parameters are defined in the URL (as GET) completeUrlQueryParameters(postRequest, urlBuilder); MultipartBody.Builder bodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); parts.entrySet().forEach(param -> { PostRequest.Part part = param.getValue(); bodyBuilder.addFormDataPart( param.getKey(), part.getFile().getName(), RequestBody.create(MediaType.parse(part.getMediaType()), part.getFile())); }); body = bodyBuilder.build(); } Request.Builder okRequestBuilder = prepareOkRequestBuilder(postRequest, urlBuilder).post(body); Response response = doCall(prepareOkHttpClient(noRedirectOkHttpClient, postRequest), okRequestBuilder.build()); response = checkRedirect(response, postRequest); return new OkHttpResponse(response); }
@Test public void http_error() throws Exception { server.enqueue(new MockResponse().setResponseCode(404)); PostRequest request = new PostRequest("api/issues/search"); underTest = HttpConnector.newBuilder().url(serverUrl).build(); WsResponse wsResponse = underTest.call(request); assertThat(wsResponse.code()).isEqualTo(404); }
/** * * This is part of the internal API. * This is a POST request. * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/user_groups/update">Further information about this action online (including a response example)</a> * @since 5.2 */ public void update(UpdateRequest request) { call( new PostRequest(path("update")) .setParam("description", request.getDescription()) .setParam("id", request.getId()) .setParam("name", request.getName()), UpdateWsResponse.parser()); }
@Test public void upload_file() throws Exception { answerHelloWorld(); File reportFile = temp.newFile(); FileUtils.write(reportFile, "the report content"); PostRequest request = new PostRequest("api/report/upload") .setParam("project", "theKey") .setPart("report", new PostRequest.Part(MediaTypes.TXT, reportFile)) .setMediaType(MediaTypes.PROTOBUF); underTest = HttpConnector.newBuilder().url(serverUrl).build(); WsResponse response = underTest.call(request); assertThat(response.hasContent()).isTrue(); RecordedRequest recordedRequest = server.takeRequest(); assertThat(recordedRequest.getMethod()).isEqualTo("POST"); assertThat(recordedRequest.getPath()).isEqualTo("/api/report/upload?project=theKey"); String body = IOUtils.toString(recordedRequest.getBody().inputStream()); assertThat(body) .contains("Content-Disposition: form-data; name=\"report\"") .contains("Content-Type: text/plain") .contains("the report content"); }
/** * * This is part of the internal API. * This is a POST request. * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/ce/cancel_all">Further information about this action online (including a response example)</a> * @since 5.2 */ public void cancelAll() { call( new PostRequest(path("cancel_all")) .setMediaType(MediaTypes.JSON) ).content(); }
@Test public void override_timeout_on_post_with_redirect() { underTest = HttpConnector.newBuilder().url(serverUrl).build(); server.enqueue(new MockResponse().setResponseCode(301).setHeader("Location:", "/redirect")); // Headers are not affected by setBodyDelay, let's throttle the answer server.enqueue(new MockResponse().throttleBody(1,100, TimeUnit.MILLISECONDS).setBody("Hello delayed")); expectedException.expect(IllegalStateException.class); expectedException.expectCause(IsInstanceOf.instanceOf(SocketTimeoutException.class)); WsResponse call = underTest.call(new PostRequest("/").setTimeOutInMs(5)); assertThat(call.content()).equals("Hello delayed"); }
private WsResponse post(PostRequest postRequest) { HttpUrl.Builder urlBuilder = prepareUrlBuilder(postRequest); RequestBody body; Map<String, PostRequest.Part> parts = postRequest.getParts(); if (parts.isEmpty()) { // parameters are defined in the body (application/x-www-form-urlencoded) FormBody.Builder formBody = new FormBody.Builder(); postRequest.getParameters().getKeys() .forEach(key -> postRequest.getParameters().getValues(key) .forEach(value -> formBody.add(key, value))); body = formBody.build(); } else { // parameters are defined in the URL (as GET) completeUrlQueryParameters(postRequest, urlBuilder); MultipartBody.Builder bodyBuilder = new MultipartBody.Builder().setType(MultipartBody.FORM); parts.entrySet().forEach(param -> { PostRequest.Part part = param.getValue(); bodyBuilder.addFormDataPart( param.getKey(), part.getFile().getName(), RequestBody.create(MediaType.parse(part.getMediaType()), part.getFile())); }); body = bodyBuilder.build(); } Request.Builder okRequestBuilder = prepareOkRequestBuilder(postRequest, urlBuilder).post(body); Response response = doCall(prepareOkHttpClient(noRedirectOkHttpClient, postRequest), okRequestBuilder.build()); response = checkRedirect(response, postRequest); return new OkHttpResponse(response); }
/** * * This is part of the internal API. * This is a POST request. * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/organizations/remove_member">Further information about this action online (including a response example)</a> * @since 6.4 */ public void removeMember(RemoveMemberRequest request) { call( new PostRequest(path("remove_member")) .setParam("login", request.getLogin()) .setParam("organization", request.getOrganization()) .setMediaType(MediaTypes.JSON) ).content(); }
/** * * This is part of the internal API. * This is a POST request. * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/user_groups/create">Further information about this action online (including a response example)</a> * @since 5.2 */ public CreateWsResponse create(CreateRequest request) { return call( new PostRequest(path("create")) .setParam("description", request.getDescription()) .setParam("name", request.getName()) .setParam("organization", request.getOrganization()), CreateWsResponse.parser()); }
/** * Uploads the report file to server and returns the generated task id */ @VisibleForTesting String upload(File report) { LOG.debug("Upload report"); long startTime = System.currentTimeMillis(); ProjectDefinition projectDefinition = projectReactor.getRoot(); PostRequest.Part filePart = new PostRequest.Part(MediaTypes.ZIP, report); PostRequest post = new PostRequest("api/ce/submit") .setMediaType(MediaTypes.PROTOBUF) .setParam("projectKey", projectDefinition.getKey()) .setParam("projectName", projectDefinition.getName()) .setParam("projectBranch", projectDefinition.getBranch()) .setPart("report", filePart); WsResponse response = wsClient.call(post).failIfNotSuccessful(); try (InputStream protobuf = response.contentStream()) { return WsCe.SubmitResponse.parser().parseFrom(protobuf).getTaskId(); } catch (Exception e) { throw Throwables.propagate(e); } finally { long stopTime = System.currentTimeMillis(); LOG.info("Analysis report uploaded in " + (stopTime - startTime) + "ms"); } }
/** * * This is part of the internal API. * This is a POST request. * @see <a href="https://next.sonarqube.com/sonarqube/web_api/api/ce/pause">Further information about this action online (including a response example)</a> * @since 7.2 */ public void pause() { call( new PostRequest(path("pause")) .setMediaType(MediaTypes.JSON) ).content(); }