diff --git a/jeeutil/pom.xml b/jeeutil/pom.xml index 34d1aae..2fa0e98 100644 --- a/jeeutil/pom.xml +++ b/jeeutil/pom.xml @@ -1,62 +1,78 @@ - - - 4.0.0 - - shared - de.muehlencord - 1.1-SNAPSHOT - - - de.muehlencord.shared - shared-jeeutil - 1.1-SNAPSHOT - ejb - - shared-jeeutil - - - - com.inversoft - prime-jwt - - - javax - javaee-api - provided - - - org.slf4j - slf4j-api - provided - - - org.hibernate - hibernate-core - provided - - - org.primefaces - primefaces - jar - provided - - - org.apache.shiro - shiro-web - provided - - - - - - - org.apache.maven.plugins - maven-ejb-plugin - - 3.1 - - - - - - + + + + + 4.0.0 + + shared + de.muehlencord + 1.1-SNAPSHOT + + + de.muehlencord.shared + shared-jeeutil + 1.1-SNAPSHOT + ejb + + shared-jeeutil + + + + com.inversoft + prime-jwt + + + javax + javaee-api + provided + + + org.slf4j + slf4j-api + provided + + + org.hibernate + hibernate-core + provided + + + org.primefaces + primefaces + jar + provided + + + org.apache.shiro + shiro-web + provided + + + + + + + org.apache.maven.plugins + maven-ejb-plugin + + 3.1 + + + + + + diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/GenericEnumType.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/GenericEnumType.java index 374925a..32b556f 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/GenericEnumType.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/GenericEnumType.java @@ -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; import java.io.Serializable; diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTDecoder.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTDecoder.java index f5dc43a..da3093c 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTDecoder.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTDecoder.java @@ -1,84 +1,94 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package de.muehlencord.shared.jeeutil.jwt; - -import java.time.ZonedDateTime; -import org.primeframework.jwt.Verifier; -import org.primeframework.jwt.domain.JWT; -import org.primeframework.jwt.domain.JWTException; -import org.primeframework.jwt.hmac.HMACVerifier; - -/** - * - * @author Joern Muehlencord - */ -public class JWTDecoder { - - private boolean parsedSuccessfully = false; - private JWT 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"); - } - Verifier verifier = HMACVerifier.newVerifier(password); -// Verifier verifier = RSAVerifier.newVerifier(new String(Files.readAllBytes(Paths.get("public_key.pem")))); - try { - jwt = JWT.getDecoder().decode(jwtString, verifier); - parsedSuccessfully = jwt.issuer.equals(issuer); - } catch (JWTException ex) { - jwt = null; - } - } - - public String getIssuer() { - if (jwt == null) { - return null; - } else { - return jwt.issuer; - } - } - - public ZonedDateTime getIssuedAt() { - if (jwt == null) { - return null; - } else { - return jwt.issuedAt; - } - } - - public String getSubject() { - if (jwt == null) { - return null; - } else { - return jwt.subject; - } - } - - public String getUniqueId() { - if (jwt == null) { - return null; - } else { - return jwt.uniqueId; - } - } - - public ZonedDateTime getExpiration() { - if (jwt == null) { - return null; - } else { - return jwt.expiration; - } - } - - public boolean isValid() { - if ((jwt == null) || (jwt.isExpired())) { - return false; - } else { - return this.parsedSuccessfully; - } - } -} +/* + * 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.jwt; + +import java.time.ZonedDateTime; +import org.primeframework.jwt.Verifier; +import org.primeframework.jwt.domain.JWT; +import org.primeframework.jwt.domain.JWTException; +import org.primeframework.jwt.hmac.HMACVerifier; + +/** + * + * @author Joern Muehlencord + */ +public class JWTDecoder { + + private boolean parsedSuccessfully = false; + private JWT 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"); + } + Verifier verifier = HMACVerifier.newVerifier(password); +// Verifier verifier = RSAVerifier.newVerifier(new String(Files.readAllBytes(Paths.get("public_key.pem")))); + try { + jwt = JWT.getDecoder().decode(jwtString, verifier); + parsedSuccessfully = jwt.issuer.equals(issuer); + } catch (JWTException ex) { + jwt = null; + } + } + + public String getIssuer() { + if (jwt == null) { + return null; + } else { + return jwt.issuer; + } + } + + public ZonedDateTime getIssuedAt() { + if (jwt == null) { + return null; + } else { + return jwt.issuedAt; + } + } + + public String getSubject() { + if (jwt == null) { + return null; + } else { + return jwt.subject; + } + } + + public String getUniqueId() { + if (jwt == null) { + return null; + } else { + return jwt.uniqueId; + } + } + + public ZonedDateTime getExpiration() { + if (jwt == null) { + return null; + } else { + return jwt.expiration; + } + } + + public boolean isValid() { + if ((jwt == null) || (jwt.isExpired())) { + return false; + } else { + return this.parsedSuccessfully; + } + } +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTEncoder.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTEncoder.java index fb938da..64ab165 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTEncoder.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTEncoder.java @@ -1,36 +1,46 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package de.muehlencord.shared.jeeutil.jwt; - -import java.time.ZonedDateTime; -import org.primeframework.jwt.Signer; -import org.primeframework.jwt.domain.JWT; -import org.primeframework.jwt.hmac.HMACSigner; - -/** - * - * @author Joern Muehlencord - */ -public abstract class JWTEncoder { - - public static String encode(String password, String issuer, ZonedDateTime issuedAt, String subject, String uniqueId, short expirationInMinutes ) throws JWTException { - if ((password == null) || (issuer == null)) { - throw new JWTException("password and issuer must not be null"); - } - 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); - - } - -} +/* + * 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.jwt; + +import java.time.ZonedDateTime; +import org.primeframework.jwt.Signer; +import org.primeframework.jwt.domain.JWT; +import org.primeframework.jwt.hmac.HMACSigner; + +/** + * + * @author Joern Muehlencord + */ +public abstract class JWTEncoder { + + public static String encode(String password, String issuer, ZonedDateTime issuedAt, String subject, String uniqueId, short expirationInMinutes ) throws JWTException { + if ((password == null) || (issuer == null)) { + throw new JWTException("password and issuer must not be null"); + } + 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); + + } + +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTException.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTException.java index 594dbd7..dfb0f48 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTException.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTException.java @@ -1,9 +1,18 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. +/* + * 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.jwt; /** diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTGuard.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTGuard.java index 25ee84e..2b82b60 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTGuard.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/jwt/JWTGuard.java @@ -1,25 +1,35 @@ -/* - * To change this license header, choose License Headers in Project Properties. - * To change this template file, choose Tools | Templates - * and open the template in the editor. - */ -package de.muehlencord.shared.jeeutil.jwt; - -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.HttpServletResponse; -import org.apache.shiro.web.filter.authc.AuthenticationFilter; - -/** - * - * @author Joern Muehlencord - */ -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; - } -} +/* + * 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.jwt; + +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServletResponse; +import org.apache.shiro.web.filter.authc.AuthenticationFilter; + +/** + * + * @author Joern Muehlencord + */ +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; + } +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIError.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIError.java index dad0188..b8efd88 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIError.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIError.java @@ -1,32 +1,32 @@ -/* - * Copyright 2018 jomu. - * - * 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.restexfw; - -import javax.ws.rs.core.Response; - -/** - * - * @author jomu - */ -public interface APIError { - - Response.Status getStatus(); - - String getErrorCode(); - - String getMessageKey(); - -} +/* + * 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.restexfw; + +import javax.ws.rs.core.Response; + +/** + * + * @author jomu + */ +public interface APIError { + + Response.Status getStatus(); + + String getErrorCode(); + + String getMessageKey(); + +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIErrorResponse.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIErrorResponse.java index 935c27a..79ea5e1 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIErrorResponse.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIErrorResponse.java @@ -1,109 +1,109 @@ -/* - * Copyright 2018 jomu. - * - * 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.restexfw; - -import java.util.Locale; -import java.util.ResourceBundle; -import javax.ws.rs.core.Response; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlType; -import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; - -/** - * - * @author jomu - */ -@XmlRootElement -@XmlAccessorType(XmlAccessType.FIELD) -@XmlType(propOrder = {"status", "errorCode", "message"}) -public class APIErrorResponse { - - @XmlJavaTypeAdapter(ResponseStatusAdapter.class) - private final Response.Status status; - private final String errorCode; - private final String message; - private final String rootCause; - - public APIErrorResponse(APIError apiError, Locale locale) { - this.status = apiError.getStatus(); - this.errorCode = apiError.getErrorCode(); - this.message = getLocalizedMessage(apiError, locale); - this.rootCause = null; - } - - public APIErrorResponse(APIError apiError, Locale locale, Throwable th) { - this.status = apiError.getStatus(); - this.errorCode = apiError.getErrorCode(); - this.message = getLocalizedMessage(apiError, locale); - this.rootCause = th.getMessage(); - } - - public APIErrorResponse(APIError apiError, Locale locale, String rootCauseMessage) { - this.status = apiError.getStatus(); - this.errorCode = apiError.getErrorCode(); - this.message = getLocalizedMessage(apiError, locale); - this.rootCause = rootCauseMessage; - } - - public APIErrorResponse(Exception exception, Locale locale) { - this.status = Response.Status.INTERNAL_SERVER_ERROR; - this.errorCode = "0"; - this.message = exception.getLocalizedMessage(); - this.rootCause = null; - } - - public APIErrorResponse(Exception exception, Locale locale, Throwable th) { - this.status = Response.Status.INTERNAL_SERVER_ERROR; - this.errorCode = "0"; - this.message = exception.getLocalizedMessage(); - this.rootCause = th.getMessage(); - } - - public APIErrorResponse(Response.Status status, String errorCode, String messageKey, Locale locale) { - this.status = status; - this.errorCode = errorCode; - this.message = getLocalizedMessage(messageKey, locale); - this.rootCause = null; - } - - public String getErrorCode() { - return this.errorCode; - } - - public Response.Status getStatus() { - return status; - } - - public String getMessage() { - return this.message; - } - - public String getRootCause() { - return rootCause; - } - - private String getLocalizedMessage(APIError apiError, Locale locale) { - ResourceBundle resourceBundle = ResourceBundle.getBundle(apiError.getClass().getName(), locale); - return resourceBundle.getString(apiError.getMessageKey()); - } - - private String getLocalizedMessage(String messageKey, Locale locale) { - ResourceBundle resourceBundle = ResourceBundle.getBundle(getClass().getName(), locale); - return resourceBundle.getString(messageKey); - } -} +/* + * 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.restexfw; + +import java.util.Locale; +import java.util.ResourceBundle; +import javax.ws.rs.core.Response; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + +/** + * + * @author jomu + */ +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(propOrder = {"status", "errorCode", "message"}) +public class APIErrorResponse { + + @XmlJavaTypeAdapter(ResponseStatusAdapter.class) + private final Response.Status status; + private final String errorCode; + private final String message; + private final String rootCause; + + public APIErrorResponse(APIError apiError, Locale locale) { + this.status = apiError.getStatus(); + this.errorCode = apiError.getErrorCode(); + this.message = getLocalizedMessage(apiError, locale); + this.rootCause = null; + } + + public APIErrorResponse(APIError apiError, Locale locale, Throwable th) { + this.status = apiError.getStatus(); + this.errorCode = apiError.getErrorCode(); + this.message = getLocalizedMessage(apiError, locale); + this.rootCause = th.getMessage(); + } + + public APIErrorResponse(APIError apiError, Locale locale, String rootCauseMessage) { + this.status = apiError.getStatus(); + this.errorCode = apiError.getErrorCode(); + this.message = getLocalizedMessage(apiError, locale); + this.rootCause = rootCauseMessage; + } + + public APIErrorResponse(Exception exception, Locale locale) { + this.status = Response.Status.INTERNAL_SERVER_ERROR; + this.errorCode = "0"; + this.message = exception.getLocalizedMessage(); + this.rootCause = null; + } + + public APIErrorResponse(Exception exception, Locale locale, Throwable th) { + this.status = Response.Status.INTERNAL_SERVER_ERROR; + this.errorCode = "0"; + this.message = exception.getLocalizedMessage(); + this.rootCause = th.getMessage(); + } + + public APIErrorResponse(Response.Status status, String errorCode, String messageKey, Locale locale) { + this.status = status; + this.errorCode = errorCode; + this.message = getLocalizedMessage(messageKey, locale); + this.rootCause = null; + } + + public String getErrorCode() { + return this.errorCode; + } + + public Response.Status getStatus() { + return status; + } + + public String getMessage() { + return this.message; + } + + public String getRootCause() { + return rootCause; + } + + private String getLocalizedMessage(APIError apiError, Locale locale) { + ResourceBundle resourceBundle = ResourceBundle.getBundle(apiError.getClass().getName(), locale); + return resourceBundle.getString(apiError.getMessageKey()); + } + + private String getLocalizedMessage(String messageKey, Locale locale) { + ResourceBundle resourceBundle = ResourceBundle.getBundle(getClass().getName(), locale); + return resourceBundle.getString(messageKey); + } +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIException.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIException.java index 10e5cfc..f38f544 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIException.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIException.java @@ -1,68 +1,68 @@ -/* - * Copyright 2018 jomu. - * - * 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.restexfw; - - -import java.util.Locale; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.Response.ResponseBuilder; - -/** - * - * @author jomu - */ -public class APIException extends RuntimeException { - - 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_ROOT_CAUSE = "X-Root-Cause"; - private static final long serialVersionUID = -4356132354448841938L; - - private final Response httpResponse; - - public APIException(APIError apiError, Locale locale) { - httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale)); - } - - public APIException(APIError apiError, Locale locale, Throwable th) { - httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale, th)); - } - - public APIException(APIError apiError, Locale locale, String rootCause) { - httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale, rootCause)); - } - - public APIException(Exception exception, Locale locale) { - httpResponse = createHttpResponse(new APIErrorResponse(exception, locale)); - } - - public Response getHttpResponse() { - return httpResponse; - } - - private static Response createHttpResponse(APIErrorResponse response) { - ResponseBuilder builder = Response.status(response.getStatus()).entity(response) - .header(HTTP_HEADER_X_ERROR, response.getMessage()) - .header(HTTP_HEADER_X_ERROR_CODE, response.getErrorCode()); - - if (response.getRootCause() != null) { - builder = builder.header(HTTP_HEADER_X_ROOT_CAUSE, response.getRootCause()); - } - - return builder.build(); - } - -} +/* + * 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.restexfw; + + +import java.util.Locale; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.Response.ResponseBuilder; + +/** + * + * @author jomu + */ +public class APIException extends RuntimeException { + + 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_ROOT_CAUSE = "X-Root-Cause"; + private static final long serialVersionUID = -4356132354448841938L; + + private final Response httpResponse; + + public APIException(APIError apiError, Locale locale) { + httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale)); + } + + public APIException(APIError apiError, Locale locale, Throwable th) { + httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale, th)); + } + + public APIException(APIError apiError, Locale locale, String rootCause) { + httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale, rootCause)); + } + + public APIException(Exception exception, Locale locale) { + httpResponse = createHttpResponse(new APIErrorResponse(exception, locale)); + } + + public Response getHttpResponse() { + return httpResponse; + } + + private static Response createHttpResponse(APIErrorResponse response) { + ResponseBuilder builder = Response.status(response.getStatus()).entity(response) + .header(HTTP_HEADER_X_ERROR, response.getMessage()) + .header(HTTP_HEADER_X_ERROR_CODE, response.getErrorCode()); + + if (response.getRootCause() != null) { + builder = builder.header(HTTP_HEADER_X_ROOT_CAUSE, response.getRootCause()); + } + + return builder.build(); + } + +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIExceptionInterceptor.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIExceptionInterceptor.java index 409f875..1cd0172 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIExceptionInterceptor.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/APIExceptionInterceptor.java @@ -1,5 +1,5 @@ -/* - * Copyright 2018 joern (at) muehlencord.de +/* + * 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. diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/BadRequestMapper.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/BadRequestMapper.java index dc60ac6..f2b5514 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/BadRequestMapper.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/BadRequestMapper.java @@ -1,35 +1,35 @@ -/* - * Copyright 2018 joern (at) muehlencord.de - * - * 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.restexfw; - -import javax.ws.rs.BadRequestException; -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; - -/** - * - * @author Joern Muehlencord (joern (at) muehlencord.de - */ -@Provider -public class BadRequestMapper implements ExceptionMapper { - - @Override - public Response toResponse(BadRequestException ex) { - return Response.status(Response.Status.BAD_REQUEST).entity(ex.getMessage()).build(); - } - -} +/* + * 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.restexfw; + +import javax.ws.rs.BadRequestException; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +/** + * + * @author Joern Muehlencord (joern (at) muehlencord.de + */ +@Provider +public class BadRequestMapper implements ExceptionMapper { + + @Override + public Response toResponse(BadRequestException ex) { + return Response.status(Response.Status.BAD_REQUEST).entity(ex.getMessage()).build(); + } + +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ConstraintViolationEntry.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ConstraintViolationEntry.java index 95a79d0..aca35d8 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ConstraintViolationEntry.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ConstraintViolationEntry.java @@ -1,64 +1,64 @@ -/* - * Copyright 2018 jomu. - * - * 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.restexfw; - -import java.util.Iterator; -import javax.validation.ConstraintViolation; -import javax.validation.Path; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlRootElement; - -/** - * - * @author jomu - */ -@XmlRootElement -@XmlAccessorType(XmlAccessType.FIELD) -public class ConstraintViolationEntry { - - private String fieldName; - private String wrongValue; - private String errorMessage; - - public ConstraintViolationEntry() { - } - - public ConstraintViolationEntry(ConstraintViolation violation) { - Iterator iterator = violation.getPropertyPath().iterator(); - Path.Node currentNode = iterator.next(); - String invalidValue = ""; - if (violation.getInvalidValue() != null) { - invalidValue = violation.getInvalidValue().toString(); - } - this.fieldName = currentNode.getName(); - this.wrongValue = invalidValue; - this.errorMessage = violation.getMessage(); - } - - public String getFieldName() { - return fieldName; - } - - public String getWrongValue() { - return wrongValue; - } - - public String getErrorMessage() { - return errorMessage; - } - -} +/* + * 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.restexfw; + +import java.util.Iterator; +import javax.validation.ConstraintViolation; +import javax.validation.Path; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * + * @author jomu + */ +@XmlRootElement +@XmlAccessorType(XmlAccessType.FIELD) +public class ConstraintViolationEntry { + + private String fieldName; + private String wrongValue; + private String errorMessage; + + public ConstraintViolationEntry() { + } + + public ConstraintViolationEntry(ConstraintViolation violation) { + Iterator iterator = violation.getPropertyPath().iterator(); + Path.Node currentNode = iterator.next(); + String invalidValue = ""; + if (violation.getInvalidValue() != null) { + invalidValue = violation.getInvalidValue().toString(); + } + this.fieldName = currentNode.getName(); + this.wrongValue = invalidValue; + this.errorMessage = violation.getMessage(); + } + + public String getFieldName() { + return fieldName; + } + + public String getWrongValue() { + return wrongValue; + } + + public String getErrorMessage() { + return errorMessage; + } + +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ConstraintViolationMapper.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ConstraintViolationMapper.java index 6d647af..279c69c 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ConstraintViolationMapper.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ConstraintViolationMapper.java @@ -1,5 +1,5 @@ -/* - * Copyright 2018 jomu. +/* + * 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. diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ForbiddenMapper.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ForbiddenMapper.java index bcfee4b..6d670c3 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ForbiddenMapper.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ForbiddenMapper.java @@ -1,35 +1,35 @@ -/* - * Copyright 2018 joern (at) muehlencord.de - * - * 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.restexfw; - -import javax.ws.rs.ForbiddenException; -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; - -/** - * - * @author Joern Muehlencord (joern (at) muehlencord.de - */ -@Provider -public class ForbiddenMapper implements ExceptionMapper { - - @Override - public Response toResponse(ForbiddenException ex) { - return Response.status(Response.Status.FORBIDDEN).entity(ex.getMessage()).build(); - } - -} +/* + * 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.restexfw; + +import javax.ws.rs.ForbiddenException; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +/** + * + * @author Joern Muehlencord (joern (at) muehlencord.de + */ +@Provider +public class ForbiddenMapper implements ExceptionMapper { + + @Override + public Response toResponse(ForbiddenException ex) { + return Response.status(Response.Status.FORBIDDEN).entity(ex.getMessage()).build(); + } + +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotAcceptableMapper.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotAcceptableMapper.java index 453a499..60f2dd4 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotAcceptableMapper.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotAcceptableMapper.java @@ -1,35 +1,35 @@ -/* - * Copyright 2018 joern (at) muehlencord.de - * - * 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.restexfw; - -import javax.ws.rs.NotAcceptableException; -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; - -/** - * - * @author Joern Muehlencord (joern (at) muehlencord.de - */ -@Provider -public class NotAcceptableMapper implements ExceptionMapper { - - @Override - public Response toResponse(NotAcceptableException ex) { - return Response.status(Response.Status.NOT_ACCEPTABLE).entity(ex.getMessage()).build(); - } - -} +/* + * 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.restexfw; + +import javax.ws.rs.NotAcceptableException; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +/** + * + * @author Joern Muehlencord (joern (at) muehlencord.de + */ +@Provider +public class NotAcceptableMapper implements ExceptionMapper { + + @Override + public Response toResponse(NotAcceptableException ex) { + return Response.status(Response.Status.NOT_ACCEPTABLE).entity(ex.getMessage()).build(); + } + +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotAllowedMapper.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotAllowedMapper.java index 81b5dda..673c208 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotAllowedMapper.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotAllowedMapper.java @@ -1,35 +1,35 @@ -/* - * Copyright 2018 joern (at) muehlencord.de - * - * 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.restexfw; - -import javax.ws.rs.NotAllowedException; -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; - -/** - * - * @author Joern Muehlencord (joern (at) muehlencord.de - */ -@Provider -public class NotAllowedMapper implements ExceptionMapper { - - @Override - public Response toResponse(NotAllowedException ex) { - return Response.status(Response.Status.METHOD_NOT_ALLOWED).entity(ex.getMessage()).build(); - } - -} +/* + * 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.restexfw; + +import javax.ws.rs.NotAllowedException; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +/** + * + * @author Joern Muehlencord (joern (at) muehlencord.de + */ +@Provider +public class NotAllowedMapper implements ExceptionMapper { + + @Override + public Response toResponse(NotAllowedException ex) { + return Response.status(Response.Status.METHOD_NOT_ALLOWED).entity(ex.getMessage()).build(); + } + +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotAuthorizedMapper.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotAuthorizedMapper.java index 8f36d18..dbc91c3 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotAuthorizedMapper.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotAuthorizedMapper.java @@ -1,35 +1,35 @@ -/* - * Copyright 2018 joern (at) muehlencord.de - * - * 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.restexfw; - -import javax.ws.rs.NotAuthorizedException; -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; - -/** - * - * @author Joern Muehlencord (joern (at) muehlencord.de - */ -@Provider -public class NotAuthorizedMapper implements ExceptionMapper { - - @Override - public Response toResponse(NotAuthorizedException ex) { - return Response.status(Response.Status.UNAUTHORIZED).entity(ex.getMessage()).build(); - } - -} +/* + * 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.restexfw; + +import javax.ws.rs.NotAuthorizedException; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +/** + * + * @author Joern Muehlencord (joern (at) muehlencord.de + */ +@Provider +public class NotAuthorizedMapper implements ExceptionMapper { + + @Override + public Response toResponse(NotAuthorizedException ex) { + return Response.status(Response.Status.UNAUTHORIZED).entity(ex.getMessage()).build(); + } + +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotFoundMapper.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotFoundMapper.java index d6d19c1..8a96fad 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotFoundMapper.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotFoundMapper.java @@ -1,35 +1,35 @@ -/* - * Copyright 2018 joern (at) muehlencord.de - * - * 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.restexfw; - -import javax.ws.rs.NotFoundException; -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; - -/** - * - * @author Joern Muehlencord (joern (at) muehlencord.de - */ -@Provider -public class NotFoundMapper implements ExceptionMapper { - - @Override - public Response toResponse(NotFoundException ex) { - return Response.status(Response.Status.NOT_FOUND).entity(ex.getMessage()).build(); - } - -} +/* + * 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.restexfw; + +import javax.ws.rs.NotFoundException; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +/** + * + * @author Joern Muehlencord (joern (at) muehlencord.de + */ +@Provider +public class NotFoundMapper implements ExceptionMapper { + + @Override + public Response toResponse(NotFoundException ex) { + return Response.status(Response.Status.NOT_FOUND).entity(ex.getMessage()).build(); + } + +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotSupportMapper.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotSupportMapper.java index 5141fe0..f9fa129 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotSupportMapper.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/NotSupportMapper.java @@ -1,35 +1,35 @@ -/* - * Copyright 2018 joern (at) muehlencord.de - * - * 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.restexfw; - -import javax.ws.rs.NotSupportedException; -import javax.ws.rs.core.Response; -import javax.ws.rs.ext.ExceptionMapper; -import javax.ws.rs.ext.Provider; - -/** - * - * @author Joern Muehlencord (joern (at) muehlencord.de - */ -@Provider -public class NotSupportMapper implements ExceptionMapper { - - @Override - public Response toResponse(NotSupportedException ex) { - return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity(ex.getMessage()).build(); - } - -} +/* + * 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.restexfw; + +import javax.ws.rs.NotSupportedException; +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; +import javax.ws.rs.ext.Provider; + +/** + * + * @author Joern Muehlencord (joern (at) muehlencord.de + */ +@Provider +public class NotSupportMapper implements ExceptionMapper { + + @Override + public Response toResponse(NotSupportedException ex) { + return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity(ex.getMessage()).build(); + } + +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ResponseStatusAdapter.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ResponseStatusAdapter.java index 7e10281..43065fa 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ResponseStatusAdapter.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ResponseStatusAdapter.java @@ -1,37 +1,37 @@ -/* - * Copyright 2018 jomu. - * - * 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.restexfw; - -import javax.ws.rs.core.Response; -import javax.xml.bind.annotation.adapters.XmlAdapter; - -/** - * - * @author jomu - */ -public class ResponseStatusAdapter extends XmlAdapter { - - @Override - public String marshal(Response.Status status) throws Exception { - return status.name(); - } - - @Override - public Response.Status unmarshal(String statusAsString) throws Exception { - return Response.Status.valueOf(statusAsString); - } - -} +/* + * 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.restexfw; + +import javax.ws.rs.core.Response; +import javax.xml.bind.annotation.adapters.XmlAdapter; + +/** + * + * @author jomu + */ +public class ResponseStatusAdapter extends XmlAdapter { + + @Override + public String marshal(Response.Status status) throws Exception { + return status.name(); + } + + @Override + public Response.Status unmarshal(String statusAsString) throws Exception { + return Response.Status.valueOf(statusAsString); + } + +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ValidationController.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ValidationController.java index 4538ff9..880d87e 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ValidationController.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/restexfw/ValidationController.java @@ -1,40 +1,40 @@ -/* - * Copyright 2018 jomu. - * - * 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.restexfw; - -import java.util.Set; -import javax.validation.ConstraintViolation; -import javax.validation.ConstraintViolationException; -import javax.validation.Validation; -import javax.validation.Validator; -import javax.validation.ValidatorFactory; - -/** - * - * @author jomu - */ -public class ValidationController { - - public static void processBeanValidation(T entity) { - ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); - Validator validator = factory.getValidator(); - Set> errors = validator.validate(entity); - if (!errors.isEmpty()) { - throw new ConstraintViolationException(errors); - } - } - -} +/* + * 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.restexfw; + +import java.util.Set; +import javax.validation.ConstraintViolation; +import javax.validation.ConstraintViolationException; +import javax.validation.Validation; +import javax.validation.Validator; +import javax.validation.ValidatorFactory; + +/** + * + * @author jomu + */ +public class ValidationController { + + public static void processBeanValidation(T entity) { + ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); + Validator validator = factory.getValidator(); + Set> errors = validator.validate(entity); + if (!errors.isEmpty()) { + throw new ConstraintViolationException(errors); + } + } + +} diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/Email.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/Email.java index 6ce3e60..b85bf73 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/Email.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/Email.java @@ -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; import java.lang.annotation.Documented; diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/EmailClientValidationConstraint.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/EmailClientValidationConstraint.java index ecdde5f..64a1127 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/EmailClientValidationConstraint.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/EmailClientValidationConstraint.java @@ -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; import java.util.HashMap; diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/EmailConstraintValidator.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/EmailConstraintValidator.java index a974f5e..be84df5 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/EmailConstraintValidator.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/EmailConstraintValidator.java @@ -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; import java.util.regex.Pattern; diff --git a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/EmailValidator.java b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/EmailValidator.java index 17cacf0..084ebf4 100644 --- a/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/EmailValidator.java +++ b/jeeutil/src/main/java/de/muehlencord/shared/jeeutil/validator/EmailValidator.java @@ -1,64 +1,64 @@ -/* - * Copyright 2017 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; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; -import javax.faces.application.FacesMessage; -import javax.faces.component.UIComponent; -import javax.faces.context.FacesContext; -import javax.faces.validator.FacesValidator; -import javax.faces.validator.Validator; -import javax.faces.validator.ValidatorException; - -/** - * - * @author Joern Muehlencord - */ -@FacesValidator("de.muehlencord.shared.jeeutil.validator.EmailValidator") -public class EmailValidator implements Validator { - - private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\." - + "[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*" - + "(\\.[A-Za-z]{2,})$"; - - private final Pattern pattern; - - public EmailValidator() { - pattern = Pattern.compile(EMAIL_PATTERN); - } - - @Override - public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { - Matcher matcher = pattern.matcher(value.toString()); - if (!matcher.matches()) { - // 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); - // FacesMessage msg = new FacesMessage(summary, detail); - - FacesMessage msg = new FacesMessage("E-mail validation failed.","Invalid E-mail format."); - - msg.setSeverity(FacesMessage.SEVERITY_ERROR); - throw new ValidatorException(msg); - } - } - - public boolean isValid(String emailAddress) { - Matcher matcher = pattern.matcher(emailAddress); - return matcher.matches(); - } - -} +/* + * 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; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import javax.faces.application.FacesMessage; +import javax.faces.component.UIComponent; +import javax.faces.context.FacesContext; +import javax.faces.validator.FacesValidator; +import javax.faces.validator.Validator; +import javax.faces.validator.ValidatorException; + +/** + * + * @author Joern Muehlencord + */ +@FacesValidator("de.muehlencord.shared.jeeutil.validator.EmailValidator") +public class EmailValidator implements Validator { + + private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\." + + "[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*" + + "(\\.[A-Za-z]{2,})$"; + + private final Pattern pattern; + + public EmailValidator() { + pattern = Pattern.compile(EMAIL_PATTERN); + } + + @Override + public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { + Matcher matcher = pattern.matcher(value.toString()); + if (!matcher.matches()) { + // 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); + // FacesMessage msg = new FacesMessage(summary, detail); + + FacesMessage msg = new FacesMessage("E-mail validation failed.","Invalid E-mail format."); + + msg.setSeverity(FacesMessage.SEVERITY_ERROR); + throw new ValidatorException(msg); + } + } + + public boolean isValid(String emailAddress) { + Matcher matcher = pattern.matcher(emailAddress); + return matcher.matches(); + } + +}