Skip to content

Commit 34396c3

Browse files
committed
rest-test enhancements
1 parent b1d4531 commit 34396c3

File tree

5 files changed

+228
-46
lines changed

5 files changed

+228
-46
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Password Management Servlets (PWM)
3+
* http://www.pwm-project.org
4+
*
5+
* Copyright (c) 2006-2009 Novell, Inc.
6+
* Copyright (c) 2009-2018 The PWM Project
7+
*
8+
* This program is free software; you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation; either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program; if not, write to the Free Software
20+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21+
*/
22+
23+
24+
package password.pwm.resttest;
25+
26+
import javax.servlet.annotation.WebServlet;
27+
import javax.servlet.http.HttpServlet;
28+
29+
@WebServlet(
30+
name = "ExternalMacroTest",
31+
urlPatterns = { "/macro", }
32+
)
33+
public class RestTestExternalMacroServlet extends HttpServlet
34+
{
35+
36+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Password Management Servlets (PWM)
3+
* http://www.pwm-project.org
4+
*
5+
* Copyright (c) 2006-2009 Novell, Inc.
6+
* Copyright (c) 2009-2018 The PWM Project
7+
*
8+
* This program is free software; you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation; either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program; if not, write to the Free Software
20+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21+
*/
22+
23+
package password.pwm.resttest;
24+
25+
import com.google.gson.JsonObject;
26+
import com.google.gson.JsonParser;
27+
import org.apache.commons.io.IOUtils;
28+
29+
import javax.servlet.ServletException;
30+
import javax.servlet.annotation.WebServlet;
31+
import javax.servlet.http.HttpServlet;
32+
import javax.servlet.http.HttpServletRequest;
33+
import javax.servlet.http.HttpServletResponse;
34+
import java.io.IOException;
35+
import java.io.InputStream;
36+
import java.io.PrintWriter;
37+
38+
@WebServlet(
39+
name = "RestTestExternalTokenDestinationServlet",
40+
urlPatterns = { "/external-token-destination", }
41+
)
42+
public class RestTestExternalTokenDestinationServlet extends HttpServlet
43+
{
44+
45+
@Override
46+
protected void doPost( final HttpServletRequest req, final HttpServletResponse resp )
47+
throws ServletException, IOException
48+
{
49+
System.out.println( "--External Token Destination--" );
50+
final InputStream inputStream = req.getInputStream();
51+
final String body = IOUtils.toString( inputStream );
52+
final JsonObject jsonObject = new JsonParser().parse( body ).getAsJsonObject();
53+
final String email = jsonObject.getAsJsonObject( "tokenDestination" ).get( "email" ).getAsString();
54+
final String sms = jsonObject.getAsJsonObject( "tokenDestination" ).get( "sms" ).getAsString();
55+
final String displayValue = "YourTokenDestination";
56+
57+
resp.setHeader( "Content-Type", "application/json" );
58+
59+
final PrintWriter writer = resp.getWriter();
60+
final String response = "{\"email\":\"" + email + "\",\"sms\":\"" + sms + "\",\"displayValue\":\"" + displayValue + "\"}";
61+
writer.write( response );
62+
writer.close();
63+
}
64+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Password Management Servlets (PWM)
3+
* http://www.pwm-project.org
4+
*
5+
* Copyright (c) 2006-2009 Novell, Inc.
6+
* Copyright (c) 2009-2018 The PWM Project
7+
*
8+
* This program is free software; you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation; either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program; if not, write to the Free Software
20+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21+
*/
22+
23+
package password.pwm.resttest;
24+
25+
import com.google.gson.Gson;
26+
import com.google.gson.reflect.TypeToken;
27+
28+
import javax.servlet.ServletException;
29+
import javax.servlet.annotation.WebServlet;
30+
import javax.servlet.http.HttpServlet;
31+
import javax.servlet.http.HttpServletRequest;
32+
import javax.servlet.http.HttpServletResponse;
33+
import java.io.IOException;
34+
import java.io.PrintWriter;
35+
import java.util.Map;
36+
37+
@WebServlet(
38+
name = "RestTestPasswordCheckServlet",
39+
urlPatterns = { "/external-password-check" }
40+
)
41+
public class RestTestPasswordCheckServlet extends HttpServlet
42+
{
43+
44+
@Override
45+
protected void doPost( final HttpServletRequest req, final HttpServletResponse resp ) throws ServletException, IOException
46+
{
47+
System.out.println( "REST TEST: --External Password Check--" );
48+
final Gson gson = new Gson();
49+
final Map<String, String> inputJson = gson.fromJson( RestTestUtilities.readRequestBodyAsString( req ), new TypeToken<Map<String, Object>>()
50+
{
51+
}.getType() );
52+
final String inputPassword = inputJson.get( "password" );
53+
final boolean error = inputPassword.contains( "aaa" );
54+
55+
final String errorMessage = error ? "TOO Many aaa's (REMOTE REST SERVICE)" : "No error. (REMOTE REST SERVICE)";
56+
57+
resp.setHeader( "Content-Type", "application/json" );
58+
final PrintWriter writer = resp.getWriter();
59+
final String response = "{\"error\":\"" + error + "\",\"errorMessage\":\"" + errorMessage + "\"}";
60+
writer.write( response );
61+
writer.close();
62+
}
63+
}

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

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,9 @@
2020
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2121
*/
2222

23-
2423
package password.pwm.resttest;
2524

2625
import com.google.gson.Gson;
27-
import com.google.gson.JsonObject;
28-
import com.google.gson.JsonParser;
2926
import org.apache.commons.io.IOUtils;
3027

3128
import javax.servlet.ServletException;
@@ -38,25 +35,18 @@
3835
import java.io.PrintWriter;
3936

4037
@WebServlet(
41-
name = "NewUserServlet",
42-
urlPatterns = { "/sms", "/macro", "/external-token-destination", "/external-password-check" }
38+
name = "RestTestSmsGatewayServlet",
39+
urlPatterns = { "/sms" }
4340
)
44-
45-
public class ExternalMacroServlet extends HttpServlet
41+
public class RestTestSmsGatewayServlet extends HttpServlet
4642
{
4743
private static final String USERNAME_PARAMETER = "username";
4844
private static final String SUCCESSFUL = "true";
4945
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";
5446

5547
@Override
5648
protected void doPost( final HttpServletRequest req, final HttpServletResponse resp ) throws ServletException, IOException
5749
{
58-
if ( req.getServletPath().equals( SMS_URL ) )
59-
{
6050
final SmsResponse instance = SmsResponse.getInstance();
6151
final InputStream inputStream = req.getInputStream();
6252
final String body = IOUtils.toString( inputStream );
@@ -75,43 +65,11 @@ protected void doPost( final HttpServletRequest req, final HttpServletResponse r
7565
final PrintWriter writer = resp.getWriter();
7666
writer.write( "{\"output\":\"Message Received\"}" );
7767
writer.close();
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-
}
10868
}
10969

11070
@Override
11171
protected void doGet( final HttpServletRequest req, final HttpServletResponse resp ) throws IOException
11272
{
113-
if ( req.getServletPath().equals( SMS_URL ) )
114-
{
11573
//Check request
11674
final SmsResponse instance = SmsResponse.getInstance();
11775
final String requestUsername = req.getParameter( USERNAME_PARAMETER );
@@ -136,5 +94,5 @@ protected void doGet( final HttpServletRequest req, final HttpServletResponse re
13694
writer.close();
13795

13896
}
139-
}
97+
14098
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Password Management Servlets (PWM)
3+
* http://www.pwm-project.org
4+
*
5+
* Copyright (c) 2006-2009 Novell, Inc.
6+
* Copyright (c) 2009-2018 The PWM Project
7+
*
8+
* This program is free software; you can redistribute it and/or modify
9+
* it under the terms of the GNU General Public License as published by
10+
* the Free Software Foundation; either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with this program; if not, write to the Free Software
20+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21+
*/
22+
23+
package password.pwm.resttest;
24+
25+
import org.apache.commons.io.IOUtils;
26+
27+
import javax.servlet.http.HttpServletRequest;
28+
import java.io.IOException;
29+
import java.io.InputStreamReader;
30+
import java.io.Reader;
31+
import java.io.StringWriter;
32+
import java.nio.charset.Charset;
33+
34+
public class RestTestUtilities
35+
{
36+
public static String readRequestBodyAsString( final HttpServletRequest req )
37+
throws IOException
38+
{
39+
final StringWriter stringWriter = new StringWriter();
40+
final Reader readerStream = new InputStreamReader(
41+
req.getInputStream(),
42+
Charset.forName( "UTF8" )
43+
);
44+
45+
try
46+
{
47+
IOUtils.copy( readerStream, stringWriter );
48+
}
49+
catch ( Exception e )
50+
{
51+
final String errorMsg = "error reading request body stream: " + e.getMessage();
52+
throw new IOException( errorMsg );
53+
}
54+
finally
55+
{
56+
IOUtils.closeQuietly( readerStream );
57+
}
58+
59+
return stringWriter.toString();
60+
}
61+
}

0 commit comments

Comments
 (0)