-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit ed80170
Showing
20 changed files
with
625 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
target | ||
*.class | ||
.classpath | ||
.project | ||
.settings | ||
.idea | ||
*.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# Vraptor message handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<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> | ||
|
||
<groupId>br.com.caelum.vraptor</groupId> | ||
<artifactId>vraptor-i18n</artifactId> | ||
<version>4.2.0-RC5-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
<name>vraptor-i18n</name> | ||
<url>http://maven.apache.org</url> | ||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<weld.version>2.1.2.Final</weld.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>br.com.caelum</groupId> | ||
<artifactId>vraptor</artifactId> | ||
<version>4.2.0-RC3</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jboss.weld.servlet</groupId> | ||
<artifactId>weld-servlet-core</artifactId> | ||
<version>${weld.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.jboss.weld</groupId> | ||
<artifactId>weld-core-impl</artifactId> | ||
<version>${weld.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.inject</groupId> | ||
<artifactId>javax.inject</artifactId> | ||
<version>1</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.8.2</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mockito</groupId> | ||
<artifactId>mockito-core</artifactId> | ||
<version>1.8.5</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.mortbay.jetty</groupId> | ||
<artifactId>servlet-api</artifactId> | ||
<version>3.0.20100224</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>joda-time</groupId> | ||
<artifactId>joda-time</artifactId> | ||
<version>1.6</version> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.servlet.jsp.jstl</groupId> | ||
<artifactId>jstl-api</artifactId> | ||
<version>1.2</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>2.3.2</version> | ||
<configuration> | ||
<source>1.7</source> | ||
<target>1.7</target> | ||
<encoding>UTF-8</encoding> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.urutau.vraptor; | ||
|
||
public enum Category { | ||
DEFAULT, | ||
PRIMARY, | ||
SUCCESS, | ||
INFO, | ||
WARNING, | ||
DANGER; | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/urutau/vraptor/handler/ErrorMessageHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.urutau.vraptor.handler; | ||
|
||
import com.urutau.vraptor.Category; | ||
|
||
import br.com.caelum.vraptor.validator.Validator; | ||
|
||
public interface ErrorMessageHandler { | ||
|
||
/** | ||
* Sets the category that will be show | ||
* | ||
* @param category {@link Category} Object with all contexts of system | ||
*/ | ||
Judge use(String category); | ||
|
||
/** | ||
* returns {@link Validator#hasErrors()} | ||
*/ | ||
boolean hasErrors(); | ||
|
||
/** | ||
* @return {@link Validator#getErrors().size()} | ||
*/ | ||
int errorsCount(); | ||
} |
50 changes: 50 additions & 0 deletions
50
src/main/java/com/urutau/vraptor/handler/ErrorMessageObserver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.urutau.vraptor.handler; | ||
|
||
import javax.enterprise.context.RequestScoped; | ||
import javax.enterprise.event.Observes; | ||
import javax.inject.Inject; | ||
|
||
import com.urutau.vraptor.handler.qualifier.Category; | ||
import com.urutau.vraptor.handler.qualifier.Condition; | ||
import com.urutau.vraptor.handler.qualifier.Message; | ||
import com.urutau.vraptor.i18n.I18nMessageCreator; | ||
|
||
import br.com.caelum.vraptor.validator.I18nMessage; | ||
import br.com.caelum.vraptor.validator.Validator; | ||
|
||
/** | ||
* Chain sequence would be: use(category).when(condition).toShow(message) | ||
* .(redirectingTo(XptoController).hello()); // optional | ||
*/ | ||
@RequestScoped | ||
public class ErrorMessageObserver { | ||
|
||
private final I18nMessageCreator i18nCreator; | ||
private final Validator validator; | ||
|
||
public ErrorMessageObserver() { | ||
this(null, null); | ||
} | ||
|
||
@Inject | ||
public ErrorMessageObserver(I18nMessageCreator i18nCreator, Validator validator) { | ||
this.i18nCreator = i18nCreator; | ||
this.validator = validator; | ||
} | ||
|
||
private String category; | ||
private boolean condition; | ||
|
||
public void setCategory(@Observes @Category String category) { | ||
this.category = category; | ||
} | ||
|
||
public void setCondition(@Observes @Condition Boolean condition) { | ||
this.condition = condition; | ||
} | ||
|
||
public void setMessage(@Observes @Message String message) { | ||
I18nMessage translatedMessage = i18nCreator.translate(message).to(category); | ||
validator.addIf(condition, translatedMessage); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.urutau.vraptor.handler; | ||
|
||
public interface Judge { | ||
/** | ||
* Sets a condition to throws an specific error | ||
* | ||
* @param condition true to message be showed | ||
* @return {@link Screened} to writes a message | ||
*/ | ||
Screened when(Boolean condition); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.urutau.vraptor.handler; | ||
|
||
public interface MessageHandler { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.urutau.vraptor.handler; | ||
|
||
import br.com.caelum.vraptor.validator.Validator; | ||
import br.com.caelum.vraptor.view.Results; | ||
|
||
public interface Redirector { | ||
/** | ||
* Uses {@link Validator#onErrorRedirectTo(Class)} to redirects to another action | ||
* | ||
* @param controller contains action that will be called | ||
* @return {@link br.com.caelum.vraptor.Controller} to calls some action | ||
*/ | ||
<Controller> Controller redirectingTo(Class<Controller> controller); | ||
|
||
/** | ||
* Uses {@link Validator#onErrorUse(Class)} to send a JSON format | ||
* that contains all errors added. | ||
*/ | ||
void sendJSON(); | ||
|
||
/** | ||
* Uses {@link Validator#onErrorUsePageOf(Object)} to redirects to certain page | ||
* | ||
* @param controller contains page references | ||
* @return {@link br.com.caelum.vraptor.Controller} to calls some page | ||
*/ | ||
<Controller> Controller onErrorUsePageOf(Class<Controller> controller); | ||
|
||
/** | ||
* Stay on the current page | ||
* | ||
* @see {@link Results#referer()} | ||
*/ | ||
void stayInCurrent(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.urutau.vraptor.handler; | ||
|
||
public interface Screened { | ||
/** | ||
* Writes the user message after invoke a {@link #when(Boolean)} method | ||
* | ||
* @param message Error message | ||
*/ | ||
public Redirector toShow(String message); | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/com/urutau/vraptor/handler/impl/DefaultErrorMessageHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.urutau.vraptor.handler.impl; | ||
|
||
import javax.enterprise.context.RequestScoped; | ||
import javax.enterprise.event.Event; | ||
import javax.inject.Inject; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.urutau.vraptor.handler.ErrorMessageHandler; | ||
import com.urutau.vraptor.handler.Judge; | ||
import com.urutau.vraptor.handler.qualifier.Category; | ||
|
||
import br.com.caelum.vraptor.validator.Validator; | ||
|
||
@RequestScoped | ||
public class DefaultErrorMessageHandler implements ErrorMessageHandler { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DefaultErrorMessageHandler.class); | ||
|
||
private final Validator validator; | ||
private final Judge judge; | ||
|
||
@Category | ||
private final Event<String> eventCategory; | ||
|
||
/** | ||
* @deprecated CDI eyes only | ||
*/ | ||
public DefaultErrorMessageHandler() { | ||
this(null, null, null); | ||
} | ||
|
||
@Inject | ||
public DefaultErrorMessageHandler(Validator validator, Judge judge, | ||
Event<String> event) { | ||
this.validator = validator; | ||
this.judge = judge; | ||
this.eventCategory = event; | ||
} | ||
|
||
public Judge use(String category) { | ||
logger.info("Select category " + category); | ||
eventCategory.fire(category); | ||
return judge; | ||
} | ||
|
||
public boolean hasErrors() { | ||
return validator.hasErrors(); | ||
} | ||
|
||
public int errorsCount() { | ||
return validator.getErrors().size(); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/main/java/com/urutau/vraptor/handler/impl/DefaultJudge.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.urutau.vraptor.handler.impl; | ||
|
||
import javax.enterprise.context.RequestScoped; | ||
import javax.enterprise.event.Event; | ||
import javax.inject.Inject; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.urutau.vraptor.handler.Judge; | ||
import com.urutau.vraptor.handler.Screened; | ||
import com.urutau.vraptor.handler.qualifier.Condition; | ||
|
||
@RequestScoped | ||
public class DefaultJudge implements Judge { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(DefaultJudge.class); | ||
|
||
private final Screened screened; | ||
|
||
@Condition | ||
private final Event<Boolean> eventCondition; | ||
|
||
/** | ||
* @deprecated CDI eyes only | ||
*/ | ||
public DefaultJudge() { | ||
this(null, null); | ||
} | ||
|
||
@Inject | ||
public DefaultJudge(Screened screened, Event<Boolean> event) { | ||
this.screened = screened; | ||
this.eventCondition = event; | ||
} | ||
|
||
@Override | ||
public Screened when(Boolean conditionToShow) { | ||
logger.info("Show message is " + conditionToShow); | ||
eventCondition.fire(conditionToShow); | ||
return screened; | ||
} | ||
|
||
} |
Oops, something went wrong.