Question

I'm trying to write a custom authentication class for xwiki.

I have a project that has an authentication mechanism written in there and if someone trys to access the xwiki, i want it to redirect them to my project's login page by using the xwiki custom authentication.

I'm trying to write a class separately from my project and separate from the xwiki, package it up as a jar and put it in the xwiki WEB-INF/lib folder of the xwiki project as that's what the instructions say for the xwiki custom authentication.

The problem is, the class i need to write imports some of the xwiki classes. If i'm writing a separate class with these imports, it won't compile correctly.

How am i supposed to create a class that extends some of the xwiki classes if it's a separate class that needs to go into xwiki's WEB-INF/lib folder?

Here's an example that someone posted https://www.box.com/shared/9kamt5d9c5.

Simple example

package my.xwiki.custom.authentication;

import java.io.*;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.user.api.XWikiUser;
import com.xpn.xwiki.user.impl.xiki.XWikiAuthServiceImpl;


public class XWikiSSOImpl extends XWikiAuthServiceImpl {

    @Override
    public XWikiUser checkAuth(XWikiContext context) throws XWikiException {
        System.out.println("we actually in checkAuth?");
    }
}

When i add my jar file to my xwiki and run it, i get the following error:

Detailed information:

    Error number 0 in 11: Uncaught exception
Wrapped Exception: Unresolved compilation problems: 
    The import com.xpn cannot be resolved
    The import com.xpn cannot be resolved
    The import com.xpn cannot be resolved
    The import com.xpn cannot be resolved
    XWikiAuthServiceImpl cannot be resolved to a type
    XWikiUser cannot be resolved to a type
    XWikiContext cannot be resolved to a type
    XWikiException cannot be resolved to a type

com.xpn.xwiki.XWikiException: Error number 0 in 11: Uncaught exception
Wrapped Exception: Unresolved compilation problems: 
    The import com.xpn cannot be resolved
    The import com.xpn cannot be resolved
    The import com.xpn cannot be resolved
    The import com.xpn cannot be resolved
    XWikiAuthServiceImpl cannot be resolved to a type
    XWikiUser cannot be resolved to a type
    XWikiContext cannot be resolved to a type
    XWikiException cannot be resolved to a type

    at com.xpn.xwiki.web.XWikiAction.execute(XWikiAction.java:254)
    at com.xpn.xwiki.web.XWikiAction.execute(XWikiAction.java:116)
    at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
    at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
    at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
    at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.xpn.xwiki.web.ActionFilter.doFilter(ActionFilter.java:120)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.xwiki.wysiwyg.server.filter.ConversionFilter.doFilter(ConversionFilter.java:144)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.xpn.xwiki.plugin.webdav.XWikiDavFilter.doFilter(XWikiDavFilter.java:66)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.xwiki.container.servlet.filters.internal.SavedRequestRestorerFilter.doFilter(SavedRequestRestorerFilter.java:208)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.xwiki.container.servlet.filters.internal.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:111)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.Error: Unresolved compilation problems: 
    The import com.xpn cannot be resolved
    The import com.xpn cannot be resolved
    The import com.xpn cannot be resolved
    The import com.xpn cannot be resolved
    XWikiAuthServiceImpl cannot be resolved to a type
    XWikiUser cannot be resolved to a type
    XWikiContext cannot be resolved to a type
    XWikiException cannot be resolved to a type

    at my.xwiki.custom.authentication.XWikiSSOImpl <init>(XWikiSSOImpl.java:4)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at com.xpn.xwiki.XWiki.getAuthService(XWiki.java:5288)
    at com.xpn.xwiki.XWiki.checkAuth(XWiki.java:4090)
    at com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl.checkAccess(XWikiRightServiceImpl.java:205)
    at com.xpn.xwiki.XWiki.checkAccess(XWiki.java:4103)
    at com.xpn.xwiki.XWiki.prepareDocuments(XWiki.java:5128)
    at com.xpn.xwiki.web.XWikiAction.execute(XWikiAction.java:179)
    ... 34 more
Was it helpful?

Solution

Based on the link provided you don't seem to be missing any imports and assuming you don't have any obvious syntax errors then it points to your development environment. A typical solution is setting the CLASSPATH variable with the path to your package but that has been error prone in my experience.

If you are using Eclipse, right click on your Project name and select properties. In the properties dialog click on the 'Java Build Path' on the left side. Within the 'Java Build Path' properties click on the Libraries tab. You want to click on the 'Add an External Jar...' button where you will specify the path to the xwiki jar.

This is what my included jars look on my project...

OTHER TIPS

To compile a class, the compiler needs the definitions of all referenced classes (that includes the super class). It can either use classes that have already been compiled (as .class files or from a JAR file), or compile these classes from source. Since you don't want to compile the xwiki classes, you'll want to do the former.

You'll therefore need to add the jar files containing those classes to your classpath. How to do that depends on how you invoke the compiler or which IDE you use. With eclipse, you'd right-click the project -> Build Path -> Add External Archives. With javac, you can use the -cp option.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top