Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map http 404 to Java Optional.empty [#1520] #1521

Merged
merged 4 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion streampipes-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,13 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<propertyExpansion>
checkstyle.config.base.path=${project.parent.basedir}/tools/maven
</propertyExpansion>
</configuration>
</plugin>
</plugins>
</build>

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import org.apache.streampipes.client.serializer.ListSerializer;
import org.apache.streampipes.client.serializer.ObjectSerializer;
import org.apache.streampipes.client.util.StreamPipesApiPath;
import org.apache.streampipes.commons.exceptions.SpHttpErrorStatusCode;
import org.apache.streampipes.commons.exceptions.SpRuntimeException;

import java.util.List;
import java.util.Optional;

public abstract class AbstractTypedClientApi<T> extends AbstractClientApi {

Expand All @@ -40,9 +42,17 @@ protected List<T> getAll(StreamPipesApiPath apiPath) throws SpRuntimeException {
return new GetRequest<>(clientConfig, apiPath, targetClass, serializer).executeRequest();
}

protected T getSingle(StreamPipesApiPath apiPath) throws SpRuntimeException {
protected Optional<T> getSingle(StreamPipesApiPath apiPath) throws SpRuntimeException {
ObjectSerializer<Void, T> serializer = new ObjectSerializer<>();
return new GetRequest<>(clientConfig, apiPath, targetClass, serializer).executeRequest();
try {
return Optional.of(new GetRequest<>(clientConfig, apiPath, targetClass, serializer).executeRequest());
} catch (SpHttpErrorStatusCode e) {
if (e.getHttpStatusCode() == 404) {
return Optional.empty();
} else {
throw e;
}
}
}

protected abstract StreamPipesApiPath getBaseResourcePath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@
package org.apache.streampipes.client.api;

import java.util.List;
import java.util.Optional;

public interface CRUDApi<K, V> {

V get(K id);
Optional<V> get(K id);

List<V> all();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.apache.streampipes.model.datalake.DataLakeMeasure;

import java.util.List;
import java.util.Optional;

public class DataLakeMeasureApi extends AbstractTypedClientApi<DataLakeMeasure>
implements CRUDApi<String, DataLakeMeasure> {
Expand All @@ -31,7 +32,7 @@ public DataLakeMeasureApi(StreamPipesClientConfig clientConfig) {
super(clientConfig, DataLakeMeasure.class);
}

public DataLakeMeasure get(String id) {
public Optional<DataLakeMeasure> get(String id) {
return getSingle(getBaseResourcePath().addToPath(id));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.streampipes.model.graph.DataProcessorInvocation;

import java.util.List;
import java.util.Optional;

public class DataProcessorApi extends AbstractTypedClientApi<DataProcessorInvocation>
implements CRUDApi<String, DataProcessorInvocation> {
Expand All @@ -43,7 +44,7 @@ protected StreamPipesApiPath getBaseResourcePath() {
}

@Override
public DataProcessorInvocation get(String s) {
public Optional<DataProcessorInvocation> get(String s) {
return getSingle(getBaseResourcePath().addToPath(s));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.streampipes.model.graph.DataSinkInvocation;

import java.util.List;
import java.util.Optional;

public class DataSinkApi extends AbstractTypedClientApi<DataSinkInvocation>
implements CRUDApi<String, DataSinkInvocation> {
Expand All @@ -36,7 +37,7 @@ public DataSinkApi(StreamPipesClientConfig clientConfig) {
}

@Override
public DataSinkInvocation get(String s) {
public Optional<DataSinkInvocation> get(String s) {
return getSingle(getBaseResourcePath().addToPath(s));
}

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

import java.net.URLEncoder;
import java.util.List;
import java.util.Optional;

public class DataStreamApi extends AbstractTypedClientApi<SpDataStream> implements CRUDApi<String, SpDataStream> {

Expand All @@ -36,7 +37,7 @@ public DataStreamApi(StreamPipesClientConfig clientConfig) {
}

@Override
public SpDataStream get(String streamId) {
public Optional<SpDataStream> get(String streamId) {
return getSingle(StreamPipesApiPath.fromBaseApiPath()
.addToPath("streams").addToPath(streamId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.streampipes.model.pipeline.PipelineOperationStatus;

import java.util.List;
import java.util.Optional;

public class PipelineApi extends AbstractTypedClientApi<Pipeline> implements CRUDApi<String, Pipeline> {

Expand All @@ -34,7 +35,7 @@ public PipelineApi(StreamPipesClientConfig clientConfig) {
}

@Override
public Pipeline get(String pipelineId) {
public Optional<Pipeline> get(String pipelineId) {
return getSingle(getBaseResourcePath().addToPath(pipelineId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.streampipes.model.template.PipelineElementTemplate;

import java.util.List;
import java.util.Optional;

public class PipelineElementTemplateApi extends AbstractTypedClientApi<PipelineElementTemplate>
implements CRUDApi<String, PipelineElementTemplate> {
Expand All @@ -31,7 +32,7 @@ public PipelineElementTemplateApi(StreamPipesClientConfig clientConfig) {
}

@Override
public PipelineElementTemplate get(String id) {
public Optional<PipelineElementTemplate> get(String id) {
return getSingle(getBaseResourcePath().addToPath(id));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.streampipes.client.model.StreamPipesClientConfig;
import org.apache.streampipes.client.serializer.Serializer;
import org.apache.streampipes.client.util.StreamPipesApiPath;
import org.apache.streampipes.commons.exceptions.SpHttpErrorStatusCode;
import org.apache.streampipes.commons.exceptions.SpRuntimeException;

import com.fasterxml.jackson.databind.ObjectMapper;
Expand Down Expand Up @@ -100,14 +101,19 @@ public T executeRequest() {
if (status.getStatusCode() == HttpStatus.SC_OK) {
return afterRequest(serializer, response.getEntity());
} else {
if (status.getStatusCode() == 401) {
throw new SpRuntimeException(
" 401 - Access to this resource is forbidden - did you provide a poper API key or client secret?");
} else {
throw new SpRuntimeException(status.getStatusCode() + " - " + status.getReasonPhrase());
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);
case HttpStatus.SC_NOT_FOUND:
throw new SpHttpErrorStatusCode(" 404 - The requested resource could not be found.", 404);
default:
throw new SpHttpErrorStatusCode(status.getStatusCode() + " - " + status.getReasonPhrase(),
status.getStatusCode());
}
}
} catch (IOException | SpRuntimeException e) {
} catch (IOException e) {
throw new SpRuntimeException(
"Could not connect to the StreamPipes API - please check that StreamPipes is available", e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.apache.streampipes.commons.exceptions;

public class SpHttpErrorStatusCode extends SpRuntimeException {

private static final long serialVersionUID = 2289470758226670270L;
private Integer httpStatusCode;

/**
* Creates a new Exception with the given message and null as the cause.
*
* @param message The exception message
*/
public SpHttpErrorStatusCode(String message, Integer httpStatusCode) {
super(message);
this.httpStatusCode = httpStatusCode;
}

public Integer getHttpStatusCode() {
return httpStatusCode;
}
}
2 changes: 1 addition & 1 deletion streampipes-rest-extensions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@
</plugins>
</build>

</project>
</project>
3 changes: 2 additions & 1 deletion streampipes-rest-shared/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
~
-->

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>streampipes-parent</artifactId>
<groupId>org.apache.streampipes</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ protected <T> Response badRequest(T entity) {
return error(entity, 400);
}

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

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

protected <T> Response error(T entity,
Integer statusCode) {
protected <T> Response error(T entity, Integer statusCode) {
return Response
.status(statusCode)
.entity(entity)
Expand Down
7 changes: 6 additions & 1 deletion streampipes-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,12 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
<configuration>
<propertyExpansion>
checkstyle.config.base.path=${project.parent.basedir}/tools/maven
</propertyExpansion>
</configuration>
</plugin>
</plugins>
</build>
</project>
</project>
Loading