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"?>
<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>
<parent>
<artifactId>shared</artifactId>
<groupId>de.muehlencord</groupId>
<version>1.1-SNAPSHOT</version>
</parent>
<groupId>de.muehlencord.shared</groupId>
<artifactId>shared-jeeutil</artifactId>
<version>1.1-SNAPSHOT</version>
<packaging>ejb</packaging>
<name>shared-jeeutil</name>
<dependencies>
<dependency>
<groupId>com.inversoft</groupId>
<artifactId>prime-jwt</artifactId>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<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>
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<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>
<parent>
<artifactId>shared</artifactId>
<groupId>de.muehlencord</groupId>
<version>1.1-SNAPSHOT</version>
</parent>
<groupId>de.muehlencord.shared</groupId>
<artifactId>shared-jeeutil</artifactId>
<version>1.1-SNAPSHOT</version>
<packaging>ejb</packaging>
<name>shared-jeeutil</name>
<dependencies>
<dependency>
<groupId>com.inversoft</groupId>
<artifactId>prime-jwt</artifactId>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<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;
import java.io.Serializable;

View File

@ -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 <joern at muehlencord.de>
*/
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 <joern at muehlencord.de>
*/
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;
}
}
}

View File

@ -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 <joern at muehlencord.de>
*/
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 <joern at muehlencord.de>
*/
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);
}
}

View File

@ -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;
/**

View File

@ -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 <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;
}
}
/*
* 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 <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.
*
* 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();
}

View File

@ -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);
}
}

View File

@ -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();
}
}

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");
* you may not use this file except in compliance with the License.

View File

@ -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<BadRequestException> {
@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<BadRequestException> {
@Override
public Response toResponse(BadRequestException ex) {
return Response.status(Response.Status.BAD_REQUEST).entity(ex.getMessage()).build();
}
}

View File

@ -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<Path.Node> 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<Path.Node> 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;
}
}

View File

@ -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.

View File

@ -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<ForbiddenException> {
@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<ForbiddenException> {
@Override
public Response toResponse(ForbiddenException ex) {
return Response.status(Response.Status.FORBIDDEN).entity(ex.getMessage()).build();
}
}

View File

@ -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<NotAcceptableException> {
@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<NotAcceptableException> {
@Override
public Response toResponse(NotAcceptableException ex) {
return Response.status(Response.Status.NOT_ACCEPTABLE).entity(ex.getMessage()).build();
}
}

View File

@ -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<NotAllowedException> {
@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<NotAllowedException> {
@Override
public Response toResponse(NotAllowedException ex) {
return Response.status(Response.Status.METHOD_NOT_ALLOWED).entity(ex.getMessage()).build();
}
}

View File

@ -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<NotAuthorizedException> {
@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<NotAuthorizedException> {
@Override
public Response toResponse(NotAuthorizedException ex) {
return Response.status(Response.Status.UNAUTHORIZED).entity(ex.getMessage()).build();
}
}

View File

@ -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<NotFoundException> {
@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<NotFoundException> {
@Override
public Response toResponse(NotFoundException ex) {
return Response.status(Response.Status.NOT_FOUND).entity(ex.getMessage()).build();
}
}

View File

@ -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<NotSupportedException> {
@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<NotSupportedException> {
@Override
public Response toResponse(NotSupportedException ex) {
return Response.status(Response.Status.UNSUPPORTED_MEDIA_TYPE).entity(ex.getMessage()).build();
}
}

View File

@ -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<String, Response.Status> {
@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<String, Response.Status> {
@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);
}
}

View File

@ -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 <T> void processBeanValidation(T entity) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<T>> 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 <T> void processBeanValidation(T entity) {
ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();
Set<ConstraintViolation<T>> errors = validator.validate(entity);
if (!errors.isEmpty()) {
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;
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;
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;
import java.util.regex.Pattern;

View File

@ -1,64 +1,64 @@
/*
* Copyright 2017 Joern Muehlencord <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.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 <joern at muehlencord.de>
*/
@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 <joern at muehlencord.de>
*/
@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();
}
}