Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
abhijeet_mukkawar
Active Contributor

Taking the document NW / CE – SharePoint Connectivity : Uploading a file on SharePoint further, a natural extension to read a file from SharePoint can be provided. For reading file from SharePoint, HTTPGet method is used. The GET method retrieve whatever information (in the form of an entity) is identified by the Request-URI. If the Request-URI refers to a data-producing process, it is the produced data which shall be returned as the entity in the response and not the source text of the process, unless that text happens to be the output of the process. HTTPGet method accepts an URI of the file to be read from SharePoint and returns the content, here, in a simple string attribute.

Below is a code for a method that accepts a URI path of the SharePoint file and returns its content in string format.

(Please refer the document NW / CE – SharePoint Connectivity : Uploading a file on SharePoint for Prerequisite and Implementation details. Below code is just for the method without any additional details)

public String httpCallRead(String filePath){

                    

            String strReturn =  "" ;

        

            // CloseableHttpClient provides HTTPClient interface for executing HTTP methods.

            // Execution of HTTP method involves one or more HTTP requests/response exchanges

            // which is handled by HTTP Client interface. We provide the request object which

            // will be executed by HttpClient to transmit request to target server and gets the response back.

        

            CloseableHttpClient httpclient = HttpClients.createDefault();

            CloseableHttpResponse response1 = null;

        

  

            try{

              

                  // Credentials providers are intended to maintain a set of user credentials

                  // and to be able to produce user credentials for a particular authentication scope.

                  // When registering credentials with the credentials provider one can provide a

                  // wild card (any host, any port, any realm, any scheme) instead of a concrete attribute value.

              

                  CredentialsProvider credsProvider = new BasicCredentialsProvider();

                  credsProvider.setCredentials(AuthScope.ANY,

                              new NTCredentials(<userId>, <password>, <workstation>, <domain>));

              

                  HttpHost target = new HttpHost(<SharePoint host>, <port>);

                  // Creates context for stateful connection with SharePoint which is used to execute

                  // logically related requests

                  HttpClientContext context = HttpClientContext.create();

                  context.setCredentialsProvider(credsProvider);             

                  HttpGet httpget = new HttpGet(filePath);

                  try {

                        response1 = httpclient.execute(target, httpget, context);

                        HttpEntity entity1 = (response1).getEntity();

// we can know the success/failure status of the response using getStatusLine(). The string attribute will read the status line – and assuming the successful return – will read the contents using EntityUtils.toString(entity)

                        strReturn = strReturn+" "+entity1.toString()+" "+response1.getStatusLine()+"    "+EntityUtils.toString(entity1);

                  } catch(Exception e){

                        strReturn = strReturn+" "+"Inner try , cheap call: "+e.getLocalizedMessage();

                        e.printStackTrace();

                  }finally {

                        response1.close();

                  }

        

            }catch(Exception e){

                  strReturn = strReturn+" "+"Outer try :"+e.getLocalizedMessage();

                  e.printStackTrace();

            }

            return strReturn ;

      }

To test the code we can use web service wrapper using EJB that can be executed from WS Navigator by supplying the URI on SharePoint server.

(TextDocument.txt from SharePoint server with content “This is a test message” is read by passing the URI.)

As NTLM connections are stateful, it is generally recommended to trigger NTLM authentication using a relatively cheap method, such as GET or HEAD, and re-use the same connection to execute more expensive methods, especially those enclose a request entity, such as POST or PUT.

Taking this concept in the current context, we better call HTTPGet method first followed by HTTPPut.

1 Comment
Labels in this area