updated license header

This commit is contained in:
2019-01-30 00:56:33 +01:00
parent 5c7abf4c47
commit 76795365c0
25 changed files with 990 additions and 875 deletions

View File

@ -1,62 +1,78 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-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"> <!--
<modelVersion>4.0.0</modelVersion> Copyright 2019 joern.muehlencord.
<parent>
<artifactId>shared</artifactId> Licensed under the Apache License, Version 2.0 (the "License");
<groupId>de.muehlencord</groupId> you may not use this file except in compliance with the License.
<version>1.1-SNAPSHOT</version> You may obtain a copy of the License at
</parent>
http://www.apache.org/licenses/LICENSE-2.0
<groupId>de.muehlencord.shared</groupId>
<artifactId>shared-jeeutil</artifactId> Unless required by applicable law or agreed to in writing, software
<version>1.1-SNAPSHOT</version> distributed under the License is distributed on an "AS IS" BASIS,
<packaging>ejb</packaging> WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
<name>shared-jeeutil</name> limitations under the License.
-->
<dependencies>
<dependency> <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">
<groupId>com.inversoft</groupId> <modelVersion>4.0.0</modelVersion>
<artifactId>prime-jwt</artifactId> <parent>
</dependency> <artifactId>shared</artifactId>
<dependency> <groupId>de.muehlencord</groupId>
<groupId>javax</groupId> <version>1.1-SNAPSHOT</version>
<artifactId>javaee-api</artifactId> </parent>
<scope>provided</scope>
</dependency> <groupId>de.muehlencord.shared</groupId>
<dependency> <artifactId>shared-jeeutil</artifactId>
<groupId>org.slf4j</groupId> <version>1.1-SNAPSHOT</version>
<artifactId>slf4j-api</artifactId> <packaging>ejb</packaging>
<scope>provided</scope>
</dependency> <name>shared-jeeutil</name>
<dependency>
<groupId>org.hibernate</groupId> <dependencies>
<artifactId>hibernate-core</artifactId> <dependency>
<scope>provided</scope> <groupId>com.inversoft</groupId>
</dependency> <artifactId>prime-jwt</artifactId>
<dependency> </dependency>
<groupId>org.primefaces</groupId> <dependency>
<artifactId>primefaces</artifactId> <groupId>javax</groupId>
<type>jar</type> <artifactId>javaee-api</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.shiro</groupId> <groupId>org.slf4j</groupId>
<artifactId>shiro-web</artifactId> <artifactId>slf4j-api</artifactId>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
</dependencies> <dependency>
<groupId>org.hibernate</groupId>
<build> <artifactId>hibernate-core</artifactId>
<plugins> <scope>provided</scope>
<plugin> </dependency>
<groupId>org.apache.maven.plugins</groupId> <dependency>
<artifactId>maven-ejb-plugin</artifactId> <groupId>org.primefaces</groupId>
<configuration> <artifactId>primefaces</artifactId>
<ejbVersion>3.1</ejbVersion> <type>jar</type>
</configuration> <scope>provided</scope>
</plugin> </dependency>
</plugins> <dependency>
</build> <groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
</project> <scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<ejbVersion>3.1</ejbVersion>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -1,3 +1,18 @@
/*
* Copyright 2019 joern.muehlencord.
*
* Licensed 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 de.muehlencord.shared.jeeutil; package de.muehlencord.shared.jeeutil;
import java.io.Serializable; import java.io.Serializable;

View File

@ -1,84 +1,94 @@
/* /*
* To change this license header, choose License Headers in Project Properties. * Copyright 2019 joern.muehlencord.
* To change this template file, choose Tools | Templates *
* and open the template in the editor. * Licensed under the Apache License, Version 2.0 (the "License");
*/ * you may not use this file except in compliance with the License.
package de.muehlencord.shared.jeeutil.jwt; * You may obtain a copy of the License at
*
import java.time.ZonedDateTime; * http://www.apache.org/licenses/LICENSE-2.0
import org.primeframework.jwt.Verifier; *
import org.primeframework.jwt.domain.JWT; * Unless required by applicable law or agreed to in writing, software
import org.primeframework.jwt.domain.JWTException; * distributed under the License is distributed on an "AS IS" BASIS,
import org.primeframework.jwt.hmac.HMACVerifier; * 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.
* */
* @author Joern Muehlencord <joern at muehlencord.de> package de.muehlencord.shared.jeeutil.jwt;
*/
public class JWTDecoder { import java.time.ZonedDateTime;
import org.primeframework.jwt.Verifier;
private boolean parsedSuccessfully = false; import org.primeframework.jwt.domain.JWT;
private JWT jwt = null; import org.primeframework.jwt.domain.JWTException;
import org.primeframework.jwt.hmac.HMACVerifier;
public JWTDecoder(String password, String issuer, String jwtString) throws JWTException {
if ((password == null) || (issuer == null) || (jwtString == null)) { /**
throw new JWTException("password, issuer and jwt must not be null"); *
} * @author Joern Muehlencord <joern at muehlencord.de>
Verifier verifier = HMACVerifier.newVerifier(password); */
// Verifier verifier = RSAVerifier.newVerifier(new String(Files.readAllBytes(Paths.get("public_key.pem")))); public class JWTDecoder {
try {
jwt = JWT.getDecoder().decode(jwtString, verifier); private boolean parsedSuccessfully = false;
parsedSuccessfully = jwt.issuer.equals(issuer); private JWT jwt = null;
} catch (JWTException ex) {
jwt = null; public JWTDecoder(String password, String issuer, String jwtString) throws JWTException {
} if ((password == null) || (issuer == null) || (jwtString == null)) {
} throw new JWTException("password, issuer and jwt must not be null");
}
public String getIssuer() { Verifier verifier = HMACVerifier.newVerifier(password);
if (jwt == null) { // Verifier verifier = RSAVerifier.newVerifier(new String(Files.readAllBytes(Paths.get("public_key.pem"))));
return null; try {
} else { jwt = JWT.getDecoder().decode(jwtString, verifier);
return jwt.issuer; parsedSuccessfully = jwt.issuer.equals(issuer);
} } catch (JWTException ex) {
} jwt = null;
}
public ZonedDateTime getIssuedAt() { }
if (jwt == null) {
return null; public String getIssuer() {
} else { if (jwt == null) {
return jwt.issuedAt; return null;
} } else {
} return jwt.issuer;
}
public String getSubject() { }
if (jwt == null) {
return null; public ZonedDateTime getIssuedAt() {
} else { if (jwt == null) {
return jwt.subject; return null;
} } else {
} return jwt.issuedAt;
}
public String getUniqueId() { }
if (jwt == null) {
return null; public String getSubject() {
} else { if (jwt == null) {
return jwt.uniqueId; return null;
} } else {
} return jwt.subject;
}
public ZonedDateTime getExpiration() { }
if (jwt == null) {
return null; public String getUniqueId() {
} else { if (jwt == null) {
return jwt.expiration; return null;
} } else {
} return jwt.uniqueId;
}
public boolean isValid() { }
if ((jwt == null) || (jwt.isExpired())) {
return false; public ZonedDateTime getExpiration() {
} else { if (jwt == null) {
return this.parsedSuccessfully; return null;
} } else {
} return jwt.expiration;
} }
}
public boolean isValid() {
if ((jwt == null) || (jwt.isExpired())) {
return false;
} else {
return this.parsedSuccessfully;
}
}
}

View File

@ -1,36 +1,46 @@
/* /*
* To change this license header, choose License Headers in Project Properties. * Copyright 2019 joern.muehlencord.
* To change this template file, choose Tools | Templates *
* and open the template in the editor. * Licensed under the Apache License, Version 2.0 (the "License");
*/ * you may not use this file except in compliance with the License.
package de.muehlencord.shared.jeeutil.jwt; * You may obtain a copy of the License at
*
import java.time.ZonedDateTime; * http://www.apache.org/licenses/LICENSE-2.0
import org.primeframework.jwt.Signer; *
import org.primeframework.jwt.domain.JWT; * Unless required by applicable law or agreed to in writing, software
import org.primeframework.jwt.hmac.HMACSigner; * 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.
* @author Joern Muehlencord <joern at muehlencord.de> */
*/ package de.muehlencord.shared.jeeutil.jwt;
public abstract class JWTEncoder {
import java.time.ZonedDateTime;
public static String encode(String password, String issuer, ZonedDateTime issuedAt, String subject, String uniqueId, short expirationInMinutes ) throws JWTException { import org.primeframework.jwt.Signer;
if ((password == null) || (issuer == null)) { import org.primeframework.jwt.domain.JWT;
throw new JWTException("password and issuer must not be null"); import org.primeframework.jwt.hmac.HMACSigner;
}
Signer signer = HMACSigner.newSHA256Signer(password); /**
// Signer signer = RSASigner.newSHA256Signer(new String(Files.readAllBytes(Paths.get("private_key.pem")))); *
* @author Joern Muehlencord <joern at muehlencord.de>
*/
JWT jwt = new JWT().setIssuer(issuer) // FIXME - make configurable public abstract class JWTEncoder {
.setIssuedAt(issuedAt)
.setSubject(subject) public static String encode(String password, String issuer, ZonedDateTime issuedAt, String subject, String uniqueId, short expirationInMinutes ) throws JWTException {
.setUniqueId(uniqueId) if ((password == null) || (issuer == null)) {
.setExpiration(issuedAt.plusMinutes(expirationInMinutes)); throw new JWTException("password and issuer must not be null");
return JWT.getEncoder().encode(jwt, signer); }
Signer signer = HMACSigner.newSHA256Signer(password);
} // Signer signer = RSASigner.newSHA256Signer(new String(Files.readAllBytes(Paths.get("private_key.pem"))));
}
JWT jwt = new JWT().setIssuer(issuer) // FIXME - make configurable
.setIssuedAt(issuedAt)
.setSubject(subject)
.setUniqueId(uniqueId)
.setExpiration(issuedAt.plusMinutes(expirationInMinutes));
return JWT.getEncoder().encode(jwt, signer);
}
}

View File

@ -1,9 +1,18 @@
/* /*
* To change this license header, choose License Headers in Project Properties. * Copyright 2019 joern.muehlencord.
* To change this template file, choose Tools | Templates *
* and open the template in the editor. * Licensed 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 de.muehlencord.shared.jeeutil.jwt; package de.muehlencord.shared.jeeutil.jwt;
/** /**

View File

@ -1,25 +1,35 @@
/* /*
* To change this license header, choose License Headers in Project Properties. * Copyright 2019 joern.muehlencord.
* To change this template file, choose Tools | Templates *
* and open the template in the editor. * Licensed under the Apache License, Version 2.0 (the "License");
*/ * you may not use this file except in compliance with the License.
package de.muehlencord.shared.jeeutil.jwt; * You may obtain a copy of the License at
*
import javax.servlet.ServletRequest; * http://www.apache.org/licenses/LICENSE-2.0
import javax.servlet.ServletResponse; *
import javax.servlet.http.HttpServletResponse; * Unless required by applicable law or agreed to in writing, software
import org.apache.shiro.web.filter.authc.AuthenticationFilter; * 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.
* @author Joern Muehlencord <joern at muehlencord.de> */
*/ package de.muehlencord.shared.jeeutil.jwt;
public class JWTGuard extends AuthenticationFilter {
import javax.servlet.ServletRequest;
@Override import javax.servlet.ServletResponse;
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception { import javax.servlet.http.HttpServletResponse;
HttpServletResponse httpResponse = (HttpServletResponse) response; import org.apache.shiro.web.filter.authc.AuthenticationFilter;
httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return false; /**
} *
} * @author Joern Muehlencord <joern at muehlencord.de>
*/
public class JWTGuard extends AuthenticationFilter {
@Override
protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return false;
}
}

View File

@ -1,32 +1,32 @@
/* /*
* Copyright 2018 jomu. * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.jeeutil.restexfw; package de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
/** /**
* *
* @author jomu * @author jomu
*/ */
public interface APIError { public interface APIError {
Response.Status getStatus(); Response.Status getStatus();
String getErrorCode(); String getErrorCode();
String getMessageKey(); String getMessageKey();
} }

View File

@ -1,109 +1,109 @@
/* /*
* Copyright 2018 jomu. * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.jeeutil.restexfw; package de.muehlencord.shared.jeeutil.restexfw;
import java.util.Locale; import java.util.Locale;
import java.util.ResourceBundle; import java.util.ResourceBundle;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
/** /**
* *
* @author jomu * @author jomu
*/ */
@XmlRootElement @XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
@XmlType(propOrder = {"status", "errorCode", "message"}) @XmlType(propOrder = {"status", "errorCode", "message"})
public class APIErrorResponse { public class APIErrorResponse {
@XmlJavaTypeAdapter(ResponseStatusAdapter.class) @XmlJavaTypeAdapter(ResponseStatusAdapter.class)
private final Response.Status status; private final Response.Status status;
private final String errorCode; private final String errorCode;
private final String message; private final String message;
private final String rootCause; private final String rootCause;
public APIErrorResponse(APIError apiError, Locale locale) { public APIErrorResponse(APIError apiError, Locale locale) {
this.status = apiError.getStatus(); this.status = apiError.getStatus();
this.errorCode = apiError.getErrorCode(); this.errorCode = apiError.getErrorCode();
this.message = getLocalizedMessage(apiError, locale); this.message = getLocalizedMessage(apiError, locale);
this.rootCause = null; this.rootCause = null;
} }
public APIErrorResponse(APIError apiError, Locale locale, Throwable th) { public APIErrorResponse(APIError apiError, Locale locale, Throwable th) {
this.status = apiError.getStatus(); this.status = apiError.getStatus();
this.errorCode = apiError.getErrorCode(); this.errorCode = apiError.getErrorCode();
this.message = getLocalizedMessage(apiError, locale); this.message = getLocalizedMessage(apiError, locale);
this.rootCause = th.getMessage(); this.rootCause = th.getMessage();
} }
public APIErrorResponse(APIError apiError, Locale locale, String rootCauseMessage) { public APIErrorResponse(APIError apiError, Locale locale, String rootCauseMessage) {
this.status = apiError.getStatus(); this.status = apiError.getStatus();
this.errorCode = apiError.getErrorCode(); this.errorCode = apiError.getErrorCode();
this.message = getLocalizedMessage(apiError, locale); this.message = getLocalizedMessage(apiError, locale);
this.rootCause = rootCauseMessage; this.rootCause = rootCauseMessage;
} }
public APIErrorResponse(Exception exception, Locale locale) { public APIErrorResponse(Exception exception, Locale locale) {
this.status = Response.Status.INTERNAL_SERVER_ERROR; this.status = Response.Status.INTERNAL_SERVER_ERROR;
this.errorCode = "0"; this.errorCode = "0";
this.message = exception.getLocalizedMessage(); this.message = exception.getLocalizedMessage();
this.rootCause = null; this.rootCause = null;
} }
public APIErrorResponse(Exception exception, Locale locale, Throwable th) { public APIErrorResponse(Exception exception, Locale locale, Throwable th) {
this.status = Response.Status.INTERNAL_SERVER_ERROR; this.status = Response.Status.INTERNAL_SERVER_ERROR;
this.errorCode = "0"; this.errorCode = "0";
this.message = exception.getLocalizedMessage(); this.message = exception.getLocalizedMessage();
this.rootCause = th.getMessage(); this.rootCause = th.getMessage();
} }
public APIErrorResponse(Response.Status status, String errorCode, String messageKey, Locale locale) { public APIErrorResponse(Response.Status status, String errorCode, String messageKey, Locale locale) {
this.status = status; this.status = status;
this.errorCode = errorCode; this.errorCode = errorCode;
this.message = getLocalizedMessage(messageKey, locale); this.message = getLocalizedMessage(messageKey, locale);
this.rootCause = null; this.rootCause = null;
} }
public String getErrorCode() { public String getErrorCode() {
return this.errorCode; return this.errorCode;
} }
public Response.Status getStatus() { public Response.Status getStatus() {
return status; return status;
} }
public String getMessage() { public String getMessage() {
return this.message; return this.message;
} }
public String getRootCause() { public String getRootCause() {
return rootCause; return rootCause;
} }
private String getLocalizedMessage(APIError apiError, Locale locale) { private String getLocalizedMessage(APIError apiError, Locale locale) {
ResourceBundle resourceBundle = ResourceBundle.getBundle(apiError.getClass().getName(), locale); ResourceBundle resourceBundle = ResourceBundle.getBundle(apiError.getClass().getName(), locale);
return resourceBundle.getString(apiError.getMessageKey()); return resourceBundle.getString(apiError.getMessageKey());
} }
private String getLocalizedMessage(String messageKey, Locale locale) { private String getLocalizedMessage(String messageKey, Locale locale) {
ResourceBundle resourceBundle = ResourceBundle.getBundle(getClass().getName(), locale); ResourceBundle resourceBundle = ResourceBundle.getBundle(getClass().getName(), locale);
return resourceBundle.getString(messageKey); return resourceBundle.getString(messageKey);
} }
} }

View File

@ -1,68 +1,68 @@
/* /*
* Copyright 2018 jomu. * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.jeeutil.restexfw; package de.muehlencord.shared.jeeutil.restexfw;
import java.util.Locale; import java.util.Locale;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder; import javax.ws.rs.core.Response.ResponseBuilder;
/** /**
* *
* @author jomu * @author jomu
*/ */
public class APIException extends RuntimeException { public class APIException extends RuntimeException {
public static final String HTTP_HEADER_X_ERROR = "X-Error"; public static final String HTTP_HEADER_X_ERROR = "X-Error";
public static final String HTTP_HEADER_X_ERROR_CODE = "X-Error-Code"; public static final String HTTP_HEADER_X_ERROR_CODE = "X-Error-Code";
public static final String HTTP_HEADER_X_ROOT_CAUSE = "X-Root-Cause"; public static final String HTTP_HEADER_X_ROOT_CAUSE = "X-Root-Cause";
private static final long serialVersionUID = -4356132354448841938L; private static final long serialVersionUID = -4356132354448841938L;
private final Response httpResponse; private final Response httpResponse;
public APIException(APIError apiError, Locale locale) { public APIException(APIError apiError, Locale locale) {
httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale)); httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale));
} }
public APIException(APIError apiError, Locale locale, Throwable th) { public APIException(APIError apiError, Locale locale, Throwable th) {
httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale, th)); httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale, th));
} }
public APIException(APIError apiError, Locale locale, String rootCause) { public APIException(APIError apiError, Locale locale, String rootCause) {
httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale, rootCause)); httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale, rootCause));
} }
public APIException(Exception exception, Locale locale) { public APIException(Exception exception, Locale locale) {
httpResponse = createHttpResponse(new APIErrorResponse(exception, locale)); httpResponse = createHttpResponse(new APIErrorResponse(exception, locale));
} }
public Response getHttpResponse() { public Response getHttpResponse() {
return httpResponse; return httpResponse;
} }
private static Response createHttpResponse(APIErrorResponse response) { private static Response createHttpResponse(APIErrorResponse response) {
ResponseBuilder builder = Response.status(response.getStatus()).entity(response) ResponseBuilder builder = Response.status(response.getStatus()).entity(response)
.header(HTTP_HEADER_X_ERROR, response.getMessage()) .header(HTTP_HEADER_X_ERROR, response.getMessage())
.header(HTTP_HEADER_X_ERROR_CODE, response.getErrorCode()); .header(HTTP_HEADER_X_ERROR_CODE, response.getErrorCode());
if (response.getRootCause() != null) { if (response.getRootCause() != null) {
builder = builder.header(HTTP_HEADER_X_ROOT_CAUSE, response.getRootCause()); builder = builder.header(HTTP_HEADER_X_ROOT_CAUSE, response.getRootCause());
} }
return builder.build(); return builder.build();
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2018 joern (at) muehlencord.de * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,35 +1,35 @@
/* /*
* Copyright 2018 joern (at) muehlencord.de * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.jeeutil.restexfw; package de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.BadRequestException; import javax.ws.rs.BadRequestException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.Provider;
/** /**
* *
* @author Joern Muehlencord (joern (at) muehlencord.de * @author Joern Muehlencord (joern (at) muehlencord.de
*/ */
@Provider @Provider
public class BadRequestMapper implements ExceptionMapper<BadRequestException> { public class BadRequestMapper implements ExceptionMapper<BadRequestException> {
@Override @Override
public Response toResponse(BadRequestException ex) { public Response toResponse(BadRequestException ex) {
return Response.status(Response.Status.BAD_REQUEST).entity(ex.getMessage()).build(); return Response.status(Response.Status.BAD_REQUEST).entity(ex.getMessage()).build();
} }
} }

View File

@ -1,64 +1,64 @@
/* /*
* Copyright 2018 jomu. * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.jeeutil.restexfw; package de.muehlencord.shared.jeeutil.restexfw;
import java.util.Iterator; import java.util.Iterator;
import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolation;
import javax.validation.Path; import javax.validation.Path;
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
/** /**
* *
* @author jomu * @author jomu
*/ */
@XmlRootElement @XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
public class ConstraintViolationEntry { public class ConstraintViolationEntry {
private String fieldName; private String fieldName;
private String wrongValue; private String wrongValue;
private String errorMessage; private String errorMessage;
public ConstraintViolationEntry() { public ConstraintViolationEntry() {
} }
public ConstraintViolationEntry(ConstraintViolation violation) { public ConstraintViolationEntry(ConstraintViolation violation) {
Iterator<Path.Node> iterator = violation.getPropertyPath().iterator(); Iterator<Path.Node> iterator = violation.getPropertyPath().iterator();
Path.Node currentNode = iterator.next(); Path.Node currentNode = iterator.next();
String invalidValue = ""; String invalidValue = "";
if (violation.getInvalidValue() != null) { if (violation.getInvalidValue() != null) {
invalidValue = violation.getInvalidValue().toString(); invalidValue = violation.getInvalidValue().toString();
} }
this.fieldName = currentNode.getName(); this.fieldName = currentNode.getName();
this.wrongValue = invalidValue; this.wrongValue = invalidValue;
this.errorMessage = violation.getMessage(); this.errorMessage = violation.getMessage();
} }
public String getFieldName() { public String getFieldName() {
return fieldName; return fieldName;
} }
public String getWrongValue() { public String getWrongValue() {
return wrongValue; return wrongValue;
} }
public String getErrorMessage() { public String getErrorMessage() {
return errorMessage; return errorMessage;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2018 jomu. * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.

View File

@ -1,35 +1,35 @@
/* /*
* Copyright 2018 joern (at) muehlencord.de * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.jeeutil.restexfw; package de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.ForbiddenException; import javax.ws.rs.ForbiddenException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.Provider;
/** /**
* *
* @author Joern Muehlencord (joern (at) muehlencord.de * @author Joern Muehlencord (joern (at) muehlencord.de
*/ */
@Provider @Provider
public class ForbiddenMapper implements ExceptionMapper<ForbiddenException> { public class ForbiddenMapper implements ExceptionMapper<ForbiddenException> {
@Override @Override
public Response toResponse(ForbiddenException ex) { public Response toResponse(ForbiddenException ex) {
return Response.status(Response.Status.FORBIDDEN).entity(ex.getMessage()).build(); return Response.status(Response.Status.FORBIDDEN).entity(ex.getMessage()).build();
} }
} }

View File

@ -1,35 +1,35 @@
/* /*
* Copyright 2018 joern (at) muehlencord.de * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.jeeutil.restexfw; package de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.NotAcceptableException; import javax.ws.rs.NotAcceptableException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.Provider;
/** /**
* *
* @author Joern Muehlencord (joern (at) muehlencord.de * @author Joern Muehlencord (joern (at) muehlencord.de
*/ */
@Provider @Provider
public class NotAcceptableMapper implements ExceptionMapper<NotAcceptableException> { public class NotAcceptableMapper implements ExceptionMapper<NotAcceptableException> {
@Override @Override
public Response toResponse(NotAcceptableException ex) { public Response toResponse(NotAcceptableException ex) {
return Response.status(Response.Status.NOT_ACCEPTABLE).entity(ex.getMessage()).build(); return Response.status(Response.Status.NOT_ACCEPTABLE).entity(ex.getMessage()).build();
} }
} }

View File

@ -1,35 +1,35 @@
/* /*
* Copyright 2018 joern (at) muehlencord.de * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.jeeutil.restexfw; package de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.NotAllowedException; import javax.ws.rs.NotAllowedException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.Provider;
/** /**
* *
* @author Joern Muehlencord (joern (at) muehlencord.de * @author Joern Muehlencord (joern (at) muehlencord.de
*/ */
@Provider @Provider
public class NotAllowedMapper implements ExceptionMapper<NotAllowedException> { public class NotAllowedMapper implements ExceptionMapper<NotAllowedException> {
@Override @Override
public Response toResponse(NotAllowedException ex) { public Response toResponse(NotAllowedException ex) {
return Response.status(Response.Status.METHOD_NOT_ALLOWED).entity(ex.getMessage()).build(); return Response.status(Response.Status.METHOD_NOT_ALLOWED).entity(ex.getMessage()).build();
} }
} }

View File

@ -1,35 +1,35 @@
/* /*
* Copyright 2018 joern (at) muehlencord.de * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.jeeutil.restexfw; package de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.NotAuthorizedException; import javax.ws.rs.NotAuthorizedException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.Provider;
/** /**
* *
* @author Joern Muehlencord (joern (at) muehlencord.de * @author Joern Muehlencord (joern (at) muehlencord.de
*/ */
@Provider @Provider
public class NotAuthorizedMapper implements ExceptionMapper<NotAuthorizedException> { public class NotAuthorizedMapper implements ExceptionMapper<NotAuthorizedException> {
@Override @Override
public Response toResponse(NotAuthorizedException ex) { public Response toResponse(NotAuthorizedException ex) {
return Response.status(Response.Status.UNAUTHORIZED).entity(ex.getMessage()).build(); return Response.status(Response.Status.UNAUTHORIZED).entity(ex.getMessage()).build();
} }
} }

View File

@ -1,35 +1,35 @@
/* /*
* Copyright 2018 joern (at) muehlencord.de * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.jeeutil.restexfw; package de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.NotFoundException; import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.Provider;
/** /**
* *
* @author Joern Muehlencord (joern (at) muehlencord.de * @author Joern Muehlencord (joern (at) muehlencord.de
*/ */
@Provider @Provider
public class NotFoundMapper implements ExceptionMapper<NotFoundException> { public class NotFoundMapper implements ExceptionMapper<NotFoundException> {
@Override @Override
public Response toResponse(NotFoundException ex) { public Response toResponse(NotFoundException ex) {
return Response.status(Response.Status.NOT_FOUND).entity(ex.getMessage()).build(); return Response.status(Response.Status.NOT_FOUND).entity(ex.getMessage()).build();
} }
} }

View File

@ -1,35 +1,35 @@
/* /*
* Copyright 2018 joern (at) muehlencord.de * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.jeeutil.restexfw; package de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.NotSupportedException; import javax.ws.rs.NotSupportedException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider; import javax.ws.rs.ext.Provider;
/** /**
* *
* @author Joern Muehlencord (joern (at) muehlencord.de * @author Joern Muehlencord (joern (at) muehlencord.de
*/ */
@Provider @Provider
public class NotSupportMapper implements ExceptionMapper<NotSupportedException> { public class NotSupportMapper implements ExceptionMapper<NotSupportedException> {
@Override @Override
public Response toResponse(NotSupportedException ex) { public Response toResponse(NotSupportedException ex) {
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity(ex.getMessage()).build(); return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity(ex.getMessage()).build();
} }
} }

View File

@ -1,37 +1,37 @@
/* /*
* Copyright 2018 jomu. * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.jeeutil.restexfw; package de.muehlencord.shared.jeeutil.restexfw;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.xml.bind.annotation.adapters.XmlAdapter; import javax.xml.bind.annotation.adapters.XmlAdapter;
/** /**
* *
* @author jomu * @author jomu
*/ */
public class ResponseStatusAdapter extends XmlAdapter<String, Response.Status> { public class ResponseStatusAdapter extends XmlAdapter<String, Response.Status> {
@Override @Override
public String marshal(Response.Status status) throws Exception { public String marshal(Response.Status status) throws Exception {
return status.name(); return status.name();
} }
@Override @Override
public Response.Status unmarshal(String statusAsString) throws Exception { public Response.Status unmarshal(String statusAsString) throws Exception {
return Response.Status.valueOf(statusAsString); return Response.Status.valueOf(statusAsString);
} }
} }

View File

@ -1,40 +1,40 @@
/* /*
* Copyright 2018 jomu. * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.jeeutil.restexfw; package de.muehlencord.shared.jeeutil.restexfw;
import java.util.Set; import java.util.Set;
import javax.validation.ConstraintViolation; import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException; import javax.validation.ConstraintViolationException;
import javax.validation.Validation; import javax.validation.Validation;
import javax.validation.Validator; import javax.validation.Validator;
import javax.validation.ValidatorFactory; import javax.validation.ValidatorFactory;
/** /**
* *
* @author jomu * @author jomu
*/ */
public class ValidationController { public class ValidationController {
public static <T> void processBeanValidation(T entity) { public static <T> void processBeanValidation(T entity) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator(); Validator validator = factory.getValidator();
Set<ConstraintViolation<T>> errors = validator.validate(entity); Set<ConstraintViolation<T>> errors = validator.validate(entity);
if (!errors.isEmpty()) { if (!errors.isEmpty()) {
throw new ConstraintViolationException(errors); throw new ConstraintViolationException(errors);
} }
} }
} }

View File

@ -1,3 +1,18 @@
/*
* Copyright 2019 joern.muehlencord.
*
* Licensed 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 de.muehlencord.shared.jeeutil.validator; package de.muehlencord.shared.jeeutil.validator;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;

View File

@ -1,3 +1,18 @@
/*
* Copyright 2019 joern.muehlencord.
*
* Licensed 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 de.muehlencord.shared.jeeutil.validator; package de.muehlencord.shared.jeeutil.validator;
import java.util.HashMap; import java.util.HashMap;

View File

@ -1,3 +1,18 @@
/*
* Copyright 2019 joern.muehlencord.
*
* Licensed 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 de.muehlencord.shared.jeeutil.validator; package de.muehlencord.shared.jeeutil.validator;
import java.util.regex.Pattern; import java.util.regex.Pattern;

View File

@ -1,64 +1,64 @@
/* /*
* Copyright 2017 Joern Muehlencord <joern at muehlencord.de>. * Copyright 2019 joern.muehlencord.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package de.muehlencord.shared.jeeutil.validator; package de.muehlencord.shared.jeeutil.validator;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.faces.application.FacesMessage; import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent; import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext; import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator; import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator; import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException; import javax.faces.validator.ValidatorException;
/** /**
* *
* @author Joern Muehlencord <joern at muehlencord.de> * @author Joern Muehlencord <joern at muehlencord.de>
*/ */
@FacesValidator("de.muehlencord.shared.jeeutil.validator.EmailValidator") @FacesValidator("de.muehlencord.shared.jeeutil.validator.EmailValidator")
public class EmailValidator implements Validator { public class EmailValidator implements Validator {
private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\." private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\."
+ "[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*" + "[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*"
+ "(\\.[A-Za-z]{2,})$"; + "(\\.[A-Za-z]{2,})$";
private final Pattern pattern; private final Pattern pattern;
public EmailValidator() { public EmailValidator() {
pattern = Pattern.compile(EMAIL_PATTERN); pattern = Pattern.compile(EMAIL_PATTERN);
} }
@Override @Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
Matcher matcher = pattern.matcher(value.toString()); Matcher matcher = pattern.matcher(value.toString());
if (!matcher.matches()) { if (!matcher.matches()) {
// String summary = context.getApplication().evaluateExpressionGet(context, "#{msgs.email_validation_failed}", String.class); // String summary = context.getApplication().evaluateExpressionGet(context, "#{msgs.email_validation_failed}", String.class);
// String detail = context.getApplication().evaluateExpressionGet(context, "#{msgs.email_validation_failed_detail}", String.class); // String detail = context.getApplication().evaluateExpressionGet(context, "#{msgs.email_validation_failed_detail}", String.class);
// FacesMessage msg = new FacesMessage(summary, detail); // FacesMessage msg = new FacesMessage(summary, detail);
FacesMessage msg = new FacesMessage("E-mail validation failed.","Invalid E-mail format."); FacesMessage msg = new FacesMessage("E-mail validation failed.","Invalid E-mail format.");
msg.setSeverity(FacesMessage.SEVERITY_ERROR); msg.setSeverity(FacesMessage.SEVERITY_ERROR);
throw new ValidatorException(msg); throw new ValidatorException(msg);
} }
} }
public boolean isValid(String emailAddress) { public boolean isValid(String emailAddress) {
Matcher matcher = pattern.matcher(emailAddress); Matcher matcher = pattern.matcher(emailAddress);
return matcher.matches(); return matcher.matches();
} }
} }