Skip to content

Commit

Permalink
remove occurrences of hard-coded HTTP results and replace them with …
Browse files Browse the repository at this point in the history
…the corresponding values of code.HttpStatus
  • Loading branch information
bagechengzi committed Oct 27, 2023
1 parent 5987161 commit 5f34277
Show file tree
Hide file tree
Showing 22 changed files with 59 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.streampipes.client.api;

import org.apache.http.HttpStatus;
import org.apache.streampipes.client.http.DeleteRequest;
import org.apache.streampipes.client.http.GetRequest;
import org.apache.streampipes.client.http.PostRequestWithPayloadResponse;
Expand Down Expand Up @@ -89,7 +90,7 @@ protected <T> Optional<T> getSingleOpt(StreamPipesApiPath apiPath,
ObjectSerializer<Void, T> serializer = new ObjectSerializer<>();
return Optional.of(new GetRequest<>(clientConfig, apiPath, targetClass, serializer).executeRequest());
} catch (SpHttpErrorStatusCode e) {
if (e.getHttpStatusCode() == 404) {
if (e.getHttpStatusCode() == HttpStatus.SC_NOT_FOUND) {
return Optional.empty();
} else {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.streampipes.client.api;

import org.apache.http.HttpStatus;
import org.apache.streampipes.client.http.GetRequest;
import org.apache.streampipes.client.model.StreamPipesClientConfig;
import org.apache.streampipes.client.serializer.ListSerializer;
Expand Down Expand Up @@ -47,7 +48,7 @@ protected Optional<T> getSingle(StreamPipesApiPath apiPath) throws SpRuntimeExce
try {
return Optional.of(new GetRequest<>(clientConfig, apiPath, targetClass, serializer).executeRequest());
} catch (SpHttpErrorStatusCode e) {
if (e.getHttpStatusCode() == 404) {
if (e.getHttpStatusCode() == HttpStatus.SC_NOT_FOUND) {
return Optional.empty();
} else {
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ public T executeRequest() {
switch (status.getStatusCode()) {
case HttpStatus.SC_UNAUTHORIZED -> throw new SpHttpErrorStatusCode(
" 401 - Access to this resource is forbidden - did you provide a poper API key or client secret?",
401);
HttpStatus.SC_UNAUTHORIZED);
case HttpStatus.SC_NOT_FOUND ->
throw new SpHttpErrorStatusCode(" 404 - The requested resource could not be found.", 404);
throw new SpHttpErrorStatusCode(" 404 - The requested resource could not be found.", HttpStatus.SC_NOT_FOUND);
default -> throw new SpHttpErrorStatusCode(status.getStatusCode() + " - " + status.getReasonPhrase(),
status.getStatusCode());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.apache.streampipes.connect.management.management;

import org.apache.http.HttpStatus;
import org.apache.streampipes.commons.exceptions.SpConfigurationException;
import org.apache.streampipes.commons.exceptions.connect.AdapterException;
import org.apache.streampipes.connect.management.util.WorkerPaths;
Expand Down Expand Up @@ -107,7 +108,7 @@ private static void triggerAdapterStateChange(AdapterDescription ad,
var response = triggerPost(url, ad.getCorrespondingDataStreamElementId(), adapterDescription);
var responseString = getResponseBody(response);

if (response.getStatusLine().getStatusCode() != 200) {
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
var exception = getSerializer().readValue(responseString, AdapterException.class);
throw new AdapterException(exception.getMessage(), exception.getCause());
}
Expand Down Expand Up @@ -145,7 +146,7 @@ public static RuntimeOptionsResponse getConfiguration(String workerEndpoint,

String responseString = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8);

if (response.getStatusLine().getStatusCode() == 200) {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
return getSerializer().readValue(responseString, RuntimeOptionsResponse.class);
} else {
var exception = getSerializer().readValue(responseString, SpConfigurationException.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.apache.streampipes.connect.adapters.netio;

import org.apache.http.HttpStatus;
import org.apache.streampipes.commons.exceptions.connect.AdapterException;
import org.apache.streampipes.connect.adapters.netio.model.NetioAllPowerOutputs;
import org.apache.streampipes.connect.adapters.netio.model.NetioPowerOutput;
Expand Down Expand Up @@ -159,7 +160,7 @@ public GuessSchema onSchemaRequested(IAdapterParameterExtractor extractor,
requestData();
return NetioUtils.getNetioSchema();
} catch (IOException e) {
if (e instanceof HttpResponseException && ((HttpResponseException) e).getStatusCode() == 401) {
if (e instanceof HttpResponseException && ((HttpResponseException) e).getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
throw new AdapterException(
"Unauthorized! Could not connect to NETIO sensor: " + this.ip + " with username " + this.username);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
//
// stubFor(get(urlEqualTo("/"))
// .willReturn(aResponse()
// .withStatus(200)
// .withStatus(HttpStatus.SC_OK)
// .withBody(expected)));
//
// String result = PullRestAdapter.getDataFromEndpointString("http://localhost:" + port + "/");
Expand All @@ -44,7 +44,7 @@
// public void getDataFromEndpointStringFail() throws AdapterException {
// stubFor(get(urlEqualTo("/"))
// .willReturn(aResponse()
// .withStatus(404)
// .withStatus(HttpStatus.SC_NOT_FOUND)
// .withBody("")));
//
// PullRestAdapter.getDataFromEndpointString("http://localhost:" + port + "/");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//
// stubFor(get(urlEqualTo("/"))
// .willReturn(aResponse()
// .withStatus(200)
// .withStatus(HttpStatus.SC_OK)
// .withBody(expected)));
//
//
Expand Down Expand Up @@ -77,7 +77,7 @@
//
// stubFor(get(urlEqualTo("/"))
// .willReturn(aResponse()
// .withStatus(200)
// .withStatus(HttpStatus.SC_OK)
// .withBody("Example response")));
//
//
Expand Down
4 changes: 4 additions & 0 deletions streampipes-integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.streampipes.integration.containers;

import org.apache.http.HttpStatus;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.HttpWaitStrategy;

Expand All @@ -35,7 +36,7 @@ public PulsarContainer() {
public void start() {
this.waitStrategy = new HttpWaitStrategy()
.forPort(BROKER_HTTP_PORT)
.forStatusCode(200)
.forStatusCode(HttpStatus.SC_OK)
.forPath("/admin/v2/namespaces/public/default")
.withStartupTimeout(Duration.of(300, SECONDS));
this.withExposedPorts(BROKER_SERVICE_PORT, BROKER_HTTP_PORT);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.apache.streampipes.manager.health;

import org.apache.http.HttpStatus;
import org.apache.streampipes.manager.execution.ExtensionServiceExecutions;
import org.apache.streampipes.model.extensions.svcdiscovery.SpServiceRegistration;
import org.apache.streampipes.storage.api.CRUDStorage;
Expand Down Expand Up @@ -53,7 +54,7 @@ private void checkServiceHealth(SpServiceRegistration service) {
try {
var request = ExtensionServiceExecutions.extServiceGetRequest(healthCheckUrl);
var response = request.execute();
if (response.returnResponse().getStatusLine().getStatusCode() != 200) {
if (response.returnResponse().getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
processUnhealthyService(service);
} else {
if (!service.isHealthy()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.streampipes.rest.extensions;

import jakarta.ws.rs.core.Response;
import org.apache.http.HttpStatus;

public class AbstractExtensionsResource {

Expand All @@ -31,13 +32,13 @@ protected <T> Response ok(T entity) {

protected Response clientError() {
return Response
.status(400)
.status(HttpStatus.SC_BAD_REQUEST)
.build();
}

protected Response serverError() {
return Response
.status(500)
.status(HttpStatus.SC_INTERNAL_SERVER_ERROR)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.apache.streampipes.rest.extensions;

import org.apache.http.HttpStatus;
import org.apache.streampipes.commons.constants.GlobalStreamPipesConstants;
import org.apache.streampipes.extensions.api.pe.IStreamPipesPipelineElement;
import org.apache.streampipes.extensions.management.assets.AssetZipGenerator;
Expand Down Expand Up @@ -88,7 +89,7 @@ public Response getIconAsset(@PathParam("appId") String appId) throws IOExceptio
return ok(Resources.toByteArray(iconUrl));
} catch (IllegalArgumentException e) {
LOG.warn("No icon resource found for pipeline element {}", appId);
return Response.status(400).build();
return Response.status(HttpStatus.SC_BAD_REQUEST).build();
}
}

Expand All @@ -101,7 +102,7 @@ public Response getDocumentationAsset(@PathParam("id") String elementId) throws
return ok(Resources.toString(documentationUrl, Charsets.UTF_8));
} catch (IllegalArgumentException e) {
LOG.warn("No documentation resource found for pipeline element {}", elementId);
return Response.status(400).build();
return Response.status(HttpStatus.SC_BAD_REQUEST).build();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.streampipes.rest.extensions.connect;

import org.apache.http.HttpStatus;
import org.apache.streampipes.extensions.management.connect.HttpServerAdapterManagement;

import jakarta.ws.rs.POST;
Expand All @@ -36,7 +37,7 @@ public Response receiveEvent(@PathParam("endpointId") String endpointId,
HttpServerAdapterManagement.INSTANCE.notify(endpointId, body);
return Response.ok().build();
} catch (Exception e) {
return Response.status(400).entity(e.getMessage()).build();
return Response.status(HttpStatus.SC_BAD_REQUEST).entity(e.getMessage()).build();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.apache.streampipes.rest.extensions.connect;

import org.apache.http.HttpStatus;
import org.apache.streampipes.commons.exceptions.SpConfigurationException;
import org.apache.streampipes.commons.exceptions.SpRuntimeException;
import org.apache.streampipes.extensions.api.runtime.ResolvesContainerProvidedOptions;
Expand Down Expand Up @@ -65,7 +66,7 @@ public Response fetchConfigurations(@PathParam("id") String elementId,
}
} catch (SpConfigurationException e) {
return jakarta.ws.rs.core.Response
.status(400)
.status(HttpStatus.SC_BAD_REQUEST)
.entity(e)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.apache.streampipes.rest.extensions.pe;

import org.apache.http.HttpStatus;
import org.apache.streampipes.extensions.api.pe.IStreamPipesDataStream;
import org.apache.streampipes.extensions.management.assets.AssetZipGenerator;
import org.apache.streampipes.extensions.management.init.DeclarersSingleton;
Expand Down Expand Up @@ -50,7 +51,7 @@ public jakarta.ws.rs.core.Response getAssets(@PathParam("streamId") String strea
.getIncludedAssets()).makeZip());
} catch (IOException e) {
e.printStackTrace();
return jakarta.ws.rs.core.Response.status(500).build();
return jakarta.ws.rs.core.Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).build();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.apache.streampipes.rest.extensions.pe;

import org.apache.http.HttpStatus;
import org.apache.streampipes.commons.environment.Environments;
import org.apache.streampipes.commons.exceptions.SpConfigurationException;
import org.apache.streampipes.commons.exceptions.SpRuntimeException;
Expand Down Expand Up @@ -130,11 +131,11 @@ public jakarta.ws.rs.core.Response fetchConfigurations(@PathParam("elementId") S
new RuntimeResolvableRequestHandler().handleRuntimeResponse((SupportsRuntimeConfig) declarer, req);
return ok(responseOptions);
} else {
return jakarta.ws.rs.core.Response.status(500).build();
return jakarta.ws.rs.core.Response.status(HttpStatus.SC_INTERNAL_SERVER_ERROR).build();
}
} catch (SpConfigurationException e) {
return jakarta.ws.rs.core.Response
.status(400)
.status(HttpStatus.SC_BAD_REQUEST)
.entity(e)
.build();
}
Expand Down
4 changes: 4 additions & 0 deletions streampipes-rest-shared/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.streampipes.rest.shared.impl;

import jakarta.ws.rs.core.Response;
import org.apache.http.HttpStatus;

public abstract class AbstractSharedRestInterface {

Expand All @@ -28,19 +29,19 @@ protected <T> Response ok(T entity) {
}

protected <T> Response badRequest(T entity) {
return error(entity, 400);
return error(entity, HttpStatus.SC_BAD_REQUEST);
}

protected <T> Response notFound(T entity) {
return error(entity, 404);
return error(entity, HttpStatus.SC_NOT_FOUND);
}

protected <T> Response notFound() {
return Response.status(404).build();
return Response.status(HttpStatus.SC_NOT_FOUND).build();
}

protected <T> Response serverError(T entity) {
return error(entity, 500);
return error(entity, HttpStatus.SC_INTERNAL_SERVER_ERROR);
}

protected <T> Response error(T entity, Integer statusCode) {
Expand All @@ -51,15 +52,15 @@ protected <T> Response error(T entity, Integer statusCode) {
}

protected Response badRequest() {
return Response.status(400).build();
return Response.status(HttpStatus.SC_BAD_REQUEST).build();
}

protected Response ok() {
return Response.ok().build();
}

protected Response created() {
return Response.status(201).build();
return Response.status(HttpStatus.SC_CREATED).build();
}

protected Response fail() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package org.apache.streampipes.rest.impl;

import org.apache.http.HttpStatus;
import org.apache.streampipes.mail.MailSender;
import org.apache.streampipes.model.ShortUserInfo;
import org.apache.streampipes.model.client.user.ChangePasswordRequest;
Expand Down Expand Up @@ -241,7 +242,7 @@ public Response updateUsername(@PathParam("principalId") String principalId,
}
}

return Response.status(401).build();
return Response.status(HttpStatus.SC_UNAUTHORIZED).build();
}

@PUT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.apache.streampipes.rest.impl.connect;

import org.apache.http.HttpStatus;
import org.apache.streampipes.commons.exceptions.connect.AdapterException;
import org.apache.streampipes.connect.management.management.AdapterMasterManagement;
import org.apache.streampipes.connect.management.management.AdapterUpdateManagement;
Expand Down Expand Up @@ -167,7 +168,7 @@ public Response deleteAdapter(@PathParam("id") String elementId) {
return ok(Notifications.error(e.getMessage()));
}
} else {
return Response.status(409).entity(pipelinesUsingAdapter).build();
return Response.status(HttpStatus.SC_CONFLICT).entity(pipelinesUsingAdapter).build();
}
}

Expand Down
Loading

0 comments on commit 5f34277

Please sign in to comment.