How can I implement Picketlink Authenticator in the war layer - jboss

As the title say, I created a class in the war layer that is annotated with #Picketlink. Note that I have an ear deployment structure (ejb, war).
The custom authenticator:
#PicketLink
public class PicketlinkAuthenticator extends BaseAuthenticator { }
If I put that class in the ejb layer, the authentication is ok but when I put it to the war layer it seems like it's not found by the project as it's throwing:
20:49:46,027 INFO [org.picketlink.common] (default task-10) Using logger implementation: org.picketlink.common.DefaultPicketLinkLogger
20:49:46,043 INFO [org.picketlink.idm] (default task-10) PLIDM001000: Bootstrapping PicketLink Identity Manager
20:49:46,068 WARN [org.picketlink.idm] (default task-10) PLIDM001101: Working directory [\tmp\pl-idm] is marked to be always created. All your existing data will be lost.
20:49:46,111 INFO [org.picketlink.idm] (default task-10) PLIDM001100: Using working directory [\tmp\pl-idm].
20:49:46,127 DEBUG [org.picketlink.idm] (default task-10) No partitions to load from \tmp\pl-idm\pl-idm-partitions.db
20:49:46,152 DEBUG [org.picketlink.idm] (default task-10) Initializing Partition [6a373282-0173-4b7d-bd6a-ff0e5dc43436] with id [6a373282-0173-4b7d-bd6a-ff0e5dc43436].
20:49:46,153 DEBUG [org.picketlink.idm] (default task-10) Loaded Agents for Partition [6a373282-0173-4b7d-bd6a-ff0e5dc43436].
20:49:46,154 DEBUG [org.picketlink.idm] (default task-10) Loaded Credentials for Partition [6a373282-0173-4b7d-bd6a-ff0e5dc43436].
Why not just move the authenticator to the ejb side?
->Because I'm throwing custom error like user expired, etc. I need jsf to post these error messages.
Why not move the picketlink dependency in the web layer?
->Because my account that extended the picketlink account is binded to my services.
As suggested here I already added the picketlink module in the war project:
https://docs.jboss.org/author/display/PLINK/JBoss+Modules
<jboss-deployment-structure>
<ear-subdeployments-isolated>false</ear-subdeployments-isolated>
<sub-deployment name="THE-WAR-MODULE-THAT-REQUIRES-PICKETLINK.war">
<dependencies>
<module name="org.picketlink" />
</dependencies>
</sub-deployment>
</jboss-deployment-structure>
Anyway around this? I just want to show some custom errors :-(

I was not able to solve this problem but I have a work-around solution and that is to move the picketlink module to the web layer and just pass the identity instance to the services that need it.

I have been missing around with the same problem as well for a while now (it's 2016 now ...). What seems to make it work is to add the following CDI annotations:
#PicketLink
#Name
#RequestScoped
public class PicketlinkAuthenticator extends BaseAuthenticator { }
I would have expected the core Authentication Manager to pick this up just based on the #PicketLink Annotation, but without the CDI Annotations, the custom Authenticator class is never even loaded. Maybe there is an other way that will require us to bootstrap PicketLink - but I could not find any references.

Related

How to share the session in multiple WAR in EAR file in JBOSS-7.3.6

How to share the session in multiple WAR in EAR file in JBOSS-7.3.6 ?
we have following entry in jboss-all.xml JBoss 7.2 version (before migration)
<jboss xmlns="urn:jboss:1.0">
<shared-session-config xmlns="urn:jboss:shared-session-config:1.0">
<max-active-sessions>10</max-active-sessions>
<session-config>
<session-timeout>0</session-timeout>
<cookie-config>
<name>JSESSIONID</name>
<domain>domainName</domain>
<path>/cookiePath</path>
<comment>cookie comment</comment>
<http-only>true</http-only>
<secure>true</secure>
<max-age>-1</max-age>
</cookie-config>
<tracking-mode>COOKIE</tracking-mode>
</session-config>
<replication-config>
<cache-name>web</cache-name>
<replication-granularity>SESSION</replication-granularity>
</replication-config>
</shared-session-config>
</jboss>
But now I'm putting the same xml.. or below xml snippet but could not work in JBOSS-7.3.6
<shared-session-config xmlns="urn:jboss:shared-session-config:2.0">
<distributable/>
<max-active-sessions>10</max-active-sessions>
-- rest copy from above
getting the following exception
21:50:31,221 ERROR [io.undertow.request] (default task-1) UT005023: Exception handling request to URL: java.lang.IllegalStateException: WFLYCLWEBUT0001: Session idKL5QLs4873uTTnzT6JFg03_avxMEa8Q1-B4Jy4 is invalid
at org.wildfly.clustering.web.undertow.session.DistributableSession.validate(DistributableSession.java:265)
at org.wildfly.clustering.web.undertow.session.DistributableSession.validate(DistributableSession.java:257)
Can anyone suggest how to achieve ?
Thanks in advance !
First of all you have to know that sharing sessions between web applications would violate the JavaEE Servlet specification! That means your applications may not be portable! However you can check the coresponding documentation for more info: Development Guide / Configuring Session Sharing Between Subdeployments In Enterprise Archives

Resteasy Bean Validation Not Working on Remote Server

I have a problem similar to the one described here.
I am using RESTEasy within a standalone Jetty application. When I start the application locally and call a service (e.g. localhost:16880/rest/user/login) bean validation works fine, i.e. I get validation errors like this:
[PARAMETER]
[UserService#login(arg0).appKey]
[app_key may not be null or empty]
[]
However, when I deploy my application to a remote host and call the same service (e.g. remotehost:16880/rest/user/login) bean validation is not invoked at all.
I am using the #ValidateRequest annotation for the service and #Valid annotation for the bean parameter.
My Resteasy version is 3.0.13.Final, though I have tried earlier versions as well. I have tried to write my custom validator, but that didn't work either.
I am puzzled why the validation works locally, but not on remote server. Any suggestions would be highly appreciated.
Since you are using Jetty as standalone server, you have to define RESTEasy validation providers where you define ServletContextHandler. Note that in standalone server there is no container to scan for #Provider classes and to activate them, so you must do it manually.
I expect that you create and start your server app something like:
//create a server listening at some port
Server server= new Server(port);
//add server handlers
HandlerList handlers= new HandlerList();
initHandlers(handlers);
server.setHandler(handlers);
//start the server
server.start();
In initHandlers you must have defined your RESTEasy support:
public void initHandlers(List<HandlerList> handlers) {
//define root context handler
ServletContextHandler servletContextHandler= new ServletContextHandler(ServletContextHandler.SESSIONS);
servletContextHandler.setContextPath("/");
handlers.addHandler(servletContextHandler);
//define RESTEasy handler
ServletHolder restServlet= new ServletHolder(new HttpServlet30Dispatcher());
//since this is a standalone server, somewhere you have to define RESTful services and Singletons
restServlet.setInitParameter("javax.ws.rs.Application", "com.exampleapp.MyRestApplication");
restServlet.setInitParameter("resteasy.servlet.mapping.prefix", "rest");
servletContextHandler.addServlet(restServlet, "rest/*");
}
So what is left to do now is to add Validation provider as init parameter:
restServlet.setInitParameter("resteasy.providers", "org.jboss.resteasy.plugins.validation.ValidatorContextResolver,org.jboss.resteasy.api.validation.ResteasyViolationExceptionMapper");
On this link I tried to find the name of the validator providers: https://docs.jboss.org/resteasy/docs/3.0.4.Final/userguide/html/Validation.html
RESTEasy obtains a bean validation implemenation by looking in the available META-INF/services/javax.ws.rs.Providers files for an implementation of ContextResolver
So it does not say what, but says where. Now open the "resteasy-hibernatevalidator-provider-3...*.jar (from Eclipse -> Maven dependencies or manually unzip) and look into META-INF/services/javax.ws.rs.ext.Providers It says:
org.jboss.resteasy.plugins.validation.hibernate.ValidatorContextResolver
org.jboss.resteasy.api.validation.ResteasyViolationExceptionMapper
If you don't have this dependency, then add it to your pom file:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-hibernatevalidator-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
One more note: that at the same place where you described validation providers, you also add other providers, if you happen to need them (such as JacksonJaxbJson, etc).

Openshift - deploying simple Java EE app on Wildfly fails

I'm trying to deploy a very simple application on Openshift. It's an EAR project with a single WAR and EJB module. Inside the WAR there's a REST service that calls an EJB defined in EJB module. Locally and on Openshift I'm using Wildfly 9.0.0 CR2 and PostgreSQL 9.2. When deploying locally everything works fine. When the same code is deployed on Openshift I'm getting following errors in logs:
2015-06-28 18:23:04,574 WARN [org.jboss.as.clustering.jgroups] (MSC service thread 1-1) WFLYCLJG0006: property bind_addr for protocol org.jgroups.protocols.TCP attempting to override socket binding value 127.12.77.1 : property value 127.12.77.1 will be ignored
2015-06-28 18:23:04,574 WARN [org.jboss.as.clustering.jgroups] (MSC service thread 1-1) WFLYCLJG0006: property bind_port for protocol org.jgroups.protocols.TCP attempting to override socket binding value 7600 : property value 7600 will be ignored
2015-06-28 18:23:06,252 INFO [org.jboss.as.jpa] (ServerService Thread Pool -- 70) WFLYJPA0010: Starting Persistence Unit (phase 2 of 2) Service 'cooking.ear/cooking-ejb-1.0-SNAPSHOT.jar#cookingPU'
2015-06-28 18:23:08,165 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-1) MSC000001: Failed to start service jboss.jgroups.channel.ee: org.jboss.msc.service.StartException in service jboss.jgroups.channel.ee: java.security.PrivilegedActionException: java.net.BindException: [TCP] /127.12.77.1 is not a valid address on any local network interface
at org.wildfly.clustering.jgroups.spi.service.ChannelBuilder.start(ChannelBuilder.java:79)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.security.PrivilegedActionException: java.net.BindException: [TCP] /127.12.77.1 is not a valid address on any local network interface
at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:638)
at org.jboss.as.clustering.jgroups.JChannelFactory.createChannel(JChannelFactory.java:99)
at org.wildfly.clustering.jgroups.spi.service.ChannelBuilder.start(ChannelBuilder.java:77)
... 5 more
Caused by: java.net.BindException: [TCP] /127.12.77.1 is not a valid address on any local network interface
at org.jgroups.util.Util.checkIfValidAddress(Util.java:3480)
at org.jgroups.stack.Configurator.ensureValidBindAddresses(Configurator.java:902)
at org.jgroups.stack.Configurator.setupProtocolStack(Configurator.java:118)
at org.jgroups.stack.Configurator.setupProtocolStack(Configurator.java:57)
at org.jgroups.stack.ProtocolStack.setup(ProtocolStack.java:477)
at org.jgroups.JChannel.init(JChannel.java:854)
at org.jgroups.JChannel.<init>(JChannel.java:159)
at org.jboss.as.clustering.jgroups.JChannelFactory$1.run(JChannelFactory.java:96)
at org.jboss.as.clustering.jgroups.JChannelFactory$1.run(JChannelFactory.java:93)
at org.wildfly.security.manager.WildFlySecurityManager.doChecked(WildFlySecurityManager.java:634)
... 7 more
The address mentioned - 127.12.77.1 is $OPENSHIFT_WILDFLY_IP.
I have no idea what is causing this issue. First I thought it's a database connectivity issue because it happens when 2nd phase of starting persistence unit happens. I connected to DB on Openshift and saw that it was created successfully so maybe that's not it, but here's the persistence.xml I'm using:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="cookingPU">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/PostgreSQLDS</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect" />
</properties>
</persistence-unit>
</persistence>
The datasource used is the default one. I didn't change anything in standalone.xml.
Another thing I noticed is that the deploy problem happens when I add any EJB to the project.
This is a simple one I tried to use:
#Stateless
public class AnyEjb {
public String hello() {
return "Hi there!";
}
}
This is defined in EJB module. Then in web module I have this class calling it:
#Path("anything")
#Consumes(MediaType.APPLICATION_JSON)
#Produces(MediaType.APPLICATION_JSON)
public class AnyEndpoint {
#EJB
private AnyEjb anyEjb;
#GET
public String sayHi() {
return anyEjb.hello();
}
}
I'm not sure if and how it can be connected with this BindException.
I've tried running this application locally with both standalone and standalone-full-ha profile and it works in both cases. I just feel it has to be some issue with Openshift configuration but I have no idea where to look anymore. I'm very new to Openshift and Java EE. Please point me in a right direction. Any help will be much appreciated.
Might be https://issues.jboss.org/browse/JGRP-1928. Talk to Rado Husar to see how to resolve this.
It looks like the problem is (as #Bela Ban suggested) connected with the version of JGroups shipped with WildFly 9.0.0CR2. I will be waiting for the fix coming with the Final version of WildFly.
Meanwhile as a workaround to be able to deploy an application on WildFly 9.0.0CR2 on Openshift, I decided to disable clustering capabilities for my server.
In standalone.xml available in .openshift I have removed org.jboss.as.clustering.jgroups module and changed infinispan cache settings from distributed to local.
I have been basing on this solution (for WildFly 8): https://gist.github.com/fjuma/3df7f64fbaebd5506ef5#file-standalone-xml
But I had to modify it so that it works on Wildfly 9. Full standalone.xml that's been working for me is available here for reference http://pastebin.com/aANkPUWk

Why does Gate.init() call throws java.lang.NullPointerException?

I'm new to GATE, ANNIE & Info Extraction(IE). I'm trying to use ANNIE to do some IE. I installed GATE on Ubuntu 14.*, GATE Developer 8 worked perfectly. I'm trying to use GATE Embedded from my Java (JDK 1.8) project in Eclipse (Luna). I created a user library to add necessary GATE Jars (gate.jar & all jars in Lib) to build path. I added gate.home system property using
System.setProperty("gate.home", "/home/haree/GATE_Developer_8.0");
I tried the examples provided on GATE link, but I'm stuck with the following Null Pointer Exception which I couldn't fix.
Output:
Initialising GATE...
log4j:WARN No appenders could be found for logger (gate.Gate).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Exception in thread "main" java.lang.NullPointerException
at gate.util.Files.getResource(Files.java:399)
at gate.util.Files.getGateResource(Files.java:419)
at gate.Gate.init(Gate.java:178)
at sheffield.examples.TestGate.main(TestGate.java:16)
I noticed that the Exception is caused by - Gate.init() method call. I wrote a simple class with a call to the above method and played with that system property(gate.home) etc but of no use. I somehow feel that the exception is caused when the GATE API is trying to read the gate.xml file which is in the installation directory. I copy that here as well along with code I wrote. Any help in fixing this is appreciated.
Code:
package sheffield.examples;
import gate.Gate;
import gate.util.GateException;
import gate.util.Out;
public class TestGate {
public static void main(String[] args) {
// initialise the GATE library
Out.prln("Initialising GATE...");
System.setProperty("gate.home", "/home/haree/GATE_Developer_8.0");
try {
Gate.init();
} catch (GateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Out.prln("...GATE initialised");
}
}
gate.xml file:
<?xml version="1.0"?>
<!-- gate.xml -->
<!-- $Id: gate.xml 17080 2013-11-12 19:29:34Z markagreenwood $ -->
<!-- any entries in this file will be processed at Gate.init()
time - i.e. will appear in every GATE invocation -->
<GATE>
<GATECONFIG Save_options_on_exit="true" Save_session_on_exit="true"/>
</GATE>
Note: I googled and found this link which discusses the same problem but it didn't help me
More Info:
I configured log4j which I ignored before and logs look like this:
Initialising GATE...
18:49:37 INFO [main] - gate.Gate.initLocalPaths - Using /home/haree/GATE_Developer_8.0 as GATE home
18:49:37 INFO [main] - gate.Gate.initLocalPaths - Using /home/haree/GATE_Developer_8.0/plugins as installed plug-ins directory.
18:49:37 INFO [main] - gate.Gate.initLocalPaths - Using /home/haree/GATE_Developer_8.0/gate.xml as site configuration file.
18:49:37 INFO [main] - gate.Gate.initLocalPaths - Using /home/haree/.gate.xml as user configuration file
18:49:37 INFO [main] - gate.Gate.initLocalPaths - Using /home/haree/.gate.session as user session file
Exception in thread "main" java.lang.NullPointerException
at gate.util.Files.getResource(Files.java:399)
at gate.util.Files.getGateResource(Files.java:419)
at gate.Gate.init(Gate.java:178)
at sheffield.examples.TestGate.main(TestGate.java:16)
I have read that GATE creates/ uses two configuration files in the user home directory namely: 1)gate.xml & 2)gate.session
I couldn't find both these files in the home directory for some reason (I'm new to Ubuntu file system), not sure if they are hidden files.
The issue has to do with the class loading in Eclipse, and I suspect it wouldn't be happening if you did it in IntelliJ or via the CLI (I tried it in IntelliJ Idea, and it worked fine.)
When you call
Gate.init();
It's stepping through a bunch of code that initializes it's state, and one of them tries to load a resource using the custom Gate class loader (in Files.getResource()). The class loader hasn't been initialized yet though, so it falls back to trying to load the resource using this:
return Files.class.getClassLoader().getResource(resourceName);
Which, in some cases (the Eclipse case, specifically), will return a null classloader. This is (I suppose) a bug in Gate, as they should fall back to the system classloader in this case.
This is the same issue as here: getClass().getClassLoader() is null, why?

Configuring a war file

My end goal is simple. I am building a website that has three components: a mobile interface, an admin web interface, and a web user interface. I plan to put each of these modules into separate WAR files and, together with an EJB module & possibly a common library module, bundle them all into a EAR for deployment on JBoss AS 7.1.
But first things first, can I deploy a very simple WAR file and get it to work - NO!
My war file has the following structure:
project-mobile.war:
META-INF/MANIFEST.MF
WEB-INF/jboss-web.xml
WEB-INF/classes/org/dobbo/project/mobile/Accounts.class
index.html
The manifest.mf file as the following content:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.0
Created-By: 1.6.0_18-b18 (Sun Microsystems Inc.)
Built-By: steve
The jboss-web.xml file's content are:
<?xml version="1.0"?>
<jboss-web>
<context-root>/mobile</context-root>
</jboss-web>
The Accounts.java file was based on the Library.java example from https://docs.jboss.org/author/display/AS71/Java+API+for+RESTful+Web+Services+(JAX-RS)
I did make some changes, added the Application subclassing and the AppicationPath annotation as this got me a little further in getting the deployment to work.
package org.dobbo.<project>.mobile;
import javax.ws.rs.*;
import javax.ws.rs.core.Application;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
#ApplicationPath("/accounts")
#Consumes({"application/xml"})
#Produces({"application/xml"})
public class Accounts
extends Application {
...
#GET
#Path("/list")
public Collection<UUID> getAccounts() {
Collection<UUID> list = uuids.values();
log.info("getAccounts: " + list);
return list;
}
...
}
So far so good.
When I deploy I get the following logged to the JBoss console (timestamp & log level INFO removed):
[org.jboss.as.repository] (management-handler-thread - 12) JBAS014900: Content added at location /home/jboss/jboss-7.1.1/standalone/data/content/11/e8cdaea9dfa4e984ada3e22b0c890970f0ba75/content
[org.jboss.as.server.deployment] (MSC service thread 1-3) JBAS015877: Stopped deployment sokoban-mobile.war in 60ms
[org.jboss.as.server.deployment] (MSC service thread 1-1) JBAS015876: Starting deployment of "project-mobile.war"
[org.jboss.web] (MSC service thread 1-2) JBAS018210: Registering web context: /mobile
[org.jboss.as.server] (management-handler-thread - 12) JBAS018565: Replaced deployment "project-mobile.war" with deployment "project-mobile.war"
So far all seems well. But when I point my browser at jboss-server:8080/mobile/accounts/list I get a:
HTTP Status 404 - Could not find resource for relative : /list of full path: http://jboss-server:8080/mobile/accounts/list
type Status report
message Could not find resource for relative : /list of full path: http://jboss-server:8080/mobile/accounts/list
description The requested resource (Could not find resource for relative : /list of full path: http://jboss-server:8080/mobile/accounts/list) is not available.
However, something is working because pointing the broswer at jboss-server:8080/mobile/ successfully returns the index.html file also included in the WAR file.
It's probably just a simple configuration error on my part, but I can't work it out for the life of me. Many thanks to all that take the trouble to read this issue and even more thanks for posting any ideas you have.
Steve
The logs shows that no REST resource is loaded.
The resource is missing the #Path annotation.
You need on Application class with the #ApplicationPath annotation (The class will be empty).
Then you need to add the #Path annotation to your resource class (Accounts for your case)
So you will have:
#ApplicationPath("/")
public class AccountApp
extends Application {}
And:
#Path("accounts")
#Consumes({"application/xml"})
#Produces({"application/xml"})
public class Accounts{
...
#GET
#Path("/list")
public Collection<UUID> getAccounts() {
Collection<UUID> list = uuids.values();
log.info("getAccounts: " + list);
return list;
}
...
}