1

I am not able to connect to cms with java using core service.

I have done the following steps

core-service url i am using like http://CMS-Server/webservices/CoreService2011.svc?wsdl

maven configuration is like

<build>
    <finalName>generated-webservices</finalName>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>1.9</version>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>${project.build.directory}/generated/src/main/java</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>jaxws-maven-plugin</artifactId>
            <version>1.12</version>
            <configuration>
                <wsdlUrls>
                    <wsdlUrl>http://CMS-Server/webservices/CoreService2011.svc?wsdl</wsdlUrl>
                </wsdlUrls>
                <keep>true</keep>
                <sourceDestDir>${project.build.directory}/target/generated/src/main/java</sourceDestDir>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>wsimport</goal>
                    </goals>
                </execution>
            </executions>
            <dependencies>
                <!-- Use current JAX-WS RI version -->
                <dependency>
                    <groupId>com.sun.xml.ws</groupId>
                    <artifactId>jaxws-tools</artifactId>
                    <version>2.1.7</version>
                </dependency>
                <dependency>
                    <groupId>com.sun.xml.ws</groupId>
                    <artifactId>jaxws-rt</artifactId>
                    <version>2.1.7</version>
                </dependency>
            </dependencies>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>xmlbeans-maven-plugin</artifactId>
            <version>2.3.3</version>
            <executions>
                <execution>
                    <goals>
                        <goal>xmlbeans</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaDirectory>src/main/resources/wsdl</schemaDirectory>
                <xmlConfigs>
                    <xmlConfig implementation="java.io.File">${basedir}/src/main/resources/config/xmlbeans.xsdconfig</xmlConfig>
                </xmlConfigs>
                <sourceGenerationDirectory>${project.build.directory}/generated-sources</sourceGenerationDirectory>
            </configuration>
        </plugin>
    </plugins>
</build>
  • created authenticator class

    public class BasicHttpAuthenticator extends Authenticator {
    
    private final String user;
    private final String password;
    
    public BasicHttpAuthenticator(String user, String password) {
        this.user = user;
        this.password = password;
    }
    
    @Override
    protected PasswordAuthentication getPasswordAuthentication(){
        return new PasswordAuthentication(user, password.toCharArray());
    }
    }
    

connecting to core service like

private static final QName Q_NAME = new QName("http://www.sdltridion.com/ContentManager/CoreService","CoreService2011");

public String connectCoreService() throws Exception {
    BasicHttpAuthenticator basicHttpAuthenticator = new BasicHttpAuthenticator(username, password);
    Authenticator.setDefault(basicHttpAuthenticator);
    URL url = new URL(wsdl);
    CoreService2011 service = new CoreService2011(url,Q_NAME);
    ICoreService endpoint = service.getBasicHttp();
    UserData currentUser = endpoint.getCurrentUser();
    System.out.println(String.format("'%s' %s", currentUser.getTitle(), currentUser.getId()));
    return "core service connected";
}

but getting error at time when calling

endpoint.getCurrentUser();

with error message :-

Oct 14, 2016 2:13:25 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [dispatcher-servlet] in context with path [] threw exception [Request processing failed; nested exception is com.sun.xml.ws.client.ClientTransportException: The server sent HTTP status code 401: Unauthorized] with root cause com.sun.xml.ws.client.ClientTransportException: The server sent HTTP status code 401: Unauthorized at com.sun.xml.ws.transport.http.client.HttpTransportPipe.checkStatusCode(HttpTransportPipe.java:203) at com.sun.xml.ws.transport.http.client.HttpTransportPipe.process(HttpTransportPipe.java:179) at com.sun.xml.ws.transport.http.client.HttpTransportPipe.processRequest(HttpTransportPipe.java:94) at com.sun.xml.ws.transport.DeferredTransportPipe.processRequest(DeferredTransportPipe.java:116) at com.sun.xml.ws.api.pipe.Fiber.__doRun(Fiber.java:598) at com.sun.xml.ws.api.pipe.Fiber._doRun(Fiber.java:557) at com.sun.xml.ws.api.pipe.Fiber.doRun(Fiber.java:542) at com.sun.xml.ws.api.pipe.Fiber.runSync(Fiber.java:439) at com.sun.xml.ws.client.Stub.process(Stub.java:222) at com.sun.xml.ws.client.sei.SEIStub.doProcess(SEIStub.java:135) at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:109) at com.sun.xml.ws.client.sei.SyncMethodHandler.invoke(SyncMethodHandler.java:89) at com.sun.xml.ws.client.sei.SEIStub.invoke(SEIStub.java:118) at com.sun.proxy.$Proxy66.getCurrentUser(Unknown Source) at com.test.cms.coreservice.controllers.CoreServiceController.connectCoreService(CoreServiceController.java:53) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578) at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) at javax.servlet.http.HttpServlet.service(HttpServlet.java:621) at javax.servlet.http.HttpServlet.service(HttpServlet.java:728) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745)

please provide some input, so that would help me to solve this.

  • Are you running the Java client directly on the server - if so, does the Core Service webservice accept "Basic Authentication" – Dylan .. Mark Saunders Oct 14 '16 at 10:36
  • Hi Dylan, I am running client from my machine and trying to connect CMS server and failing, but i can connect cms directly from browser and authentication of my machine is different from authentication of cms. – Neetesh Narvaria Oct 14 '16 at 10:48
  • 1
    Neetesh - can you update your question to clarify that is doesn't work under s specific condition but it does in others so readers can better visual the scenario you're in. And on that - I believe that if you run it from the application it will be trying to connect via Basic Authentication and not using the credentials you supply in code - thus you should confirm that (1) Basic Authentication is permitted through the webservice and (2) the user you are logged in has the permissions required to access the webservice. – Dylan .. Mark Saunders Oct 14 '16 at 11:15
  • Hi Dylan, i am providing correct authentication through which i connect cms, but i suspect that when using Basic Authentication it is picking my machine authentication, although i am not able to find actual problem, to run application from cms server i don't have permission right now, if i get permission then i check and update the question. – Neetesh Narvaria Oct 14 '16 at 11:27
2

If this works when using your browser but doesn't when you try to use the application directly / on the server then it could be that the application will be trying to connect via Basic Authentication and not using the credentials you supply in code.

You should confirm that

  • Basic Authentication is permitted through the webservice and
  • the user you are logged in has the permissions required to access the webservice
|improve this answer|||||
  • Dylan, Thanks for your feedback, I've tried with Binding username and password property to client and i can access CMS now. – Neetesh Narvaria Oct 18 '16 at 5:11
1

I have tried by binding username and password property, which i was missing.

so i have removed this line

ICoreService endpoint = service.getBasicHttp();

and added

    ICoreService client = service.getPort(ICoreService.class);
    BindingProvider bp = (BindingProvider) client;
    bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "domain\\username");
    bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "password");

after this now i am able to access cms using core service

public String connectCoreService() throws Exception {

        BasicHttpAuthenticator basicHttpAuthenticator = new BasicHttpAuthenticator(username, password);
        Authenticator.setDefault(basicHttpAuthenticator);

        URL url = new URL(wsdl);
        CoreService2011 service = new CoreService2011(url, Q_NAME);

        ICoreService client = service.getPort(ICoreService.class);
        BindingProvider bp = (BindingProvider) client;
        bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "domain\\username");
        bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "password");

        UserData currentUser = client.getCurrentUser();
        System.out.println(String.format("'%s' %s", currentUser.getTitle().getValue(), currentUser.getId().getValue()));
        return "core service connected";
    }
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.