Skip to content

Commit

Permalink
[WFLY-8561] fixing test for @Runas functionality which was ported fro…
Browse files Browse the repository at this point in the history
…m eap5 testsuite (JBPAPP-7897)
  • Loading branch information
ochaloup committed Apr 13, 2017
1 parent 66bb9e7 commit 02ae007
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 307 deletions.
Expand Up @@ -29,5 +29,5 @@
* @author Ondrej Chaloupka
*/
public interface GoodBye extends EJBObject {
String sayGoodBye(String userID) throws RemoteException;
String sayGoodBye() throws RemoteException;
}
Expand Up @@ -25,29 +25,22 @@
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;

import org.jboss.logging.Logger;

/**
* Simple bean which returns goodbye greeting.
* Bean returning goodbye greeting.
*
* @author Ondrej Chaloupka
*/
public class GoodByeBean implements SessionBean {
private static final long serialVersionUID = 1L;
private SessionContext sessionContext;
private static final Logger log = Logger.getLogger(GoodByeBean.class);
public static final String SAYING = "GoodBye";

public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
public String sayGoodBye() {
return SAYING;
}

public String sayGoodBye(String userID) {
String greeting = "GoodBye " + userID;
log.trace("Inside GoodByeBean.sayGoodBye(). Greeting: " + greeting);
return greeting;
public void setSessionContext(SessionContext sessionContext) {
}

// Methods typically ignored.
public void ejbCreate() {
}

Expand Down
Expand Up @@ -29,5 +29,5 @@
* @author Ondrej Chaloupka
*/
public interface GoodByeLocal extends EJBLocalObject {
String sayGoodBye(String userID) throws EJBException;
String sayGoodBye() throws EJBException;
}
Expand Up @@ -23,7 +23,6 @@
package org.jboss.as.test.integration.ejb.security.runas.ejb2mdb;

import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.Remote;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
Expand All @@ -38,97 +37,81 @@
import javax.jms.Session;
import javax.jms.TemporaryQueue;
import javax.jms.TextMessage;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.jboss.as.test.shared.TimeoutUtil;
import org.jboss.logging.Logger;

// Security related imports

/**
* Bean passes message to HelloMDB bean and checks the reply queue. The HellpMDB bean calls this one for getting hello greeting
* for JBossAdmin role.
* Bean passes message to HelloMDB bean and checks the reply queue.
*
* @author Ondrej Chaloupka
*/
@Stateless(name = "Hello")
@RolesAllowed({})
@Remote(Hello.class)
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class HelloBean implements Hello {
private static final Logger log = Logger.getLogger(HelloBean.class);

public static final String SAYING = "Hello";
public static final String QUEUE_NAME = "queue/TestQueue";
public static final String QUEUE_NAME_JNDI = "java:jboss/exported/" + QUEUE_NAME;

@Resource
private SessionContext context;

@RolesAllowed("JBossAdmin")
public String sayHello() throws Exception {
log.debug("session context: " + context);
log.debug("caller name: " + context.getCallerPrincipal().getName());
InitialContext initialContext = new InitialContext();

if (context.isCallerInRole("JBossAdmin")) {
throw new IllegalArgumentException("User is in role!!");
try {
ConnectionFactory cf = (ConnectionFactory) initialContext.lookup("java:/ConnectionFactory");
String replyMessage = HelloBean.sendMessage(cf);
return String.format("%s %s, %s", SAYING, getName(), replyMessage);
} finally {
initialContext.close();
}

log.trace("HelloBean - sending message");
String msg = this.sendMessage();

String name = getName();
return "Hello " + name + "! " + msg;
}

private String getName() {
return "Fred";
}

public String sendMessage() throws Exception {
String destinationName = "java:jboss/exported/queue/TestQueue";
Context ic = null;
ConnectionFactory cf = null;
/**
* Helper method to send message to {@link #QUEUE_NAME}.
*/
public static String sendMessage(ConnectionFactory cf) throws Exception {
Connection connection = null;

try {
ic = getInitialContext();
cf = (ConnectionFactory) ic.lookup("java:/ConnectionFactory");
Queue queue = (Queue) ic.lookup(destinationName);
Queue queue = cf.createContext("guest", "guest").createQueue(QUEUE_NAME);
connection = cf.createConnection("guest", "guest");
connection.start(); // we need to start connection for consumer

Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
MessageProducer sender = session.createProducer(queue);
TextMessage message = session.createTextMessage("hello goodbye");
TemporaryQueue replyQueue = session.createTemporaryQueue();
message.setJMSReplyTo(replyQueue);
sender.send(message);
log.tracef("Message '%s' sent", message);

// consume message processed by MDB
MessageConsumer consumer = session.createConsumer(replyQueue);
TextMessage replyMsg = (TextMessage) consumer.receive(5000);
log.trace("Message received:" + replyMsg);
return replyMsg.getText();
TextMessage replyMessage = (TextMessage) consumer.receive(TimeoutUtil.adjust(5000));
log.tracef("Message '%s' received", replyMessage);

if(replyMessage == null) {
return "ReplyMessage is 'null'. No response received from HelloMDB."
+ " Consult server log for details.";
}
return replyMessage.getText();
} finally {
if (ic != null) {
try {
ic.close();
} catch (Exception ignore) {
}
try {
if (connection != null) connection.close();
} catch (JMSException jmse) {
log.trace("connection close failed", jmse);
}
closeConnection(connection);
}

}

public static InitialContext getInitialContext() throws NamingException {
return new InitialContext();
}

private void closeConnection(Connection conn) {
try {
if (conn != null) {
conn.close();
}
} catch (JMSException jmse) {
log.trace("connection close failed: " + jmse);
}
private String getName() {
return context.getCallerPrincipal().getName();
}

}
Expand Up @@ -24,6 +24,7 @@

import javax.annotation.Resource;
import javax.annotation.security.RunAs;
import javax.annotation.security.PermitAll;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.EJB;
import javax.ejb.MessageDriven;
Expand Down Expand Up @@ -51,6 +52,7 @@
@ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(propertyName = "maxSession", propertyValue = "1")})
@RunAs("INTERNAL_ROLE")
@PermitAll // needed for access not being denied, see WFLY-8560
public class HelloMDB implements MessageListener {
private static final Logger log = Logger.getLogger(HowdyBean.class);

Expand All @@ -62,26 +64,18 @@ public class HelloMDB implements MessageListener {

@Resource(lookup = "java:global/runasmdbejb-ejb2/GoodBye!org.jboss.as.test.integration.ejb.security.runas.ejb2mdb.GoodByeLocalHome")
GoodByeLocalHome goodByeHome;
GoodByeLocal goodBye;

public void onMessage(Message message) {
try {
log.debug("[HelloMDB] calling goodByeHome.create()");
goodBye = goodByeHome.create();
log.debug("[HelloMDB] returned from goodByeHome.create()");
String replyMsg = this.callEJB() + goodBye.sayGoodBye("user1");
log.trace("[HelloMDB] calling sayHowdy: " + replyMsg);
GoodByeLocal goodBye = goodByeHome.create();
String messageToReply = String.format("%s! %s.", howdy.sayHowdy(), goodBye.sayGoodBye());

sendReply(replyMsg, (Queue) message.getJMSReplyTo(), message.getJMSMessageID());
sendReply(messageToReply, (Queue) message.getJMSReplyTo(), message.getJMSMessageID());
} catch (Exception e) {
log.error("EXCEPTION: ", e);
log.errorf(e, "Can't process message '%s'", message);
}
}

private String callEJB() {
return howdy.sayHowdy();
}

private void sendReply(String msg, Queue destination, String messageID) throws JMSException {
QueueConnection conn = qFactory.createQueueConnection("guest", "guest");
try {
Expand Down
Expand Up @@ -30,10 +30,6 @@
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import org.jboss.logging.Logger;

// Security related imports

/**
* Returns hola greeting for INTERNAL_ROLE.
*
Expand All @@ -44,25 +40,18 @@
@Remote(Hola.class)
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class HolaBean implements Hola {
private static final Logger log = Logger.getLogger(HowdyBean.class);
public static final String SAYING = "Hola";

@Resource
private SessionContext context;

@RolesAllowed("INTERNAL_ROLE")
public String sayHola() {
log.trace("HolaBean.sayHola(). Caller name: " + context.getCallerPrincipal().getName());

if (context.isCallerInRole("JBossAdmin")) {
log.trace("User is in role!!");
}

String name = getName();
return "Hola " + name + "!";
return String.format("%s %s", SAYING, getName());
}

private String getName() {
return "Fred";
return context.getCallerPrincipal().getName();
}

}
Expand Up @@ -31,10 +31,6 @@
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;

import org.jboss.logging.Logger;

// Security related imports

/**
* Returns howdy greeting for INTERNAL_ROLE.
*
Expand All @@ -45,7 +41,7 @@
@Local(Howdy.class)
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class HowdyBean implements Howdy {
private static final Logger log = Logger.getLogger(HowdyBean.class);
public static final String SAYING = "Howdy";

@Resource
private SessionContext context;
Expand All @@ -54,15 +50,10 @@ public class HowdyBean implements Howdy {
Hola hola;

public String sayHowdy() {
log.trace("HowdyBean.sayHowdy(). Caller name: " + context.getCallerPrincipal().getName());
log.trace("[Howdy] calling sayHola: " + hola.sayHola());

String name = getName();

return "Howdy " + name + "! ";
return String.format("%s %s, %s", SAYING, getName(), hola.sayHola());
}

private String getName() {
return "Fred";
return context.getCallerPrincipal().getName();
}
}

0 comments on commit 02ae007

Please sign in to comment.