Skip to content

Commit

Permalink
The jsp2sful tests moved to Maven
Browse files Browse the repository at this point in the history
Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Nov 13, 2023
1 parent e71424f commit 788bdbf
Show file tree
Hide file tree
Showing 26 changed files with 747 additions and 675 deletions.
@@ -0,0 +1,125 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.main.itest.tools;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.glassfish.main.itest.tools.GlassFishTestEnvironment.openConnection;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.stringContainsInOrder;

/**
* HTTP client using the HTTP protocol and form based authentication.
*/
public class FormAuthHttpClient {

private final URL baseUrl;
private final String user;
private final String password;
private final CookieManager cookieManager;


/**
* Read remaining bytes in the response as a response body.
* The input stream will be closed then.
*
* @param connection
* @return String.
* @throws IOException
*/
public static String readResponseBody(final HttpURLConnection connection) throws IOException {
try (InputStream is = connection.getInputStream()) {
return new String(is.readAllBytes(), UTF_8);
}
}


/**
* @param baseUrl target endpoint.
* @param user
* @param password
*/
public FormAuthHttpClient(URL baseUrl, String user, String password) {
this.baseUrl = baseUrl;
this.user = user;
this.password = password;
this.cookieManager = new CookieManager();
CookieHandler.setDefault(cookieManager);
}


/**
* Does following steps:
* <ol>
* <li>HTTP GET to the protected relative path - expects HTTP 200 and login page - and also id
* of the new session.
* <li>HTTP POST to the <code>/j_security_check</code> with the j_username and j_password
* in the request body and JSESSIONID sent as a cookie. Expects the body for the original
* protected relative path.
* </ol>
*
* @param relativePath
* @return connection with headers and body which can be parsed.
* @throws IOException
* @throws ProtocolException
*/
public HttpURLConnection get(String relativePath) throws IOException {
final HttpCookie sessionId = createSession(relativePath);
final HttpURLConnection connection = openConnection(baseUrl.getPort(), baseUrl.getPath() + "/j_security_check");
connection.setRequestMethod("POST");
connection.setRequestProperty("Cookie", "JSESSIONID=" + URLEncoder.encode(sessionId.getValue(), UTF_8));
connection.setDoOutput(true);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Charset", UTF_8.name());
try (DataOutputStream stream = new DataOutputStream(connection.getOutputStream())) {
stream.writeBytes("j_username=" + user + "&j_password=" + password);
stream.flush();
}
return connection;
}


private HttpCookie createSession(String relativePath) throws IOException {
final HttpURLConnection connection = openConnection(baseUrl.getPort(), baseUrl.getPath() + "/" + relativePath);
connection.setRequestMethod("GET");
assertThat(connection.getResponseCode(), equalTo(200));
try (InputStream is = connection.getInputStream()) {
final String text = new String(is.readAllBytes(), UTF_8);
assertThat(text,
stringContainsInOrder("<title>Login Page</title>", "Please login", "j_username", "j_password"));
final HttpCookie sessionId = cookieManager.getCookieStore().getCookies().stream()
.filter(c -> c.getName().equals("JSESSIONID")).findFirst().get();
is.readAllBytes();
return sessionId;
} finally {
connection.disconnect();
}
}

}
16 changes: 11 additions & 5 deletions appserver/tests/application/pom.xml
Expand Up @@ -101,11 +101,6 @@
<artifactId>jakarta.authentication-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.wasp</groupId>
<artifactId>wasp</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>jakarta.jws</groupId>
<artifactId>jakarta.jws-api</artifactId>
Expand All @@ -123,6 +118,17 @@
<artifactId>jakarta.xml.ws-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.glassfish.wasp</groupId>
<artifactId>wasp</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.corba</groupId>
<artifactId>glassfish-corba-omgapi</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
Expand Down
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -14,56 +15,55 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package profile;
package org.glassfish.main.test.app.security.jsp2sful;

import jakarta.ejb.SessionContext;
import jakarta.ejb.SessionBean;
import java.util.Vector;
import java.lang.String;
import java.util.Iterator;
import jakarta.ejb.EJBException;
import java.rmi.RemoteException;
import jakarta.ejb.SessionContext;

/**
*
* @author hsingh
* @author hsingh
*/

public class ProfileInfoBean implements SessionBean {

private String name;
private static final long serialVersionUID = 1L;

private SessionContext sc = null;
private SessionContext context;

/** Creates a new instance of ProfieInfo */
public void ejbCreate(String name) {
this.name = name;
}


public String getCallerInfo() {
return sc.getCallerPrincipal().getName();
return context.getCallerPrincipal().getName();
}


public String getSecretInfo() {
return "Keep It Secret!";
}


@Override
public void ejbActivate() {
System.out.println("In ShoppingCart ejbActivate");
}


@Override
public void ejbPassivate() {
System.out.println("In ShoppingCart ejbPassivate");
}


public void ejbRemove() {
@Override
public void ejbRemove() {
System.out.println("In ShoppingCart ejbRemove");
}


@Override
public void setSessionContext(jakarta.ejb.SessionContext sessionContext) {
sc = sessionContext;
context = sessionContext;
}

}
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -14,16 +15,18 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package profile;
package org.glassfish.main.test.app.security.jsp2sful;

import jakarta.ejb.CreateException;
import jakarta.ejb.EJBHome;

import java.rmi.RemoteException;

/**
*
* @author swchan2
* @author swchan2
*/
public interface ProfileInfoHome extends EJBHome{
public interface ProfileInfoHome extends EJBHome {

public ProfileInfoRemote create(String name)
throws java.rmi.RemoteException, jakarta.ejb.CreateException;
ProfileInfoRemote create(String name) throws RemoteException, CreateException;

}
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation.
* Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -14,16 +15,19 @@
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package profile;
package org.glassfish.main.test.app.security.jsp2sful;

import java.rmi.RemoteException;
import jakarta.ejb.EJBObject;

/**
* ProfileInfo Stateful Session Bean. Test JSR 115 authorization.
* @author swchan2
*
* @author swchan2
*/
public interface ProfileInfoRemote extends EJBObject {
public String getCallerInfo() throws RemoteException;
public String getSecretInfo() throws RemoteException;

String getCallerInfo() throws RemoteException;

String getSecretInfo() throws RemoteException;
}
Expand Up @@ -2,6 +2,7 @@
<!DOCTYPE application PUBLIC "-//Sun Microsystems, Inc.//DTD J2EE Application 1.3//EN" "http://java.sun.com/dtd/application_1_3.dtd">
<!--
Copyright (c) 2023 Contributors to the Eclipse Foundation.
Copyright (c) 2017, 2018 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
Expand All @@ -15,18 +16,17 @@
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->

<application>
<display-name>jsp2sful</display-name>
<module>
<ejb>jsp2sful-ejb.jar</ejb>
</module>
<module>
<web>
<web-uri>jsp2sful-web.war</web-uri>
<display-name>jsp2sful</display-name>
<module>
<ejb>security-jsp2sful-ejb.jar</ejb>
</module>
<module>
<web>
<web-uri>security-jsp2sful-web.war</web-uri>
<context-root>jsp2sful</context-root>
</web>
</module>
</web>
</module>
</application>

0 comments on commit 788bdbf

Please sign in to comment.