24
24
package password .pwm .resttest ;
25
25
26
26
import com .google .gson .Gson ;
27
+ import com .google .gson .JsonObject ;
28
+ import com .google .gson .JsonParser ;
27
29
import org .apache .commons .io .IOUtils ;
28
30
29
31
import javax .servlet .ServletException ;
34
36
import java .io .IOException ;
35
37
import java .io .InputStream ;
36
38
import java .io .PrintWriter ;
37
- import java .time .Instant ;
38
39
39
40
@ WebServlet (
40
41
name = "NewUserServlet" ,
41
- urlPatterns = { "/sms" , "/macro" }
42
+ urlPatterns = { "/sms" , "/macro" , "/external-token-destination" , "/external-password-check" }
42
43
)
43
44
44
45
public class ExternalMacroServlet extends HttpServlet
45
46
{
46
47
private static final String USERNAME_PARAMETER = "username" ;
47
48
private static final String SUCCESSFUL = "true" ;
48
49
private static final String UNSUCCESSFUL = "false" ;
50
+ private static final String SMS_URL = "/sms" ;
51
+ private static final String MACRO_URL = "/macro" ;
52
+ private static final String EXTERNAL_TOKEN_DESTINATION_URL = "/external-token-destination" ;
53
+ private static final String EXTERNAL_PASSWORD_CHECK_URL = "/external-password-check" ;
49
54
50
55
@ Override
51
56
protected void doPost ( final HttpServletRequest req , final HttpServletResponse resp ) throws ServletException , IOException
52
57
{
53
- if ( req .getServletPath ().equals ( "/sms" ) )
58
+ if ( req .getServletPath ().equals ( SMS_URL ) )
54
59
{
55
60
final SmsResponse instance = SmsResponse .getInstance ();
56
61
final InputStream inputStream = req .getInputStream ();
@@ -59,8 +64,7 @@ protected void doPost( final HttpServletRequest req, final HttpServletResponse r
59
64
final String [] messageContent = body .split ( "=" );
60
65
final String message = messageContent [messageContent .length - 1 ];
61
66
final String username = message .split ( "\\ +" )[0 ];
62
- final Instant currentDate = Instant .now ();
63
- final SmsPostResponseBody messageBody = new SmsPostResponseBody ( message , currentDate );
67
+ final SmsPostResponseBody messageBody = new SmsPostResponseBody ( message );
64
68
65
69
instance .addToMap ( username , messageBody );
66
70
@@ -72,12 +76,41 @@ protected void doPost( final HttpServletRequest req, final HttpServletResponse r
72
76
writer .write ( "{\" output\" :\" Message Received\" }" );
73
77
writer .close ();
74
78
}
79
+ else if ( req .getServletPath ().equals ( EXTERNAL_TOKEN_DESTINATION_URL ) )
80
+ {
81
+ System .out .println ( "External Token Destination" );
82
+ final InputStream inputStream = req .getInputStream ();
83
+ final String body = IOUtils .toString ( inputStream );
84
+ final JsonObject jsonObject = new JsonParser ().parse ( body ).getAsJsonObject ();
85
+ final String email = jsonObject .getAsJsonObject ( "tokenDestination" ).get ( "email" ).getAsString ();
86
+ final String sms = jsonObject .getAsJsonObject ( "tokenDestination" ).get ( "sms" ).getAsString ();
87
+ final String displayValue = "YourTokenDestination" ;
88
+
89
+ resp .setHeader ( "Content-Type" , "application/json" );
90
+
91
+ final PrintWriter writer = resp .getWriter ();
92
+ final String response = "{\" email\" :\" " + email + "\" ,\" sms\" :\" " + sms + "\" ,\" displayValue\" :\" " + displayValue + "\" }" ;
93
+ writer .write ( response );
94
+ writer .close ();
95
+ }
96
+ else if ( req .getServletPath ().equals ( EXTERNAL_PASSWORD_CHECK_URL ) )
97
+ {
98
+ System .out .println ( "External Password Check" );
99
+ final boolean error = false ;
100
+ final String errorMessage = "No error." ;
101
+ resp .setHeader ( "Content-Type" , "application/json" );
102
+
103
+ final PrintWriter writer = resp .getWriter ();
104
+ final String response = "{\" error\" :\" " + error + "\" ,\" errorMessage\" :\" " + errorMessage + "\" }" ;
105
+ writer .write ( response );
106
+ writer .close ();
107
+ }
75
108
}
76
109
77
110
@ Override
78
111
protected void doGet ( final HttpServletRequest req , final HttpServletResponse resp ) throws IOException
79
112
{
80
- if ( req .getServletPath ().equals ( "/sms" ) )
113
+ if ( req .getServletPath ().equals ( SMS_URL ) )
81
114
{
82
115
//Check request
83
116
final SmsResponse instance = SmsResponse .getInstance ();
0 commit comments