Commit 6a5cebf370281ac33e66d7b9034f84abaa0beeb1
Merge branch '3.0.0-SNAPSHOT' of https://github.com/demoiselle/framework into 3.0.0-SNAPSHOT
Showing
29 changed files
with
414 additions
and
589 deletions
Show diff stats
demoiselle-configuration/src/main/java/org/demoiselle/jee/configuration/ConfigType.java
1 | package org.demoiselle.jee.configuration; | 1 | package org.demoiselle.jee.configuration; |
2 | 2 | ||
3 | -/** | ||
4 | - * Defines configuration types to be loaded. | ||
5 | - * | ||
6 | - * @author SERPRO | ||
7 | - */ | ||
8 | public enum ConfigType { | 3 | public enum ConfigType { |
9 | - | ||
10 | - /** | ||
11 | - * Configuration loaded on {@link System#getProperties()} or {@link System#getenv()}. | ||
12 | - */ | ||
13 | SYSTEM, | 4 | SYSTEM, |
14 | - | ||
15 | - /** | ||
16 | - * Configuration loaded on XML resources. | ||
17 | - */ | ||
18 | XML, | 5 | XML, |
19 | - | ||
20 | - /** | ||
21 | - * Configuration loaded on properties resources. | ||
22 | - */ | ||
23 | PROPERTIES | 6 | PROPERTIES |
24 | - | ||
25 | } | 7 | } |
demoiselle-configuration/src/main/java/org/demoiselle/jee/configuration/ConfigurationException.java
@@ -2,33 +2,14 @@ package org.demoiselle.jee.configuration; | @@ -2,33 +2,14 @@ package org.demoiselle.jee.configuration; | ||
2 | 2 | ||
3 | import org.demoiselle.jee.core.exception.DemoiselleException; | 3 | import org.demoiselle.jee.core.exception.DemoiselleException; |
4 | 4 | ||
5 | -/** | ||
6 | - * Exception class intended to be used by configuration components. | ||
7 | - * | ||
8 | - * @author SERPRO | ||
9 | - */ | ||
10 | public class ConfigurationException extends DemoiselleException{ | 5 | public class ConfigurationException extends DemoiselleException{ |
11 | 6 | ||
12 | private static final long serialVersionUID = 1L; | 7 | private static final long serialVersionUID = 1L; |
13 | 8 | ||
14 | - /** | ||
15 | - * Constructor with message. | ||
16 | - * | ||
17 | - * @param message | ||
18 | - * exception message | ||
19 | - */ | ||
20 | public ConfigurationException(String message) { | 9 | public ConfigurationException(String message) { |
21 | super(message); | 10 | super(message); |
22 | } | 11 | } |
23 | - | ||
24 | - /** | ||
25 | - * Constructor with message and cause. | ||
26 | - * | ||
27 | - * @param message | ||
28 | - * exception message | ||
29 | - * @param cause | ||
30 | - * exception cause | ||
31 | - */ | 12 | + |
32 | public ConfigurationException(String message, Throwable cause) { | 13 | public ConfigurationException(String message, Throwable cause) { |
33 | super(message, cause); | 14 | super(message, cause); |
34 | } | 15 | } |
demoiselle-configuration/src/main/java/org/demoiselle/jee/configuration/ConfigurationInterceptor.java
@@ -9,12 +9,6 @@ import javax.interceptor.InvocationContext; | @@ -9,12 +9,6 @@ import javax.interceptor.InvocationContext; | ||
9 | 9 | ||
10 | import org.demoiselle.jee.configuration.annotation.Configuration; | 10 | import org.demoiselle.jee.configuration.annotation.Configuration; |
11 | 11 | ||
12 | -/** | ||
13 | - * <p> | ||
14 | - * Interceptor class that loads the values of configuration files | ||
15 | - * into it's mapped class. | ||
16 | - * </p> | ||
17 | - */ | ||
18 | @Dependent | 12 | @Dependent |
19 | @Configuration | 13 | @Configuration |
20 | @Interceptor | 14 | @Interceptor |
demoiselle-configuration/src/main/java/org/demoiselle/jee/configuration/ConfigurationLoader.java
@@ -43,13 +43,6 @@ import org.demoiselle.jee.core.annotation.Ignore; | @@ -43,13 +43,6 @@ import org.demoiselle.jee.core.annotation.Ignore; | ||
43 | import org.demoiselle.jee.core.annotation.Name; | 43 | import org.demoiselle.jee.core.annotation.Name; |
44 | import org.demoiselle.jee.core.annotation.Priority; | 44 | import org.demoiselle.jee.core.annotation.Priority; |
45 | 45 | ||
46 | -/** | ||
47 | - * This component loads a config class annotated with {@link org.demoiselle.jee.configuration.annotation.configuration.Configuration} | ||
48 | - * by filling its attributes with {@link org.demoiselle.jsf.util.Parameter} | ||
49 | - * according to a {@link org.demoiselle.configuration.ConfigType}. | ||
50 | - * | ||
51 | - * @author SERPRO | ||
52 | - */ | ||
53 | @ApplicationScoped | 46 | @ApplicationScoped |
54 | public class ConfigurationLoader implements Serializable { | 47 | public class ConfigurationLoader implements Serializable { |
55 | 48 | ||
@@ -316,14 +309,7 @@ public class ConfigurationLoader implements Serializable { | @@ -316,14 +309,7 @@ public class ConfigurationLoader implements Serializable { | ||
316 | throw new ConfigurationException(message.toString(), new ConstraintViolationException(violations)); | 309 | throw new ConfigurationException(message.toString(), new ConstraintViolationException(violations)); |
317 | } | 310 | } |
318 | } | 311 | } |
319 | - | ||
320 | - | ||
321 | - | ||
322 | 312 | ||
323 | - /** | ||
324 | - * @param type Base type to look for fields | ||
325 | - * @return All non static fields from a certain type, including fields declared in superclasses of this type. | ||
326 | - */ | ||
327 | public List<Field> getNonStaticFields(Class<?> type) { | 313 | public List<Field> getNonStaticFields(Class<?> type) { |
328 | List<Field> fields = new ArrayList<Field>(); | 314 | List<Field> fields = new ArrayList<Field>(); |
329 | 315 | ||
@@ -338,11 +324,6 @@ public class ConfigurationLoader implements Serializable { | @@ -338,11 +324,6 @@ public class ConfigurationLoader implements Serializable { | ||
338 | return fields; | 324 | return fields; |
339 | } | 325 | } |
340 | 326 | ||
341 | - /** | ||
342 | - * @param type Base type to look for fields | ||
343 | - * @return All non static fields from a certain type. Inherited fields are not returned, so if you need to get | ||
344 | - * inherited fields you must iterate over this type's hierarchy. | ||
345 | - */ | ||
346 | public Field[] getNonStaticDeclaredFields(Class<?> type) { | 327 | public Field[] getNonStaticDeclaredFields(Class<?> type) { |
347 | List<Field> fields = new ArrayList<Field>(); | 328 | List<Field> fields = new ArrayList<Field>(); |
348 | 329 | ||
@@ -357,23 +338,11 @@ public class ConfigurationLoader implements Serializable { | @@ -357,23 +338,11 @@ public class ConfigurationLoader implements Serializable { | ||
357 | return fields.toArray(new Field[0]); | 338 | return fields.toArray(new Field[0]); |
358 | } | 339 | } |
359 | 340 | ||
360 | - /** | ||
361 | - * Return an URL to access a resource available to the active classloader for the calling thread. | ||
362 | - * | ||
363 | - * @param resource String representation of the location of the resource on the classpath | ||
364 | - * @return The {@link URL} for the resource | ||
365 | - */ | ||
366 | public URL getResourceAsURL(final String resource) { | 341 | public URL getResourceAsURL(final String resource) { |
367 | ClassLoader classLoader = getClassLoaderForResource(resource); | 342 | ClassLoader classLoader = getClassLoaderForResource(resource); |
368 | return classLoader != null ? classLoader.getResource(resource) : null; | 343 | return classLoader != null ? classLoader.getResource(resource) : null; |
369 | } | 344 | } |
370 | - | ||
371 | - /** | ||
372 | - * Obtains the {@link ClassLoader} for the given resource. | ||
373 | - * | ||
374 | - * @param resource String representation of the fully qualified path to the resource on the classpath | ||
375 | - * @return {@link ClassLoader} ClassLoader for the given resource. | ||
376 | - */ | 345 | + |
377 | public ClassLoader getClassLoaderForResource(final String resource) { | 346 | public ClassLoader getClassLoaderForResource(final String resource) { |
378 | final String stripped = resource.charAt(0) == '/' ? resource.substring(1) : resource; | 347 | final String stripped = resource.charAt(0) == '/' ? resource.substring(1) : resource; |
379 | 348 | ||
@@ -413,13 +382,6 @@ public class ConfigurationLoader implements Serializable { | @@ -413,13 +382,6 @@ public class ConfigurationLoader implements Serializable { | ||
413 | return result; | 382 | return result; |
414 | } | 383 | } |
415 | 384 | ||
416 | - /** | ||
417 | - * Sets a value in a field. | ||
418 | - * | ||
419 | - * @param field field to be setted. | ||
420 | - * @param object object that contains the field. | ||
421 | - * @param value value to be setted in the field. | ||
422 | - */ | ||
423 | public void setFieldValue(Field field, Object object, Object value) { | 385 | public void setFieldValue(Field field, Object object, Object value) { |
424 | try { | 386 | try { |
425 | boolean acessible = field.isAccessible(); | 387 | boolean acessible = field.isAccessible(); |
demoiselle-configuration/src/main/java/org/demoiselle/jee/configuration/annotation/Configuration.java
@@ -14,24 +14,6 @@ import java.lang.annotation.Target; | @@ -14,24 +14,6 @@ import java.lang.annotation.Target; | ||
14 | import static java.lang.annotation.ElementType.TYPE; | 14 | import static java.lang.annotation.ElementType.TYPE; |
15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; | 15 | import static java.lang.annotation.RetentionPolicy.RUNTIME; |
16 | 16 | ||
17 | -/** | ||
18 | - * <p> | ||
19 | - * Identifies a <b>configuration class</b>, that is, a structure reserved to store configuration values retrieved from a | ||
20 | - * given resource file or system variables. | ||
21 | - * </p> | ||
22 | - * <p> | ||
23 | - * Configuration resources are application scoped, meaning only one instance can ever exist in | ||
24 | - * a running application. For that reason usually configuration fields are immutable, to avoid | ||
25 | - * changes made in one context affecting other contexts in a running application. | ||
26 | - * </p> | ||
27 | - * <p>A <i>Configuration</i> is:</p> | ||
28 | - * <ul> | ||
29 | - * <li>defined when annotated with {@code @Configuration}</li> | ||
30 | - * <li>automatically injected whenever {@code @Inject} is used</li> | ||
31 | - * </ul> | ||
32 | - * | ||
33 | - * @author SERPRO | ||
34 | - */ | ||
35 | @ApplicationScoped | 17 | @ApplicationScoped |
36 | @Named | 18 | @Named |
37 | @InterceptorBinding | 19 | @InterceptorBinding |
@@ -40,43 +22,14 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; | @@ -40,43 +22,14 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
40 | @Retention(RUNTIME) | 22 | @Retention(RUNTIME) |
41 | public @interface Configuration { | 23 | public @interface Configuration { |
42 | 24 | ||
43 | - /** | ||
44 | - * Define the default prefix. | ||
45 | - */ | ||
46 | String DEFAULT_PREFIX = "demoiselle"; | 25 | String DEFAULT_PREFIX = "demoiselle"; |
47 | 26 | ||
48 | - /** | ||
49 | - * Define the default resource. | ||
50 | - */ | ||
51 | String DEFAULT_RESOURCE = "demoiselle"; | 27 | String DEFAULT_RESOURCE = "demoiselle"; |
52 | 28 | ||
53 | - /** | ||
54 | - * Defines the resource type to be used: a properties file, an XML file, or system variables. | ||
55 | - * If not specified, a properties resource file is to be considered. | ||
56 | - * | ||
57 | - * @return ConfigType Type of configuration resource file to look for | ||
58 | - */ | ||
59 | @Nonbinding ConfigType type() default ConfigType.PROPERTIES; | 29 | @Nonbinding ConfigType type() default ConfigType.PROPERTIES; |
60 | 30 | ||
61 | - /** | ||
62 | - * Defines an optional prefix to be used on every parameter key. | ||
63 | - * For instance, if prefix is set to <code>"demoiselle.pagination"</code> and an attribute named | ||
64 | - * <code>defaultPageSize</code> is found in the class, the corresponding key | ||
65 | - * <code>demoiselle.pagination.defaultPageSize</code> is expected to be read in the resource file. | ||
66 | - * | ||
67 | - * @return String prefix common to all attributes to be read by the configuration class | ||
68 | - */ | ||
69 | @Nonbinding String prefix() default DEFAULT_PREFIX; | 31 | @Nonbinding String prefix() default DEFAULT_PREFIX; |
70 | 32 | ||
71 | - /** | ||
72 | - * Defines the resource file name to be read by this configuration class. There is no need to specify file extension | ||
73 | - * in the case of properties or XML resources. | ||
74 | - * For instance, when resource is set to <code>"bookmark"</code> and the type set to properties, a corresponding | ||
75 | - * file named <code>bookmark.properties</code> is considered. | ||
76 | - * If not specified, the default configuration file <code>demoiselle.properties</code> is used instead. | ||
77 | - * | ||
78 | - * @return String Name of the resource file to look for (minus file extension) | ||
79 | - */ | ||
80 | @Nonbinding String resource() default DEFAULT_RESOURCE; | 33 | @Nonbinding String resource() default DEFAULT_RESOURCE; |
81 | 34 | ||
82 | } | 35 | } |
demoiselle-configuration/src/main/java/org/demoiselle/jee/configuration/extractor/ConfigurationValueExtractor.java
@@ -4,48 +4,7 @@ import java.lang.reflect.Field; | @@ -4,48 +4,7 @@ import java.lang.reflect.Field; | ||
4 | 4 | ||
5 | import org.apache.commons.configuration2.Configuration; | 5 | import org.apache.commons.configuration2.Configuration; |
6 | 6 | ||
7 | -/** | ||
8 | - * <p> | ||
9 | - * Interface that defines how to convert values extracted from configuration | ||
10 | - * files to fields in a class annotated with {@link Configuration}. | ||
11 | - * </p> | ||
12 | - * | ||
13 | - * <p> | ||
14 | - * Primitive types like <code>int</code> and <code>float</code>, their wrapper | ||
15 | - * counterparts like {@link Integer} and {@link Float} and the {@link String} class | ||
16 | - * can already be converted by the framework, this interface is reserved for specialized | ||
17 | - * classes. | ||
18 | - * </p> | ||
19 | - * | ||
20 | - * @author SERPRO | ||
21 | - */ | ||
22 | public interface ConfigurationValueExtractor { | 7 | public interface ConfigurationValueExtractor { |
23 | - | ||
24 | - /** | ||
25 | - * Method that must appropriately extract the value from a property file and set this value to a | ||
26 | - * field in a configuration class. | ||
27 | - * | ||
28 | - * @param prefix | ||
29 | - * optional parte of property name that must be concatenated with <b>key</b> to form the whole | ||
30 | - * property name. | ||
31 | - * @param key | ||
32 | - * key of the property. | ||
33 | - * @param field | ||
34 | - * configuration field to be setted. | ||
35 | - * @param configuration | ||
36 | - * a configuration object. | ||
37 | - * @return current value of this property | ||
38 | - * @throws Exception if the value can't be extracted from the property file | ||
39 | - */ | ||
40 | Object getValue(String prefix, String key, Field field, Configuration configuration) throws Exception; | 8 | Object getValue(String prefix, String key, Field field, Configuration configuration) throws Exception; |
41 | - | ||
42 | - /** | ||
43 | - * Checks if the extractor class is appropriate to extract values to the type of deffined by parameter | ||
44 | - * <b>field</b>. | ||
45 | - * | ||
46 | - * @param field | ||
47 | - * field to be checked. | ||
48 | - * @return <code>true</code> if this extractor can convert this field into the extractor's final type | ||
49 | - */ | ||
50 | boolean isSupported(Field field); | 9 | boolean isSupported(Field field); |
51 | } | 10 | } |
demoiselle-core/src/main/java/org/demoiselle/jee/core/message/DemoiselleMessage.java
@@ -46,7 +46,7 @@ public interface DemoiselleMessage { | @@ -46,7 +46,7 @@ public interface DemoiselleMessage { | ||
46 | String takingOff(); | 46 | String takingOff(); |
47 | 47 | ||
48 | @MessageTemplate("{engine-off}") | 48 | @MessageTemplate("{engine-off}") |
49 | - String engineOff(String ex); | 49 | + String engineOff(); |
50 | 50 | ||
51 | @MessageTemplate("{setting-up-bean-manager}") | 51 | @MessageTemplate("{setting-up-bean-manager}") |
52 | String settingUpBeanManagerException(String util); | 52 | String settingUpBeanManagerException(String util); |
demoiselle-persistence-jpa/src/main/java/org/demoiselle/jee/persistence/jpa/crud/GenericCrudDAO.java
@@ -60,13 +60,6 @@ public abstract class GenericCrudDAO<T> { | @@ -60,13 +60,6 @@ public abstract class GenericCrudDAO<T> { | ||
60 | public GenericDataPage pageResult(String sort, String order, Integer from, Integer size, String search, | 60 | public GenericDataPage pageResult(String sort, String order, Integer from, Integer size, String search, |
61 | String fields, HashMap<String, String> filter) { | 61 | String fields, HashMap<String, String> filter) { |
62 | 62 | ||
63 | - // TODO: Rever esta validação | ||
64 | - // if (GPUtils.isEmpty(sort, order) || !((order.equalsIgnoreCase("asc") | ||
65 | - // || order.equalsIgnoreCase("desc")) | ||
66 | - // && (GPUtils.fieldInClass(sort, this.entityClass)))) { | ||
67 | - // throw new GPException(GPMessage.LIST_PARAM_ERROR); | ||
68 | - // } | ||
69 | - | ||
70 | if (from == null) { | 63 | if (from == null) { |
71 | from = 0; | 64 | from = 0; |
72 | } | 65 | } |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/annotation/Cache.java
0 → 100644
@@ -0,0 +1,29 @@ | @@ -0,0 +1,29 @@ | ||
1 | +/* | ||
2 | + * To change this license header, choose License Headers in Project Properties. | ||
3 | + * To change this template file, choose Tools | Templates | ||
4 | + * and open the template in the editor. | ||
5 | + */ | ||
6 | +package org.demoiselle.jee.rest.annotation; | ||
7 | + | ||
8 | +import static java.lang.annotation.ElementType.METHOD; | ||
9 | +import static java.lang.annotation.ElementType.TYPE; | ||
10 | +import java.lang.annotation.Inherited; | ||
11 | +import java.lang.annotation.Retention; | ||
12 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | +import java.lang.annotation.Target; | ||
14 | +import javax.enterprise.util.Nonbinding; | ||
15 | +import javax.interceptor.InterceptorBinding; | ||
16 | + | ||
17 | +/** | ||
18 | + * | ||
19 | + * @author 70744416353 | ||
20 | + */ | ||
21 | +@Inherited | ||
22 | +@InterceptorBinding | ||
23 | +@Target({ METHOD, TYPE }) | ||
24 | +@Retention(RUNTIME) | ||
25 | +public @interface Cache { | ||
26 | + | ||
27 | + @Nonbinding | ||
28 | + String value() default "max-age=0"; | ||
29 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/annotation/CorsAllowMethods.java
0 → 100644
@@ -0,0 +1,26 @@ | @@ -0,0 +1,26 @@ | ||
1 | +/* | ||
2 | + * To change this license header, choose License Headers in Project Properties. | ||
3 | + * To change this template file, choose Tools | Templates | ||
4 | + * and open the template in the editor. | ||
5 | + */ | ||
6 | +package org.demoiselle.jee.rest.annotation; | ||
7 | + | ||
8 | +import static java.lang.annotation.ElementType.METHOD; | ||
9 | +import static java.lang.annotation.ElementType.TYPE; | ||
10 | +import java.lang.annotation.Inherited; | ||
11 | +import java.lang.annotation.Retention; | ||
12 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | +import java.lang.annotation.Target; | ||
14 | +import javax.enterprise.util.Nonbinding; | ||
15 | +import javax.interceptor.InterceptorBinding; | ||
16 | + | ||
17 | +/** | ||
18 | + * | ||
19 | + * @author 70744416353 | ||
20 | + */ | ||
21 | +@Inherited | ||
22 | +@InterceptorBinding | ||
23 | +@Target({METHOD, TYPE}) | ||
24 | +@Retention(RUNTIME) | ||
25 | +public @interface CorsAllowMethods { | ||
26 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/annotation/CorsAllowOrigin.java
0 → 100644
@@ -0,0 +1,29 @@ | @@ -0,0 +1,29 @@ | ||
1 | +/* | ||
2 | + * To change this license header, choose License Headers in Project Properties. | ||
3 | + * To change this template file, choose Tools | Templates | ||
4 | + * and open the template in the editor. | ||
5 | + */ | ||
6 | +package org.demoiselle.jee.rest.annotation; | ||
7 | + | ||
8 | +import static java.lang.annotation.ElementType.METHOD; | ||
9 | +import static java.lang.annotation.ElementType.TYPE; | ||
10 | +import java.lang.annotation.Inherited; | ||
11 | +import java.lang.annotation.Retention; | ||
12 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | +import java.lang.annotation.Target; | ||
14 | +import javax.enterprise.util.Nonbinding; | ||
15 | +import javax.interceptor.InterceptorBinding; | ||
16 | + | ||
17 | +/** | ||
18 | + * | ||
19 | + * @author 70744416353 | ||
20 | + */ | ||
21 | +@Inherited | ||
22 | +@InterceptorBinding | ||
23 | +@Target({METHOD, TYPE}) | ||
24 | +@Retention(RUNTIME) | ||
25 | +public @interface CorsAllowOrigin { | ||
26 | + | ||
27 | + @Nonbinding | ||
28 | + String value() default "*"; | ||
29 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/annotation/CorsMaxAge.java
0 → 100644
@@ -0,0 +1,29 @@ | @@ -0,0 +1,29 @@ | ||
1 | +/* | ||
2 | + * To change this license header, choose License Headers in Project Properties. | ||
3 | + * To change this template file, choose Tools | Templates | ||
4 | + * and open the template in the editor. | ||
5 | + */ | ||
6 | +package org.demoiselle.jee.rest.annotation; | ||
7 | + | ||
8 | +import static java.lang.annotation.ElementType.METHOD; | ||
9 | +import static java.lang.annotation.ElementType.TYPE; | ||
10 | +import java.lang.annotation.Inherited; | ||
11 | +import java.lang.annotation.Retention; | ||
12 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | +import java.lang.annotation.Target; | ||
14 | +import javax.enterprise.util.Nonbinding; | ||
15 | +import javax.interceptor.InterceptorBinding; | ||
16 | + | ||
17 | +/** | ||
18 | + * | ||
19 | + * @author 70744416353 | ||
20 | + */ | ||
21 | +@Inherited | ||
22 | +@InterceptorBinding | ||
23 | +@Target({METHOD, TYPE}) | ||
24 | +@Retention(RUNTIME) | ||
25 | +public @interface CorsMaxAge { | ||
26 | + | ||
27 | + @Nonbinding | ||
28 | + String value() default "0"; | ||
29 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/annotation/ValidatePayload.java
0 → 100644
@@ -0,0 +1,25 @@ | @@ -0,0 +1,25 @@ | ||
1 | +/* | ||
2 | + * Demoiselle Framework | ||
3 | + * | ||
4 | + * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | + * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | + */ | ||
7 | +package org.demoiselle.jee.rest.annotation; | ||
8 | + | ||
9 | +import static java.lang.annotation.ElementType.METHOD; | ||
10 | +import static java.lang.annotation.ElementType.TYPE; | ||
11 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
12 | + | ||
13 | +import java.lang.annotation.Inherited; | ||
14 | +import java.lang.annotation.Retention; | ||
15 | +import java.lang.annotation.Target; | ||
16 | + | ||
17 | +import javax.interceptor.InterceptorBinding; | ||
18 | + | ||
19 | +@Inherited | ||
20 | +@InterceptorBinding | ||
21 | +@Target({ METHOD, TYPE }) | ||
22 | +@Retention(RUNTIME) | ||
23 | +public @interface ValidatePayload { | ||
24 | + | ||
25 | +} | ||
0 | \ No newline at end of file | 26 | \ No newline at end of file |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/crud/package-info.java
0 → 100644
@@ -0,0 +1,11 @@ | @@ -0,0 +1,11 @@ | ||
1 | +/* | ||
2 | + * Demoiselle Framework | ||
3 | + * | ||
4 | + * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | + * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | + */ | ||
7 | +/** | ||
8 | + * Esta pacote tem o objetivo de conter as classes relacionadas aos | ||
9 | + * facilitadores de CRUD do framework Demoiselle. | ||
10 | + */ | ||
11 | +package org.demoiselle.jee.rest.crud; | ||
0 | \ No newline at end of file | 12 | \ No newline at end of file |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/exception/DemoiselleRESTException.java
0 → 100644
@@ -0,0 +1,41 @@ | @@ -0,0 +1,41 @@ | ||
1 | +/* | ||
2 | + * Demoiselle Framework | ||
3 | + * | ||
4 | + * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | + * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | + */ | ||
7 | +package org.demoiselle.jee.rest.exception; | ||
8 | + | ||
9 | +import java.util.HashMap; | ||
10 | + | ||
11 | +import org.demoiselle.jee.core.exception.DemoiselleException; | ||
12 | + | ||
13 | +public class DemoiselleRESTException extends DemoiselleException { | ||
14 | + | ||
15 | + private static final long serialVersionUID = 519965615171844237L; | ||
16 | + | ||
17 | + private HashMap<String, String> messages = new HashMap<>(); | ||
18 | + | ||
19 | + private int statusCode; | ||
20 | + | ||
21 | + public DemoiselleRESTException() { | ||
22 | + | ||
23 | + } | ||
24 | + | ||
25 | + public DemoiselleRESTException(String string) { | ||
26 | + super(string); | ||
27 | + } | ||
28 | + | ||
29 | + public int getStatusCode() { | ||
30 | + return statusCode; | ||
31 | + } | ||
32 | + | ||
33 | + public void addMessage(String field, String msg) { | ||
34 | + this.statusCode = 422; | ||
35 | + messages.put(field, msg); | ||
36 | + } | ||
37 | + | ||
38 | + public HashMap<String, String> getMessages() { | ||
39 | + return messages; | ||
40 | + } | ||
41 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/exception/mapper/GenericExceptionMapper.java
0 → 100644
@@ -0,0 +1,79 @@ | @@ -0,0 +1,79 @@ | ||
1 | +/* | ||
2 | + * Demoiselle Framework | ||
3 | + * | ||
4 | + * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | + * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | + */ | ||
7 | +package org.demoiselle.jee.rest.exception.mapper; | ||
8 | + | ||
9 | +import java.io.PrintWriter; | ||
10 | +import java.io.StringWriter; | ||
11 | +import java.util.HashMap; | ||
12 | + | ||
13 | +import javax.ws.rs.WebApplicationException; | ||
14 | +import javax.ws.rs.core.MediaType; | ||
15 | +import static javax.ws.rs.core.MediaType.APPLICATION_JSON; | ||
16 | +import javax.ws.rs.core.Response; | ||
17 | +import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; | ||
18 | +import static javax.ws.rs.core.Response.status; | ||
19 | +import static javax.ws.rs.core.Response.status; | ||
20 | +import static javax.ws.rs.core.Response.status; | ||
21 | +import static javax.ws.rs.core.Response.status; | ||
22 | +import javax.ws.rs.ext.ExceptionMapper; | ||
23 | +import javax.ws.rs.ext.Provider; | ||
24 | + | ||
25 | +import org.demoiselle.jee.rest.exception.DemoiselleRESTException; | ||
26 | + | ||
27 | +@Provider | ||
28 | +public class GenericExceptionMapper implements ExceptionMapper<Exception> { | ||
29 | + | ||
30 | + public Response toResponse(Exception ex) { | ||
31 | + | ||
32 | + StringWriter errorStackTrace = new StringWriter(); | ||
33 | + ex.printStackTrace(new PrintWriter(errorStackTrace)); | ||
34 | + HashMap<String, String> entity = new HashMap<>(); | ||
35 | + | ||
36 | + // Verifica se a exception é de validação de PAYLOAD do REST | ||
37 | + if (ex instanceof DemoiselleRESTException) { | ||
38 | + DemoiselleRESTException exDemoiselleREST = (DemoiselleRESTException) ex; | ||
39 | + if (!exDemoiselleREST.getMessages().isEmpty()) { | ||
40 | + entity.put("error", exDemoiselleREST.getMessages().toString()); | ||
41 | + return status(exDemoiselleREST.getStatusCode()).entity(entity) | ||
42 | + .type(APPLICATION_JSON).build(); | ||
43 | + } else if (exDemoiselleREST.getStatusCode() > 0) { | ||
44 | + entity.put("error", exDemoiselleREST.getMessage()); | ||
45 | + return status(exDemoiselleREST.getStatusCode()).entity(entity) | ||
46 | + .type(APPLICATION_JSON).build(); | ||
47 | + } | ||
48 | + | ||
49 | + } | ||
50 | + | ||
51 | + // No caso de existir message ele mostra a MESSAGE da Exception | ||
52 | + if (ex.getMessage() != null) { | ||
53 | + entity.put("error", ex.getMessage()); | ||
54 | + | ||
55 | + // Pega toda as mensagens da stacktrace | ||
56 | + int level = 1; | ||
57 | + while (ex.getCause() != null) { | ||
58 | + ex = (Exception) ex.getCause(); | ||
59 | + if (ex != null && ex.getMessage() != null && !ex.getMessage().isEmpty()) { | ||
60 | + entity.put("inner_cause_" + level, ex.getMessage()); | ||
61 | + } | ||
62 | + level += 1; | ||
63 | + } | ||
64 | + | ||
65 | + // Por padrão retorna SERVER ERROR, mas tenta encontrar o status do RESPONSE se for WebApplicationException | ||
66 | + // http://docs.oracle.com/javaee/7/api/javax/ws/rs/WebApplicationException.html | ||
67 | + int responseCode = INTERNAL_SERVER_ERROR.getStatusCode(); | ||
68 | + if (ex instanceof WebApplicationException) { | ||
69 | + responseCode = ((WebApplicationException) ex).getResponse().getStatus(); | ||
70 | + } | ||
71 | + | ||
72 | + return status(responseCode).entity(entity).type(APPLICATION_JSON).build(); | ||
73 | + } | ||
74 | + | ||
75 | + entity.put("error", "Erro interno desconhecido no servidor."); | ||
76 | + return status(500).entity(entity).type(APPLICATION_JSON).build(); | ||
77 | + } | ||
78 | + | ||
79 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/filter/JaxRsFilter.java
0 → 100644
@@ -0,0 +1,74 @@ | @@ -0,0 +1,74 @@ | ||
1 | +/* | ||
2 | + * Demoiselle Framework | ||
3 | + * | ||
4 | + * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | + * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | + */ | ||
7 | +package org.demoiselle.jee.rest.filter; | ||
8 | + | ||
9 | +import java.util.logging.Logger; | ||
10 | +import javax.annotation.PostConstruct; | ||
11 | +import javax.inject.Inject; | ||
12 | +import javax.ws.rs.container.ContainerRequestContext; | ||
13 | +import javax.ws.rs.container.ContainerRequestFilter; | ||
14 | +import javax.ws.rs.container.ContainerResponseContext; | ||
15 | +import javax.ws.rs.container.ContainerResponseFilter; | ||
16 | +import javax.ws.rs.container.PreMatching; | ||
17 | +import javax.ws.rs.container.ResourceInfo; | ||
18 | +import javax.ws.rs.core.Context; | ||
19 | +import javax.ws.rs.ext.Provider; | ||
20 | + | ||
21 | +import org.demoiselle.jee.rest.annotation.Cache; | ||
22 | +import org.demoiselle.jee.rest.annotation.CorsAllowMethods; | ||
23 | +import org.demoiselle.jee.rest.annotation.CorsAllowOrigin; | ||
24 | + | ||
25 | +/** | ||
26 | + * | ||
27 | + * @author 70744416353 | ||
28 | + */ | ||
29 | +@Provider | ||
30 | +@PreMatching | ||
31 | +public class JaxRsFilter implements ContainerRequestFilter, ContainerResponseFilter { | ||
32 | + | ||
33 | + @Inject | ||
34 | + private Logger LOG; | ||
35 | + | ||
36 | + @Context | ||
37 | + private ResourceInfo info; | ||
38 | + | ||
39 | + @Override | ||
40 | + public void filter(ContainerRequestContext requestContext) { | ||
41 | + } | ||
42 | + | ||
43 | + @Override | ||
44 | + public void filter(ContainerRequestContext requestContext, ContainerResponseContext response) { | ||
45 | + | ||
46 | + response.getHeaders().putSingle("Demoiselle", "3.0.0"); | ||
47 | + response.getHeaders().putSingle("Access-Control-Allow-Origin", "*"); | ||
48 | + response.getHeaders().putSingle("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE"); | ||
49 | + response.getHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type"); | ||
50 | + | ||
51 | + if (requestContext.getMethod().equals("GET")) { | ||
52 | + Cache max = info.getResourceMethod().getAnnotation(Cache.class); | ||
53 | + if (max != null) { | ||
54 | + response.getHeaders().putSingle("Cache-Control", max.value()); | ||
55 | + } | ||
56 | + } | ||
57 | + | ||
58 | +// CorsAllowMethods corsAllowMethods = info.getResourceMethod().getAnnotation(CorsAllowMethods.class); | ||
59 | +// if (corsAllowMethods != null) { | ||
60 | +// response.getHeaders().putSingle("Access-Control-Allow-Methods", requestContext.getMethod()); | ||
61 | +// } | ||
62 | +// | ||
63 | +// CorsAllowOrigin corsAllowOrigin = info.getResourceMethod().getAnnotation(CorsAllowOrigin.class); | ||
64 | +// if (corsAllowOrigin != null) { | ||
65 | +// response.getHeaders().putSingle("Access-Control-Allow-Origin", corsAllowOrigin.value()); | ||
66 | +// } | ||
67 | + } | ||
68 | + | ||
69 | + @PostConstruct | ||
70 | + public void init() { | ||
71 | + LOG.info("Demoiselle Module - Rest"); | ||
72 | + } | ||
73 | + | ||
74 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/rest/interceptor/ValidatePayloadInterceptor.java
0 → 100644
@@ -0,0 +1,67 @@ | @@ -0,0 +1,67 @@ | ||
1 | +/* | ||
2 | + * Demoiselle Framework | ||
3 | + * | ||
4 | + * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | + * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | + */ | ||
7 | +package org.demoiselle.jee.rest.interceptor; | ||
8 | + | ||
9 | +import static javax.interceptor.Interceptor.Priority.APPLICATION; | ||
10 | +import static javax.validation.Validation.buildDefaultValidatorFactory; | ||
11 | + | ||
12 | +import java.io.Serializable; | ||
13 | +import java.util.HashSet; | ||
14 | +import java.util.Set; | ||
15 | + | ||
16 | +import javax.annotation.Priority; | ||
17 | +import javax.interceptor.AroundInvoke; | ||
18 | +import javax.interceptor.Interceptor; | ||
19 | +import javax.interceptor.InvocationContext; | ||
20 | +import javax.validation.ConstraintViolation; | ||
21 | +import javax.validation.UnexpectedTypeException; | ||
22 | +import javax.validation.Validator; | ||
23 | +import javax.validation.ValidatorFactory; | ||
24 | + | ||
25 | +import org.demoiselle.jee.rest.annotation.ValidatePayload; | ||
26 | +import org.demoiselle.jee.rest.exception.DemoiselleRESTException; | ||
27 | + | ||
28 | +@Interceptor | ||
29 | +@ValidatePayload | ||
30 | +@Priority(APPLICATION) | ||
31 | +public class ValidatePayloadInterceptor implements Serializable { | ||
32 | + | ||
33 | + private static final long serialVersionUID = 1L; | ||
34 | + | ||
35 | + @AroundInvoke | ||
36 | + public Object manage(final InvocationContext ic) throws Exception { | ||
37 | + DemoiselleRESTException ex = new DemoiselleRESTException(); | ||
38 | + Set<ConstraintViolation<?>> violations = new HashSet<>(); | ||
39 | + for (Object params : ic.getParameters()) { | ||
40 | + if (params != null) { | ||
41 | + ValidatorFactory dfv = buildDefaultValidatorFactory(); | ||
42 | + Validator validator = dfv.getValidator(); | ||
43 | + try { | ||
44 | + violations.addAll(validator.validate(params)); | ||
45 | + for (ConstraintViolation<?> violation : violations) { | ||
46 | + String field = (violation.getRootBeanClass().getSimpleName() + "_" | ||
47 | + + violation.getPropertyPath()).toLowerCase(); | ||
48 | + // GPMessage msg = | ||
49 | + // GPMessage.INVALID_FIELD_P1.setSufix(violation.getConstraintDescriptor() | ||
50 | + // .getAnnotation().annotationType().getSimpleName().toLowerCase()); | ||
51 | + | ||
52 | + ex.addMessage(field, violation.getMessage()); | ||
53 | + } | ||
54 | + } catch (UnexpectedTypeException cause) { | ||
55 | + // GPMessage msg = GPMessage.GENERAL_ERROR_P1; | ||
56 | + // msg.setParam(cause.getMessage()); | ||
57 | + throw new DemoiselleRESTException("ERRO GENERICO -> ALTERAR"); | ||
58 | + } | ||
59 | + } | ||
60 | + } | ||
61 | + | ||
62 | + if (!violations.isEmpty() && !ex.getMessages().isEmpty()) { | ||
63 | + throw ex; | ||
64 | + } | ||
65 | + return ic.proceed(); | ||
66 | + } | ||
67 | +} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/annotation/Cache.java
@@ -1,29 +0,0 @@ | @@ -1,29 +0,0 @@ | ||
1 | -/* | ||
2 | - * To change this license header, choose License Headers in Project Properties. | ||
3 | - * To change this template file, choose Tools | Templates | ||
4 | - * and open the template in the editor. | ||
5 | - */ | ||
6 | -package org.demoiselle.jee.ws.jaxrs.annotation; | ||
7 | - | ||
8 | -import static java.lang.annotation.ElementType.METHOD; | ||
9 | -import static java.lang.annotation.ElementType.TYPE; | ||
10 | -import java.lang.annotation.Inherited; | ||
11 | -import java.lang.annotation.Retention; | ||
12 | -import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | -import java.lang.annotation.Target; | ||
14 | -import javax.enterprise.util.Nonbinding; | ||
15 | -import javax.interceptor.InterceptorBinding; | ||
16 | - | ||
17 | -/** | ||
18 | - * | ||
19 | - * @author 70744416353 | ||
20 | - */ | ||
21 | -@Inherited | ||
22 | -@InterceptorBinding | ||
23 | -@Target({ METHOD, TYPE }) | ||
24 | -@Retention(RUNTIME) | ||
25 | -public @interface Cache { | ||
26 | - | ||
27 | - @Nonbinding | ||
28 | - String value() default "max-age=0"; | ||
29 | -} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/annotation/CorsAllowMethods.java
@@ -1,26 +0,0 @@ | @@ -1,26 +0,0 @@ | ||
1 | -/* | ||
2 | - * To change this license header, choose License Headers in Project Properties. | ||
3 | - * To change this template file, choose Tools | Templates | ||
4 | - * and open the template in the editor. | ||
5 | - */ | ||
6 | -package org.demoiselle.jee.ws.jaxrs.annotation; | ||
7 | - | ||
8 | -import static java.lang.annotation.ElementType.METHOD; | ||
9 | -import static java.lang.annotation.ElementType.TYPE; | ||
10 | -import java.lang.annotation.Inherited; | ||
11 | -import java.lang.annotation.Retention; | ||
12 | -import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | -import java.lang.annotation.Target; | ||
14 | -import javax.enterprise.util.Nonbinding; | ||
15 | -import javax.interceptor.InterceptorBinding; | ||
16 | - | ||
17 | -/** | ||
18 | - * | ||
19 | - * @author 70744416353 | ||
20 | - */ | ||
21 | -@Inherited | ||
22 | -@InterceptorBinding | ||
23 | -@Target({METHOD, TYPE}) | ||
24 | -@Retention(RUNTIME) | ||
25 | -public @interface CorsAllowMethods { | ||
26 | -} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/annotation/CorsAllowOrigin.java
@@ -1,29 +0,0 @@ | @@ -1,29 +0,0 @@ | ||
1 | -/* | ||
2 | - * To change this license header, choose License Headers in Project Properties. | ||
3 | - * To change this template file, choose Tools | Templates | ||
4 | - * and open the template in the editor. | ||
5 | - */ | ||
6 | -package org.demoiselle.jee.ws.jaxrs.annotation; | ||
7 | - | ||
8 | -import static java.lang.annotation.ElementType.METHOD; | ||
9 | -import static java.lang.annotation.ElementType.TYPE; | ||
10 | -import java.lang.annotation.Inherited; | ||
11 | -import java.lang.annotation.Retention; | ||
12 | -import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | -import java.lang.annotation.Target; | ||
14 | -import javax.enterprise.util.Nonbinding; | ||
15 | -import javax.interceptor.InterceptorBinding; | ||
16 | - | ||
17 | -/** | ||
18 | - * | ||
19 | - * @author 70744416353 | ||
20 | - */ | ||
21 | -@Inherited | ||
22 | -@InterceptorBinding | ||
23 | -@Target({METHOD, TYPE}) | ||
24 | -@Retention(RUNTIME) | ||
25 | -public @interface CorsAllowOrigin { | ||
26 | - | ||
27 | - @Nonbinding | ||
28 | - String value() default "*"; | ||
29 | -} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/annotation/CorsMaxAge.java
@@ -1,29 +0,0 @@ | @@ -1,29 +0,0 @@ | ||
1 | -/* | ||
2 | - * To change this license header, choose License Headers in Project Properties. | ||
3 | - * To change this template file, choose Tools | Templates | ||
4 | - * and open the template in the editor. | ||
5 | - */ | ||
6 | -package org.demoiselle.jee.ws.jaxrs.annotation; | ||
7 | - | ||
8 | -import static java.lang.annotation.ElementType.METHOD; | ||
9 | -import static java.lang.annotation.ElementType.TYPE; | ||
10 | -import java.lang.annotation.Inherited; | ||
11 | -import java.lang.annotation.Retention; | ||
12 | -import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
13 | -import java.lang.annotation.Target; | ||
14 | -import javax.enterprise.util.Nonbinding; | ||
15 | -import javax.interceptor.InterceptorBinding; | ||
16 | - | ||
17 | -/** | ||
18 | - * | ||
19 | - * @author 70744416353 | ||
20 | - */ | ||
21 | -@Inherited | ||
22 | -@InterceptorBinding | ||
23 | -@Target({METHOD, TYPE}) | ||
24 | -@Retention(RUNTIME) | ||
25 | -public @interface CorsMaxAge { | ||
26 | - | ||
27 | - @Nonbinding | ||
28 | - String value() default "0"; | ||
29 | -} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/annotation/ValidatePayload.java
@@ -1,25 +0,0 @@ | @@ -1,25 +0,0 @@ | ||
1 | -/* | ||
2 | - * Demoiselle Framework | ||
3 | - * | ||
4 | - * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | - * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | - */ | ||
7 | -package org.demoiselle.jee.ws.jaxrs.annotation; | ||
8 | - | ||
9 | -import static java.lang.annotation.ElementType.METHOD; | ||
10 | -import static java.lang.annotation.ElementType.TYPE; | ||
11 | -import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
12 | - | ||
13 | -import java.lang.annotation.Inherited; | ||
14 | -import java.lang.annotation.Retention; | ||
15 | -import java.lang.annotation.Target; | ||
16 | - | ||
17 | -import javax.interceptor.InterceptorBinding; | ||
18 | - | ||
19 | -@Inherited | ||
20 | -@InterceptorBinding | ||
21 | -@Target({ METHOD, TYPE }) | ||
22 | -@Retention(RUNTIME) | ||
23 | -public @interface ValidatePayload { | ||
24 | - | ||
25 | -} | ||
26 | \ No newline at end of file | 0 | \ No newline at end of file |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/crud/package-info.java
@@ -1,11 +0,0 @@ | @@ -1,11 +0,0 @@ | ||
1 | -/* | ||
2 | - * Demoiselle Framework | ||
3 | - * | ||
4 | - * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | - * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | - */ | ||
7 | -/** | ||
8 | - * Esta pacote tem o objetivo de conter as classes relacionadas aos | ||
9 | - * facilitadores de CRUD do framework Demoiselle. | ||
10 | - */ | ||
11 | -package org.demoiselle.jee.ws.jaxrs.crud; | ||
12 | \ No newline at end of file | 0 | \ No newline at end of file |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/exception/DemoiselleRESTException.java
@@ -1,41 +0,0 @@ | @@ -1,41 +0,0 @@ | ||
1 | -/* | ||
2 | - * Demoiselle Framework | ||
3 | - * | ||
4 | - * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | - * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | - */ | ||
7 | -package org.demoiselle.jee.ws.jaxrs.exception; | ||
8 | - | ||
9 | -import java.util.HashMap; | ||
10 | - | ||
11 | -import org.demoiselle.jee.core.exception.DemoiselleException; | ||
12 | - | ||
13 | -public class DemoiselleRESTException extends DemoiselleException { | ||
14 | - | ||
15 | - private static final long serialVersionUID = 519965615171844237L; | ||
16 | - | ||
17 | - private HashMap<String, String> messages = new HashMap<>(); | ||
18 | - | ||
19 | - private int statusCode; | ||
20 | - | ||
21 | - public DemoiselleRESTException() { | ||
22 | - | ||
23 | - } | ||
24 | - | ||
25 | - public DemoiselleRESTException(String string) { | ||
26 | - super(string); | ||
27 | - } | ||
28 | - | ||
29 | - public int getStatusCode() { | ||
30 | - return statusCode; | ||
31 | - } | ||
32 | - | ||
33 | - public void addMessage(String field, String msg) { | ||
34 | - this.statusCode = 422; | ||
35 | - messages.put(field, msg); | ||
36 | - } | ||
37 | - | ||
38 | - public HashMap<String, String> getMessages() { | ||
39 | - return messages; | ||
40 | - } | ||
41 | -} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/exception/mapper/GenericExceptionMapper.java
@@ -1,79 +0,0 @@ | @@ -1,79 +0,0 @@ | ||
1 | -/* | ||
2 | - * Demoiselle Framework | ||
3 | - * | ||
4 | - * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | - * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | - */ | ||
7 | -package org.demoiselle.jee.ws.jaxrs.exception.mapper; | ||
8 | - | ||
9 | -import java.io.PrintWriter; | ||
10 | -import java.io.StringWriter; | ||
11 | -import java.util.HashMap; | ||
12 | - | ||
13 | -import javax.ws.rs.WebApplicationException; | ||
14 | -import javax.ws.rs.core.MediaType; | ||
15 | -import static javax.ws.rs.core.MediaType.APPLICATION_JSON; | ||
16 | -import javax.ws.rs.core.Response; | ||
17 | -import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR; | ||
18 | -import static javax.ws.rs.core.Response.status; | ||
19 | -import static javax.ws.rs.core.Response.status; | ||
20 | -import static javax.ws.rs.core.Response.status; | ||
21 | -import static javax.ws.rs.core.Response.status; | ||
22 | -import javax.ws.rs.ext.ExceptionMapper; | ||
23 | -import javax.ws.rs.ext.Provider; | ||
24 | - | ||
25 | -import org.demoiselle.jee.ws.jaxrs.exception.DemoiselleRESTException; | ||
26 | - | ||
27 | -@Provider | ||
28 | -public class GenericExceptionMapper implements ExceptionMapper<Exception> { | ||
29 | - | ||
30 | - public Response toResponse(Exception ex) { | ||
31 | - | ||
32 | - StringWriter errorStackTrace = new StringWriter(); | ||
33 | - ex.printStackTrace(new PrintWriter(errorStackTrace)); | ||
34 | - HashMap<String, String> entity = new HashMap<>(); | ||
35 | - | ||
36 | - // Verifica se a exception é de validação de PAYLOAD do REST | ||
37 | - if (ex instanceof DemoiselleRESTException) { | ||
38 | - DemoiselleRESTException exDemoiselleREST = (DemoiselleRESTException) ex; | ||
39 | - if (!exDemoiselleREST.getMessages().isEmpty()) { | ||
40 | - entity.put("error", exDemoiselleREST.getMessages().toString()); | ||
41 | - return status(exDemoiselleREST.getStatusCode()).entity(entity) | ||
42 | - .type(APPLICATION_JSON).build(); | ||
43 | - } else if (exDemoiselleREST.getStatusCode() > 0) { | ||
44 | - entity.put("error", exDemoiselleREST.getMessage()); | ||
45 | - return status(exDemoiselleREST.getStatusCode()).entity(entity) | ||
46 | - .type(APPLICATION_JSON).build(); | ||
47 | - } | ||
48 | - | ||
49 | - } | ||
50 | - | ||
51 | - // No caso de existir message ele mostra a MESSAGE da Exception | ||
52 | - if (ex.getMessage() != null) { | ||
53 | - entity.put("error", ex.getMessage()); | ||
54 | - | ||
55 | - // Pega toda as mensagens da stacktrace | ||
56 | - int level = 1; | ||
57 | - while (ex.getCause() != null) { | ||
58 | - ex = (Exception) ex.getCause(); | ||
59 | - if (ex != null && ex.getMessage() != null && !ex.getMessage().isEmpty()) { | ||
60 | - entity.put("inner_cause_" + level, ex.getMessage()); | ||
61 | - } | ||
62 | - level += 1; | ||
63 | - } | ||
64 | - | ||
65 | - // Por padrão retorna SERVER ERROR, mas tenta encontrar o status do RESPONSE se for WebApplicationException | ||
66 | - // http://docs.oracle.com/javaee/7/api/javax/ws/rs/WebApplicationException.html | ||
67 | - int responseCode = INTERNAL_SERVER_ERROR.getStatusCode(); | ||
68 | - if (ex instanceof WebApplicationException) { | ||
69 | - responseCode = ((WebApplicationException) ex).getResponse().getStatus(); | ||
70 | - } | ||
71 | - | ||
72 | - return status(responseCode).entity(entity).type(APPLICATION_JSON).build(); | ||
73 | - } | ||
74 | - | ||
75 | - entity.put("error", "Erro interno desconhecido no servidor."); | ||
76 | - return status(500).entity(entity).type(APPLICATION_JSON).build(); | ||
77 | - } | ||
78 | - | ||
79 | -} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/filter/JaxRsFilter.java
@@ -1,73 +0,0 @@ | @@ -1,73 +0,0 @@ | ||
1 | -/* | ||
2 | - * Demoiselle Framework | ||
3 | - * | ||
4 | - * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | - * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | - */ | ||
7 | -package org.demoiselle.jee.ws.jaxrs.filter; | ||
8 | - | ||
9 | -import java.util.logging.Logger; | ||
10 | -import javax.annotation.PostConstruct; | ||
11 | -import javax.inject.Inject; | ||
12 | -import javax.ws.rs.container.ContainerRequestContext; | ||
13 | -import javax.ws.rs.container.ContainerRequestFilter; | ||
14 | -import javax.ws.rs.container.ContainerResponseContext; | ||
15 | -import javax.ws.rs.container.ContainerResponseFilter; | ||
16 | -import javax.ws.rs.container.PreMatching; | ||
17 | -import javax.ws.rs.container.ResourceInfo; | ||
18 | -import javax.ws.rs.core.Context; | ||
19 | -import javax.ws.rs.ext.Provider; | ||
20 | -import org.demoiselle.jee.ws.jaxrs.annotation.Cache; | ||
21 | -import org.demoiselle.jee.ws.jaxrs.annotation.CorsAllowMethods; | ||
22 | -import org.demoiselle.jee.ws.jaxrs.annotation.CorsAllowOrigin; | ||
23 | - | ||
24 | -/** | ||
25 | - * | ||
26 | - * @author 70744416353 | ||
27 | - */ | ||
28 | -@Provider | ||
29 | -@PreMatching | ||
30 | -public class JaxRsFilter implements ContainerRequestFilter, ContainerResponseFilter { | ||
31 | - | ||
32 | - @Inject | ||
33 | - private Logger LOG; | ||
34 | - | ||
35 | - @Context | ||
36 | - private ResourceInfo info; | ||
37 | - | ||
38 | - @Override | ||
39 | - public void filter(ContainerRequestContext requestContext) { | ||
40 | - } | ||
41 | - | ||
42 | - @Override | ||
43 | - public void filter(ContainerRequestContext requestContext, ContainerResponseContext response) { | ||
44 | - | ||
45 | - response.getHeaders().putSingle("Demoiselle", "3.0.0"); | ||
46 | - response.getHeaders().putSingle("Access-Control-Allow-Origin", "*"); | ||
47 | - response.getHeaders().putSingle("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PUT, DELETE"); | ||
48 | - response.getHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type"); | ||
49 | - | ||
50 | - if (requestContext.getMethod().equals("GET")) { | ||
51 | - Cache max = info.getResourceMethod().getAnnotation(Cache.class); | ||
52 | - if (max != null) { | ||
53 | - response.getHeaders().putSingle("Cache-Control", max.value()); | ||
54 | - } | ||
55 | - } | ||
56 | - | ||
57 | -// CorsAllowMethods corsAllowMethods = info.getResourceMethod().getAnnotation(CorsAllowMethods.class); | ||
58 | -// if (corsAllowMethods != null) { | ||
59 | -// response.getHeaders().putSingle("Access-Control-Allow-Methods", requestContext.getMethod()); | ||
60 | -// } | ||
61 | -// | ||
62 | -// CorsAllowOrigin corsAllowOrigin = info.getResourceMethod().getAnnotation(CorsAllowOrigin.class); | ||
63 | -// if (corsAllowOrigin != null) { | ||
64 | -// response.getHeaders().putSingle("Access-Control-Allow-Origin", corsAllowOrigin.value()); | ||
65 | -// } | ||
66 | - } | ||
67 | - | ||
68 | - @PostConstruct | ||
69 | - public void init() { | ||
70 | - LOG.info("Demoiselle Module - Rest"); | ||
71 | - } | ||
72 | - | ||
73 | -} |
demoiselle-rest/src/main/java/org/demoiselle/jee/ws/jaxrs/interceptor/ValidatePayloadInterceptor.java
@@ -1,67 +0,0 @@ | @@ -1,67 +0,0 @@ | ||
1 | -/* | ||
2 | - * Demoiselle Framework | ||
3 | - * | ||
4 | - * License: GNU Lesser General Public License (LGPL), version 3 or later. | ||
5 | - * See the lgpl.txt file in the root directory or <https://www.gnu.org/licenses/lgpl.html>. | ||
6 | - */ | ||
7 | -package org.demoiselle.jee.ws.jaxrs.interceptor; | ||
8 | - | ||
9 | -import org.demoiselle.jee.ws.jaxrs.annotation.ValidatePayload; | ||
10 | -import java.io.Serializable; | ||
11 | -import java.util.HashSet; | ||
12 | -import java.util.Set; | ||
13 | -import javax.annotation.Priority; | ||
14 | - | ||
15 | -import javax.interceptor.AroundInvoke; | ||
16 | -import javax.interceptor.Interceptor; | ||
17 | -import static javax.interceptor.Interceptor.Priority.APPLICATION; | ||
18 | -import javax.interceptor.InvocationContext; | ||
19 | -import javax.validation.ConstraintViolation; | ||
20 | -import javax.validation.UnexpectedTypeException; | ||
21 | -import javax.validation.Validation; | ||
22 | -import static javax.validation.Validation.buildDefaultValidatorFactory; | ||
23 | -import javax.validation.Validator; | ||
24 | -import javax.validation.ValidatorFactory; | ||
25 | - | ||
26 | -import org.demoiselle.jee.ws.jaxrs.exception.DemoiselleRESTException; | ||
27 | - | ||
28 | -@Interceptor | ||
29 | -@ValidatePayload | ||
30 | -@Priority(APPLICATION) | ||
31 | -public class ValidatePayloadInterceptor implements Serializable { | ||
32 | - | ||
33 | - private static final long serialVersionUID = 1L; | ||
34 | - | ||
35 | - @AroundInvoke | ||
36 | - public Object manage(final InvocationContext ic) throws Exception { | ||
37 | - DemoiselleRESTException ex = new DemoiselleRESTException(); | ||
38 | - Set<ConstraintViolation<?>> violations = new HashSet<>(); | ||
39 | - for (Object params : ic.getParameters()) { | ||
40 | - if (params != null) { | ||
41 | - ValidatorFactory dfv = buildDefaultValidatorFactory(); | ||
42 | - Validator validator = dfv.getValidator(); | ||
43 | - try { | ||
44 | - violations.addAll(validator.validate(params)); | ||
45 | - for (ConstraintViolation<?> violation : violations) { | ||
46 | - String field = (violation.getRootBeanClass().getSimpleName() + "_" | ||
47 | - + violation.getPropertyPath()).toLowerCase(); | ||
48 | - // GPMessage msg = | ||
49 | - // GPMessage.INVALID_FIELD_P1.setSufix(violation.getConstraintDescriptor() | ||
50 | - // .getAnnotation().annotationType().getSimpleName().toLowerCase()); | ||
51 | - | ||
52 | - ex.addMessage(field, violation.getMessage()); | ||
53 | - } | ||
54 | - } catch (UnexpectedTypeException cause) { | ||
55 | - // GPMessage msg = GPMessage.GENERAL_ERROR_P1; | ||
56 | - // msg.setParam(cause.getMessage()); | ||
57 | - throw new DemoiselleRESTException("ERRO GENERICO -> ALTERAR"); | ||
58 | - } | ||
59 | - } | ||
60 | - } | ||
61 | - | ||
62 | - if (!violations.isEmpty() && !ex.getMessages().isEmpty()) { | ||
63 | - throw ex; | ||
64 | - } | ||
65 | - return ic.proceed(); | ||
66 | - } | ||
67 | -} |
demoiselle-security/src/main/java/org/demoiselle/jee/security/exception/DemoiselleSecurityException.java
@@ -8,7 +8,7 @@ package org.demoiselle.jee.security.exception; | @@ -8,7 +8,7 @@ package org.demoiselle.jee.security.exception; | ||
8 | 8 | ||
9 | import java.util.HashMap; | 9 | import java.util.HashMap; |
10 | 10 | ||
11 | -import org.demoiselle.jee.ws.jaxrs.exception.DemoiselleRESTException; | 11 | +import org.demoiselle.jee.rest.exception.DemoiselleRESTException; |
12 | 12 | ||
13 | public class DemoiselleSecurityException extends DemoiselleRESTException { | 13 | public class DemoiselleSecurityException extends DemoiselleRESTException { |
14 | 14 |