Skip to content

Commit c79be68

Browse files
authored
Merge pull request #475 from joshzcold/master
Update changes to rest test serverlet. merge most recent pwm changes
2 parents b0a22d7 + b61d274 commit c79be68

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

rest-test-service/src/main/java/password/pwm/resttest/ExternalMacroServlet.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
package password.pwm.resttest;
2525

2626
import com.google.gson.Gson;
27+
import com.google.gson.JsonObject;
28+
import com.google.gson.JsonParser;
2729
import org.apache.commons.io.IOUtils;
2830

2931
import javax.servlet.ServletException;
@@ -34,23 +36,26 @@
3436
import java.io.IOException;
3537
import java.io.InputStream;
3638
import java.io.PrintWriter;
37-
import java.time.Instant;
3839

3940
@WebServlet(
4041
name = "NewUserServlet",
41-
urlPatterns = { "/sms", "/macro" }
42+
urlPatterns = { "/sms", "/macro", "/external-token-destination", "/external-password-check" }
4243
)
4344

4445
public class ExternalMacroServlet extends HttpServlet
4546
{
4647
private static final String USERNAME_PARAMETER = "username";
4748
private static final String SUCCESSFUL = "true";
4849
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";
4954

5055
@Override
5156
protected void doPost( final HttpServletRequest req, final HttpServletResponse resp ) throws ServletException, IOException
5257
{
53-
if ( req.getServletPath().equals( "/sms" ) )
58+
if ( req.getServletPath().equals( SMS_URL ) )
5459
{
5560
final SmsResponse instance = SmsResponse.getInstance();
5661
final InputStream inputStream = req.getInputStream();
@@ -59,8 +64,7 @@ protected void doPost( final HttpServletRequest req, final HttpServletResponse r
5964
final String[] messageContent = body.split( "=" );
6065
final String message = messageContent[messageContent.length - 1];
6166
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 );
6468

6569
instance.addToMap( username, messageBody );
6670

@@ -72,12 +76,41 @@ protected void doPost( final HttpServletRequest req, final HttpServletResponse r
7276
writer.write( "{\"output\":\"Message Received\"}" );
7377
writer.close();
7478
}
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+
}
75108
}
76109

77110
@Override
78111
protected void doGet( final HttpServletRequest req, final HttpServletResponse resp ) throws IOException
79112
{
80-
if ( req.getServletPath().equals( "/sms" ) )
113+
if ( req.getServletPath().equals( SMS_URL ) )
81114
{
82115
//Check request
83116
final SmsResponse instance = SmsResponse.getInstance();

rest-test-service/src/main/java/password/pwm/resttest/SmsPostResponseBody.java

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,49 +27,37 @@
2727
public class SmsPostResponseBody
2828
{
2929
private String messageContent;
30-
private Instant date;
31-
32-
public SmsPostResponseBody( final String message, final Instant date )
33-
{
34-
final String[] strings = message.split( "&" );
35-
this.messageContent = strings[strings.length - 1];
36-
this.date = date;
37-
}
30+
// private Instant date;
3831

3932
public SmsPostResponseBody( final String message )
4033
{
4134
final String[] strings = message.split( "&" );
4235
this.messageContent = strings[strings.length - 1];
4336
}
4437

45-
public SmsPostResponseBody( final Instant date )
46-
{
47-
this.date = date;
48-
this.messageContent = "";
49-
}
50-
5138
public SmsPostResponseBody()
5239
{
5340

5441
}
5542

43+
5644
public String getMessageContent()
5745
{
5846
return messageContent;
5947
}
6048

61-
public void setMessageContent( final String messageContent )
62-
{
63-
this.messageContent = messageContent;
64-
}
49+
// public void setMessageContent( final String messageContent )
50+
// {
51+
// this.messageContent = messageContent;
52+
// }
6553

6654
public Instant getDate()
6755
{
68-
return date;
56+
return Instant.now();
6957
}
7058

71-
public void setDate( final Instant date )
72-
{
73-
this.date = date;
74-
}
59+
// public void setDate( final Instant date )
60+
// {
61+
// this.date = Instant.now();
62+
// }
7563
}

0 commit comments

Comments
 (0)