Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 02186b90

Přidáno uživatelem Pavel Fidranský před více než 5 roky(ů)

use commons-validator to validate email address

Zobrazit rozdíly:

sources/pom.xml
72 72
            <artifactId>commons-lang3</artifactId>
73 73
            <version>3.8.1</version>
74 74
        </dependency>
75
        <dependency>
76
            <groupId>commons-validator</groupId>
77
            <artifactId>commons-validator</artifactId>
78
            <version>1.6</version>
79
        </dependency>
75 80

  
76 81
        <!-- Servlets and JSP -->
77 82
        <dependency>
sources/src/main/java/cz/zcu/kiv/offscreen/servlets/api/Register.java
5 5
import cz.zcu.kiv.offscreen.user.DB;
6 6
import cz.zcu.kiv.offscreen.user.User;
7 7
import org.apache.commons.lang3.StringUtils;
8
import org.apache.commons.validator.routines.EmailValidator;
8 9
import org.apache.logging.log4j.LogManager;
9 10
import org.apache.logging.log4j.Logger;
10 11

  
11

  
12 12
import javax.servlet.http.HttpServletRequest;
13 13
import javax.servlet.http.HttpServletResponse;
14 14
import java.io.IOException;
15 15
import java.util.HashMap;
16 16
import java.util.Map;
17
import java.util.regex.Matcher;
18
import java.util.regex.Pattern;
19 17

  
20 18
public class Register extends BaseServlet {
19

  
21 20
	private static final Logger logger = LogManager.getLogger();
22
	private static final String EMAIL_ADDRESS_PATTERN = "^[A-Za-z0-9-\\+_]+(\\.[A-Za-z0-9-_]+)*@" +
23
    											"[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*" +
24
    											"(\\.[A-Za-z]{2,})$";
25
    
21

  
26 22
    @Override
27 23
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
28 24
		logger.debug("Processing request");
25

  
29 26
		String name = request.getParameter("name");
30 27
		String email = request.getParameter("email");
31 28
		String username = request.getParameter("username");
......
46 43
			errors.addProperty("email", "Please enter e-mail address.");
47 44
			logger.debug("Empty e-mail address");
48 45

  
49
		} else if (!isEmailAddressValid(email)) {
46
		} else if (!EmailValidator.getInstance().isValid(email)) {
50 47
			errors.addProperty("email", "Please enter valid e-mail address.");
51 48
			logger.debug("Invalid e-mail address");
52 49

  
......
101 98
			logger.debug("Response BAD REQUEST");
102 99
		}
103 100
    }
104
    
105
    /**
106
     * Method verify validity of email address
107
     * 
108
     * @param emailAddress - email address
109
     * @return true - email address is ok
110
     * 		   false - email address is not correct
111
     */
112
    private boolean isEmailAddressValid(String emailAddress) {
113
    	Pattern pattern = Pattern.compile(EMAIL_ADDRESS_PATTERN);
114
    	Matcher matcher = pattern.matcher(emailAddress);
115
		return matcher.matches();
116
    }
117 101
	
118 102
}

Také k dispozici: Unified diff