Skip to content

Commit 652f8e9

Browse files
authored
Merge pull request #63 from vinscom/develop
Minor fixes
2 parents b44aaf2 + c333bff commit 652f8e9

File tree

4 files changed

+50
-17
lines changed

4 files changed

+50
-17
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>in.erail</groupId>
66
<artifactId>api-framework</artifactId>
7-
<version>2.4.5</version>
7+
<version>2.4.6</version>
88
<packaging>jar</packaging>
99
<developers>
1010
<developer>
@@ -183,7 +183,7 @@
183183
<dependency>
184184
<groupId>in.erail</groupId>
185185
<artifactId>glue</artifactId>
186-
<version>2.4.5</version>
186+
<version>2.4.6</version>
187187
</dependency>
188188
<dependency>
189189
<groupId>org.mockito</groupId>

src/main/java/in/erail/route/OpenAPI3RouteBuilder.java

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.vertx.reactivex.ext.web.api.contract.openapi3.OpenAPI3RouterFactory;
2828
import in.erail.service.RESTService;
2929
import io.netty.handler.codec.http.HttpResponseStatus;
30+
import io.reactivex.Observable;
3031
import io.vertx.reactivex.core.buffer.Buffer;
3132
import io.vertx.reactivex.ext.web.Cookie;
3233
import java.util.Arrays;
@@ -50,7 +51,7 @@ public class OpenAPI3RouteBuilder extends AbstractRouterBuilderImpl {
5051
private HashMap<String, Metered> mMetrics = new HashMap<>();
5152
private MetricRegistry mMetricRegistry;
5253
private String mRequestIdHeaderName = HEADER_X_REQUEST_ID;
53-
54+
5455
public File getOpenAPI3File() {
5556
return mOpenAPI3File;
5657
}
@@ -254,33 +255,65 @@ public Router getRouter(Router pRouter) {
254255
return;
255256
}
256257

257-
routingContext.user().isAuthorized(service.getAuthority(), (event) -> {
258-
boolean authSuccess = event.succeeded() ? event.result() : false;
259-
if (authSuccess) {
260-
process(routingContext, service.getServiceUniqueId());
261-
} else {
262-
routingContext.fail(401);
263-
}
264-
});
258+
Optional<String[]> authorities = Optional.ofNullable(service.getAuthority());
259+
260+
if (authorities.isPresent()) {
261+
Observable
262+
.fromArray(authorities.get())
263+
.flatMapSingle(a -> routingContext.user().rxIsAuthorized(a))
264+
.filter(a -> a)
265+
.firstElement()
266+
.subscribe((success) -> {
267+
getLog().trace(() -> "Request is authorized. Processing Message:" + service.getOperationId());
268+
process(routingContext, service.getServiceUniqueId());
269+
}, (err) -> {
270+
getLog().error(() -> "Error processing serive message", err);
271+
routingContext.fail(err);
272+
}, () -> {
273+
getLog().warn(() -> "You are not authorized to access service:" + service.getOperationId() + ":" + routingContext.toString());
274+
routingContext.fail(401);
275+
});
276+
} else {
277+
getLog().error(() -> "Service marked as secure, but, no authority defined at service level:" + service.getOperationId());
278+
routingContext.fail(401);
279+
}
280+
265281
} else {
266282
getLog().warn("Security disabled for " + service.getServiceUniqueId());
267283
process(routingContext, service.getServiceUniqueId());
268284
}
269285
});
270286

271287
apiFactory.addFailureHandlerByOperationId(service.getOperationId(), (routingContext) -> {
288+
int respStatusCode = routingContext.statusCode();
289+
if (respStatusCode == -1) {
290+
respStatusCode = 400;
291+
}
292+
293+
getLog().debug(() -> "API Failure Handle called:" + service.getOperationId() + ":" + generateErrorResponse(routingContext));
294+
272295
routingContext
273296
.response()
274-
.setStatusCode(HttpResponseStatus.BAD_REQUEST.code())
297+
.setStatusCode(respStatusCode)
275298
.end(generateErrorResponse(routingContext));
276299
});
277300
apiFactory.setValidationFailureHandler((routingContext) -> {
301+
int respStatusCode = routingContext.statusCode();
302+
if (respStatusCode == -1) {
303+
respStatusCode = 400;
304+
}
305+
306+
getLog().debug(() -> "API Validation Failer Handle called:" + service.getOperationId() + ":" + generateErrorResponse(routingContext));
307+
278308
routingContext
279309
.response()
280-
.setStatusCode(HttpResponseStatus.BAD_REQUEST.code())
310+
.setStatusCode(respStatusCode)
281311
.end(generateErrorResponse(routingContext));
282312
});
283313
apiFactory.setNotImplementedFailureHandler((routingContext) -> {
314+
315+
getLog().debug(() -> "API not implemented:" + service.getOperationId() + ":" + generateErrorResponse(routingContext));
316+
284317
routingContext
285318
.response()
286319
.setStatusCode(HttpResponseStatus.BAD_REQUEST.code())

src/main/java/in/erail/service/RESTService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ default Event createEvent(RequestEvent pRequest) throws InstantiationException,
2929

3030
Maybe<Event> handleEvent(Event pEvent);
3131

32-
String getAuthority();
32+
String[] getAuthority();
3333

3434
boolean isSecure();
3535
}

src/main/java/in/erail/service/RESTServiceImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public abstract class RESTServiceImpl implements RESTService, MaybeTransformer<E
3333
private Scheduler mScheduler = Schedulers.io();
3434
private Event mDefaultEvent;
3535
private boolean mSecure = false;
36-
private String mAuthority;
36+
private String[] mAuthority;
3737
private Class<? extends RequestEvent> mRequestEventClass = RequestEvent.class;
3838
private Class<? extends ResponseEvent> mResponseEventClass = ResponseEvent.class;
3939
private MaybeTransformer<Event, Event> mPreProcessProcessors[];
@@ -173,11 +173,11 @@ public void setSecure(boolean pSecure) {
173173
}
174174

175175
@Override
176-
public String getAuthority() {
176+
public String[] getAuthority() {
177177
return mAuthority;
178178
}
179179

180-
public void setAuthority(String pAuthority) {
180+
public void setAuthority(String[] pAuthority) {
181181
this.mAuthority = pAuthority;
182182
}
183183

0 commit comments

Comments
 (0)