8

I have an application that needs to integrate with one of SharePoint's web services. This web service cannot be accessed freely and needs authentication.

As such, the standard wsdl2java Maven plugin in my application gives an HTTP 401 error when the generate-sources phase is executed.

Is there a way to setup Maven/POM so that I can provide a user/password that will generate the stubs?

I have come across some answers saying this is not possible but all answers are older than 1 year. I haven't found if Maven have issued an update on this. One option is to save a local copy of the WSDL (as suggested here) but I would like to avoid having local copies.

Community
  • 1
  • 1
user1817897
  • 81
  • 1
  • 2

4 Answers4

8

Because you mentioned CXF then I suppose you meant cxf-codegen-plugin. It's a bit of a hack but it works.

HTTP authentication credentials can be provided using java.net.Authenticator. One need to just define his own Authenticator class which overrides getPasswordAuthentication(..) method. Then it has to be set as default Authenticator. As far as I know it can't be done declaratively (for instance using environment properties) only programatically using Authenticator.setDefault(..).

In order to call Authenticator.setDefault(..) I would use CXF extension mechanism. Create separate maven project with similar class:

public class AuthenticatorReplacer {

    public AuthenticatorReplacer(Bus bus) {
        java.net.Authenticator.setDefault(new java.net.Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("test", "test123"
                        .toCharArray());
            }
        });
    }

}

and file src\main\resources\META-INF\cxf\bus-extensions.txt with contents:

org.example.AuthenticatorReplacer::false

Then add newly created project as a dependency to cxf-codegen-plugin:

<plugin>
    <groupId>org.apache.cxf</groupId>
    <artifactId>cxf-codegen-plugin</artifactId>
    <version>${project.version}</version>
    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>cxf-authenticator-replacer</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    ...
</plugin>

This way AuthenticatorReplacer is initialized by CXF extension mechanism and replaces default Authenticator with ours.

Dawid Pytel
  • 2,750
  • 1
  • 23
  • 30
  • You saved my life with this today. It would be nice if anybody could find out how to achieve the same without a "hack", but hey it works., – membersound Nov 03 '15 at 11:16
1

An clean alternative to @Dawid Pytel's solution would be to run this class during lifecycle of wsdl class auto generation:

<plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>exec-maven-plugin</artifactId>
      <version>1.4.0</version>
      <executions>
        <execution>
          <phase>generate-sources</phase>
          <goals>
            <goal>java</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <mainClass>path.to.AuthenticatorReplacer</mainClass>
      </configuration>
    </plugin>

Important: your AuthenticatorReplacer has to be a main(String[] args) class and running the code inside.

membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • Your solution does not allow to externalize login/password informations. Moreover, the exec plugin needs the class to be compiled, and it is not during generate-sources phase. – lpratlong Dec 10 '18 at 11:17
0

I verified that Dawid's solution works. Alternatively, you can use SoapUI to pull down and cache the wsdl and then use SoapUi code generation support to use cxf to generate the code.

http://java.dzone.com/tips/generating-client-java-code

Robert Hutto
  • 1,320
  • 12
  • 7
-1

Dawid's solution works for me too. It is a little tricky though. In Eclipse, the pom.xml keeps complaining that "wsdl2java failed: Could not load extension class AuthenticatorReplacer". You have to ignore this error message and use the command line:

mvn generate-sources

The Java classes will then be generated successfully.

Falko
  • 17,076
  • 13
  • 60
  • 105
Lao Pan
  • 1
  • 1
  • Welcome to the site. Comments should be posted as such, not as answer. Feel free to ask a separate question if you need to fix a different problem. See: [ Help Center > Answering Why and how are some answers deleted?](http://stackoverflow.com/help/deleted-answers). – mins Mar 13 '15 at 19:23
  • Welcome to the site. Comments should be posted as such, not as answer. Feel free to ask a separate question if you need to fix a different problem. See: [Why and how are some answers deleted?](http://stackoverflow.com/help/deleted-answers). – mins Mar 13 '15 at 19:23