0

Trying to Build an API to upload Binary Using CoreService Web(8.5) in Java. On using IStreamUpload.uploadBinary(AccessTokenData, byte[]), it gives exception Due to AccessToken. It gives the following Exception

com.sdltridion.contentmanager.coreservice.IStreamUploadUploadBinaryByteArrayCoreServiceFaultFaultFaultMessage: Provided access token is invalid. at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at org.apache.cxf.interceptor.ClientFaultConverter.processFaultDetail(ClientFaultConverter.java:183) at org.apache.cxf.interceptor.ClientFaultConverter.handleMessage(ClientFaultConverter.java:83) at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308) at org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:112) at org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:70) at org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:35) at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308) at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:829) at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1696) at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1570) at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1371) at org.apache.cxf.io.CacheAndWriteOutputStream.postClose(CacheAndWriteOutputStream.java:56) at org.apache.cxf.io.CachedOutputStream.close(CachedOutputStream.java:228) at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56) at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:671) at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:63) at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308) at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:530) at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:441) at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:356) at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:314) at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96) at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:140) at com.sun.proxy.$Proxy67.uploadBinaryByteArray(Unknown Source)

Since IStreamUpload does not have getCurrentUser method so, We are making two CXFClient proxies one for ICoreService and another for IStreamUpload and passing getCurrentUser from ICorseService. Below is the code for client proxy creation.

public ICoreService getCXFClient(String url, String user, String pass) {

    String id = user + "/" + pass;
    if (mapCoreService != null && mapCoreService.get(id) == null) {
        coreServiceBaseUrl = url;
        String userNameLocal = user;
        String passwordLocal = pass;

        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        jaxWsProxyFactoryBean.setServiceClass(ICoreService.class);
        jaxWsProxyFactoryBean.setAddress(coreServiceBaseUrl
                + CORE_SERVICE_PATH + "/basicHttp");
        ICoreService lcCoreService = (ICoreService) jaxWsProxyFactoryBean
                .create();

        Client clientProxy = ClientProxy.getClient(lcCoreService);          
        LoggingInInterceptor lii = new LoggingInInterceptor();
        lii.setPrettyLogging(true);

        clientProxy.getInInterceptors().add(lii);

        LoggingOutInterceptor loi = new LoggingOutInterceptor();
        loi.setPrettyLogging(true);
        clientProxy.getOutInterceptors().add(loi);
        HTTPConduit httpConduit = (HTTPConduit) clientProxy.getConduit();

        AuthorizationPolicy authorizationPolicy = httpConduit
                .getAuthorization();
        authorizationPolicy.setUserName(userNameLocal);
        authorizationPolicy.setPassword(passwordLocal);
        authorizationPolicy.setAuthorizationType("Basic");

        HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setConnectionTimeout(10000);
        policy.setReceiveTimeout(5000);
        policy.setAllowChunking(false);
        httpConduit.setClient(policy);

        mapCoreService.put(id, lcCoreService);

    }
    if (mapCoreService != null) {
        LOGGER.info("Core service ID user [{}] pass [{}] data [{}]", user,
                "############", mapCoreService.get(id));
        return mapCoreService.get(id);
    } else {
        LOGGER.info("Core service ID user [{}] pass [{}] data [{}]", user,
                "############", "NULL");
    }

    return null;

}
Client clientProxy = null;

public UploadBinary getCXFUploadClient(String url, String user, String pass) {

    String id = user + "/" + pass;
    if (mapStreamUploadService != null && mapStreamUploadService.get(id) == null) {
        coreServiceBaseUrl = url;
        String userNameLocal = user;
        String passwordLocal = pass;

        JaxWsProxyFactoryBean jaxWsProxyFactoryBean = new JaxWsProxyFactoryBean();
        jaxWsProxyFactoryBean.setServiceClass(UploadBinary.class);
        jaxWsProxyFactoryBean.setAddress(coreServiceBaseUrl
                + CORE_SERVICE_PATH + "/streamUpload_basicHttp");
        /*QName qName = new QName(
                coreServiceBaseUrl
                + CORE_SERVICE_PATH + "/streamUpload_basicHttp");*/
        //jaxWsProxyFactoryBean.setEndpointName(qName);
        UploadBinary lcCoreService = (UploadBinary) jaxWsProxyFactoryBean
                .create();
        
        Client clientProxy = ClientProxy.getClient(lcCoreService);

        LoggingInInterceptor lii = new LoggingInInterceptor();
        lii.setPrettyLogging(true);

        clientProxy.getInInterceptors().add(lii);

        LoggingOutInterceptor loi = new LoggingOutInterceptor();
        loi.setPrettyLogging(true);
        clientProxy.getOutInterceptors().add(loi);
        HTTPConduit httpConduit = (HTTPConduit) clientProxy.getConduit();

        AuthorizationPolicy authorizationPolicy = httpConduit
                .getAuthorization();
        authorizationPolicy.setUserName(userNameLocal);
        authorizationPolicy.setPassword(passwordLocal);
        authorizationPolicy.setAuthorizationType("Basic");

        HTTPClientPolicy policy = new HTTPClientPolicy();
        policy.setConnectionTimeout(10000);
        policy.setReceiveTimeout(5000);
        policy.setAllowChunking(false);
        httpConduit.setClient(policy);

        mapStreamUploadService.put(id, lcCoreService);

    }
    if (mapStreamUploadService != null) {
        LOGGER.info("Core service ID user [{}] pass [{}] data [{}]", user,
                "############", mapStreamUploadService.get(id));
        return mapStreamUploadService.get(id);
    } else {
        LOGGER.info("Core service ID user [{}] pass [{}] data [{}]", user,
                "############", "NULL");
    }

    return null;

}

Getting error in the below method at uploadBinaryByteArray

    private String uploadFile(File file,ICoreService clientlocal) {
    try {
        byte[] fileData = new byte[(int) file.length()];
        DataInputStream dis = new DataInputStream(new FileInputStream(file));
        dis.readFully(fileData);
        dis.close();
        IStreamUpload clientUpload = CoreServiceFactory.getInstance().getCXFUploadClient( "url","***", "***");
        String uploadFile = clientUpload.uploadBinaryByteArray(clientlocal.getCurrentUser(), fileData);
      return uploadFile;
    } catch (Exception e) {
        LOGGER.error("failed [{}]" ,Util.getStack(e));
        return null;
    }
}

Thanks in advance

  • What’s in the Tridion Event Log? – Rick Pannekoek Sep 1 at 15:58
  • I think you might have to type cast this "clientlocal.getCurrentUser()" as AccessTokenData when passing into the method uploadBinaryByteArray() – Sayoni Sep 2 at 17:21
  • The typecast doesnt help, still i am getting the same error. – Prashanthi Pothuru Sep 11 at 23:56
  • ><CoreServiceFault xmlns="sdltridion.com/ContentManager/R6" xmlns:i="w3.org/2001/… access token is invalid.</Message></Messages></CoreServiceFault – Prashanthi Pothuru Sep 11 at 23:57

Your Answer

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

Browse other questions tagged or ask your own question.