Example Rest Client Accessing Jive Core REST API v2 using Spring RestTemplate

Version 11

    Following is the example code showing a Rest Client accessing Jive Core Rest API using Spring RestTemplate to create a document in a social group.

    Following code also uses HttpClient 4.2 API to do BasicAuthentication.

     

    Maven dependencies are all also listed.

     

    Code is attached in a zip file (testwebserviceclient.zip). Unzip and build using maven. You can also import the project to eclipse.

     

    Maven Dependencies
    <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <org.springframework.version>3.1.2.RELEASE</org.springframework.version>
    </properties>

     

     

    <dependencies>

     

     

    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>3.8.1</version>
    <scope>test</scope>
    </dependency>

     

     

    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2.1</version>
    </dependency>

     

     

    <dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.2.1</version>
    </dependency>

     

     

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>${org.springframework.version}</version>
    </dependency>

     

     

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${org.springframework.version}</version>
    </dependency>

     

     

    </dependencies>

     

     

    RestClient Java Code

     

    import java.util.ArrayList;

    import java.util.List;

     

    import org.apache.http.HttpHost;

    import org.apache.http.auth.AuthScope;

    import org.apache.http.auth.UsernamePasswordCredentials;

    import org.apache.http.client.AuthCache;

    import org.apache.http.client.protocol.ClientContext;

    import org.apache.http.impl.auth.BasicScheme;

    import org.apache.http.impl.client.BasicAuthCache;

    import org.apache.http.impl.client.DefaultHttpClient;

    import org.apache.http.protocol.BasicHttpContext;

    import org.springframework.http.converter.FormHttpMessageConverter;

    import org.springframework.http.converter.HttpMessageConverter;

    import org.springframework.http.converter.StringHttpMessageConverter;

    import org.springframework.util.LinkedMultiValueMap;

    import org.springframework.util.MultiValueMap;

    import org.springframework.web.client.RestTemplate;

     

     

    /**

    * Rest client using spring Rest template which supports Basic Authentication.

    * The functionality of this class is to create a Document in a SocialGroup.

    */

    public class RestClient {


      public static void main(String[] args) throws Exception {

          RestClient client = new RestClient();

          client.createDocumentInGroup();

      }

     

      public void createDocumentInGroup() {

     

          String groupId = "1008";

     

          String restUrl = "http://localhost:8080/api/core/v2/groups/" + groupId + "/documents";

     

          String subject = "Rest Document";

     

          String html = "<h1>Rest Document</h1><p>Rest is fun. Creating document with Rest is even more fun</p>";

     

          MultiValueMap<String, String> params= getRequestParam(subject, html);

     

          List<HttpMessageConverter<?>> list = getMessageConverters();

     

          RestTemplate restTemplate = getRestTemplate();

     

          restTemplate.setMessageConverters(list);

     

          String result = restTemplate.postForObject(restUrl,  params, String.class);

     

          System.out.println(result);

     

          closeConnection(restTemplate);

      }

     

      private MultiValueMap<String, String> getRequestParam(String subject, String html) {

     

          MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();

     

          List<String> valueList = new ArrayList<String>();

     

          valueList = new ArrayList<String>();

          valueList.add(subject);

          map.put("subject", valueList);

     

          valueList = new ArrayList<String>();

          valueList.add(html);

          map.put("html", valueList);

     

          return map;

      }

     

      private void closeConnection(RestTemplate restTemplate) {

          ((ContextAwareHttpComponentsClientHttpRequestFactory) restTemplate.getRequestFactory()).getHttpClient().getConnectionManager().shutdown();

      }

     

      private List<HttpMessageConverter<?>> getMessageConverters() {

          HttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();

          HttpMessageConverter stringHttpMessageConverternew = new StringHttpMessageConverter();

     

          List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();

          list.add(formHttpMessageConverter);

          list.add(stringHttpMessageConverternew);

          return list;

      }

     

      /**

      * Get Rest Template

      * @return

      */

      public RestTemplate getRestTemplate() {

          ContextAwareHttpComponentsClientHttpRequestFactory customRequestFactory = getRequestFactory();

          RestTemplate restTemplate = new RestTemplate(customRequestFactory);

          return restTemplate;

      }

     

     

      /**

      * Get Request Factory

      * @return

      */

      private ContextAwareHttpComponentsClientHttpRequestFactory getRequestFactory() {

          HttpHost targetHost = new HttpHost("localhost", 8080, "http");

     

          DefaultHttpClient httpclient = new DefaultHttpClient();

     

          httpclient.getCredentialsProvider().setCredentials(

          new AuthScope(targetHost.getHostName(), targetHost.getPort()),

          new UsernamePasswordCredentials("admin", "admin"));

     

          // Create AuthCache instance

          AuthCache authCache = new BasicAuthCache();

          // Generate BASIC scheme object and add it to the local

          // auth cache

          BasicScheme basicAuth = new BasicScheme();

          authCache.put(targetHost, basicAuth);

     

          // Add AuthCache to the execution context

          BasicHttpContext localcontext = new BasicHttpContext();

          localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache);

     

          ContextAwareHttpComponentsClientHttpRequestFactory customFactory = new ContextAwareHttpComponentsClientHttpRequestFactory( httpclient);

          customFactory.setHttpContext(localcontext);

          return customFactory;

      }

     

     

    }

     

    ContextAwareHttpComponentsClientHttpRequestFactory.java

    import java.net.URI;

     

     

     

    import org.apache.http.client.HttpClient;

    import org.apache.http.protocol.HttpContext;

    import org.springframework.http.HttpMethod;

    import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;

     

     

     

    public class ContextAwareHttpComponentsClientHttpRequestFactory extends HttpComponentsClientHttpRequestFactory {

     

      private HttpContext httpContext;

     

      public ContextAwareHttpComponentsClientHttpRequestFactory(HttpClient httpClient){

          super(httpClient);

      }

     

        protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {

        //Ignoring the URI and method.

            return httpContext;

        }

     

     

      public void setHttpContext(HttpContext httpContext) {

          this.httpContext = httpContext;

      }

    }