Skip to content

Commit 166dc6c

Browse files
committed
Merge remote-tracking branch 'upstream/master' into mavenize
# Conflicts: # opengrok-indexer/src/main/java/org/opengrok/indexer/web/constraints/PositiveDuration.java # opengrok-indexer/src/main/java/org/opengrok/indexer/web/constraints/PositiveDurationValidator.java # opengrok-indexer/src/main/java/org/opengrok/indexer/web/messages/Message.java # opengrok-indexer/src/test/java/org/opengrok/indexer/web/constraints/PositiveDurationValidatorTest.java # opengrok-web/src/main/java/org/opengrok/web/api/v1/RestApp.java # opengrok-web/src/main/java/org/opengrok/web/constraints/ValidationExceptionMapper.java # src/org/opensolaris/opengrok/web/api/constraints/PositiveDuration.java # src/org/opensolaris/opengrok/web/api/constraints/PositiveDurationValidator.java # src/org/opensolaris/opengrok/web/api/error/ValidationExceptionMapper.java # src/org/opensolaris/opengrok/web/constraints/PositiveDuration.java # src/org/opensolaris/opengrok/web/constraints/PositiveDurationValidator.java # src/org/opensolaris/opengrok/web/constraints/ValidationExceptionMapper.java # test/org/opensolaris/opengrok/web/api/constraint/PositiveDurationValidatorTest.java # test/org/opensolaris/opengrok/web/constraint/PositiveDurationValidatorTest.java
2 parents 8839fe5 + 493447d commit 166dc6c

File tree

9 files changed

+156
-12
lines changed

9 files changed

+156
-12
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/web/messages/Message.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
3333
import org.hibernate.validator.constraints.NotBlank;
3434
import org.hibernate.validator.constraints.NotEmpty;
35-
import org.opengrok.indexer.web.constraints.PositiveDuration;
35+
import org.opengrok.indexer.web.api.constraints.PositiveDuration;
3636

3737
import java.io.IOException;
3838
import java.time.Duration;

opengrok-web/src/main/java/org/opengrok/web/api/v1/RestApp.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public class RestApp extends ResourceConfig {
3232
public static final String API_PATH = "/api/v1";
3333

3434
public RestApp() {
35-
packages("org.opengrok.web.api.v1.controller", "org.opengrok.web.api.v1.filter");
35+
packages("org.opengrok.web.api.constraints", "org.opengrok.web.api.error");
36+
packages(true, "org.opengrok.web.api.v1");
3637
}
3738

3839
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/*
2121
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
2222
*/
23-
package org.opengrok.indexer.web.constraints;
23+
package org.opensolaris.opengrok.web.api.constraints;
2424

2525
import javax.validation.Constraint;
2626
import javax.validation.Payload;
@@ -37,7 +37,7 @@
3737
@Retention(RetentionPolicy.RUNTIME)
3838
public @interface PositiveDuration {
3939

40-
String message() default "{org.opengrok.indexer.web.constraints.PositiveDuration.message}";
40+
String message() default "{org.opensolaris.opengrok.web.constraints.PositiveDuration.message}";
4141

4242
Class<?>[] groups() default {};
4343

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
/*
2121
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
2222
*/
23-
package org.opengrok.indexer.web.constraints;
23+
package org.opensolaris.opengrok.web.api.constraints;
2424

2525
import javax.validation.ConstraintValidator;
2626
import javax.validation.ConstraintValidatorContext;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opensolaris.opengrok.web.api.error;
24+
25+
import javax.ws.rs.core.MediaType;
26+
import javax.ws.rs.core.Response;
27+
28+
public class ExceptionMapperUtils {
29+
30+
private ExceptionMapperUtils() {
31+
}
32+
33+
/**
34+
* Turns the exception into JSON format and embeds it into the response with the provided status.
35+
* @param status status of the created response
36+
* @param e exception to embed into the response
37+
* @return response with the {@code status} and JSON media type with encoded {@code e} in the body
38+
*/
39+
public static Response toResponse(final Response.Status status, final Exception e) {
40+
return Response.status(status)
41+
.entity(new ExceptionModel(e.getMessage()))
42+
.type(MediaType.APPLICATION_JSON)
43+
.build();
44+
}
45+
46+
private static class ExceptionModel {
47+
48+
private String message;
49+
50+
ExceptionModel(final String message) {
51+
this.message = message;
52+
}
53+
54+
public String getMessage() {
55+
return message;
56+
}
57+
58+
public void setMessage(final String message) {
59+
this.message = message;
60+
}
61+
}
62+
63+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opensolaris.opengrok.web.api.error;
24+
25+
import org.opensolaris.opengrok.logger.LoggerFactory;
26+
27+
import javax.ws.rs.core.Response;
28+
import javax.ws.rs.ext.ExceptionMapper;
29+
import javax.ws.rs.ext.Provider;
30+
import java.util.logging.Level;
31+
import java.util.logging.Logger;
32+
33+
@Provider
34+
public class GenericExceptionMapper implements ExceptionMapper<Exception> {
35+
36+
private static final Logger logger = LoggerFactory.getLogger(GenericExceptionMapper.class);
37+
38+
@Override
39+
public Response toResponse(final Exception e) {
40+
logger.log(Level.WARNING, "Exception while processing request", e);
41+
42+
return ExceptionMapperUtils.toResponse(Response.Status.INTERNAL_SERVER_ERROR, e);
43+
}
44+
45+
}

opengrok-web/src/main/java/org/opengrok/web/constraints/ValidationExceptionMapper.java renamed to src/org/opensolaris/opengrok/web/api/error/ValidationExceptionMapper.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020
/*
2121
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
2222
*/
23-
package org.opengrok.web.constraints;
23+
package org.opensolaris.opengrok.web.api.error;
2424

2525
import javax.validation.ValidationException;
26-
import javax.ws.rs.core.MediaType;
2726
import javax.ws.rs.core.Response;
2827
import javax.ws.rs.ext.ExceptionMapper;
2928
import javax.ws.rs.ext.Provider;
@@ -33,10 +32,7 @@ public class ValidationExceptionMapper implements ExceptionMapper<ValidationExce
3332

3433
@Override
3534
public Response toResponse(final ValidationException e) {
36-
return Response.status(Response.Status.BAD_REQUEST)
37-
.entity(e.getMessage())
38-
.type(MediaType.TEXT_PLAIN)
39-
.build();
35+
return ExceptionMapperUtils.toResponse(Response.Status.BAD_REQUEST, e);
4036
}
4137

4238
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opensolaris.opengrok.web.api.error;
24+
25+
import javax.ws.rs.WebApplicationException;
26+
import javax.ws.rs.core.Response;
27+
import javax.ws.rs.ext.ExceptionMapper;
28+
import javax.ws.rs.ext.Provider;
29+
30+
@Provider
31+
public class WebApplicationExceptionMapper implements ExceptionMapper<WebApplicationException> {
32+
33+
@Override
34+
public Response toResponse(final WebApplicationException e) {
35+
return e.getResponse();
36+
}
37+
38+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
/*
2121
* Copyright (c) 2018 Oracle and/or its affiliates. All rights reserved.
2222
*/
23-
package org.opengrok.indexer.web.constraints;
23+
package org.opensolaris.opengrok.web.api.constraint;
2424

2525
import org.junit.Test;
26+
import org.opensolaris.opengrok.web.api.constraints.PositiveDurationValidator;
2627

2728
import java.time.Duration;
2829

0 commit comments

Comments
 (0)