All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.roboconf.dm.rest.client.WsClient Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2014-2017 Linagora, Université Joseph Fourier, Floralis
 *
 * The present code is developed in the scope of the joint LINAGORA -
 * Université Joseph Fourier - Floralis research program and is designated
 * as a "Result" pursuant to the terms and conditions of the LINAGORA
 * - Université Joseph Fourier - Floralis research program. Each copyright
 * holder of Results enumerated here above fully & independently holds complete
 * ownership of the complete Intellectual Property rights applicable to the whole
 * of said Results, and may freely exploit it in any manner which does not infringe
 * the moral rights of the other copyright holders.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.roboconf.dm.rest.client;

import javax.ws.rs.core.Cookie;

import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;

import net.roboconf.dm.rest.client.delegates.ApplicationWsDelegate;
import net.roboconf.dm.rest.client.delegates.AuthenticationWsDelegate;
import net.roboconf.dm.rest.client.delegates.DebugWsDelegate;
import net.roboconf.dm.rest.client.delegates.ManagementWsDelegate;
import net.roboconf.dm.rest.client.delegates.PreferencesWsDelegate;
import net.roboconf.dm.rest.client.delegates.SchedulerWsDelegate;
import net.roboconf.dm.rest.client.delegates.TargetWsDelegate;
import net.roboconf.dm.rest.commons.UrlConstants;
import net.roboconf.dm.rest.commons.json.ObjectMapperProvider;

/**
 * A client for the REST API of the Deployment Manager.
 * 

* This client is configured for the REST implementations that use the predefined * PATH constants. Users should call {@link #destroy()} to release the connection. *

*

* The following rules apply to the client implementation. *

*
    *
  • Methods that return a list do not throw exceptions.
  • *
  • Methods that return a list never return null.
  • *
  • Methods that return a list only expected 2xx responses from the server.
  • *
  • Methods that return an object throw an exception if they did not receive a 2xx response.
  • *
  • Methods that return an object return null if they did not receive a 200 response.
  • *
  • Methods that do not return anything throw an exception if they did not receive a 2xx response.
  • *
*

* Thrown exceptions contain the error code and the error message. *

*

* About the logging policy...
* Every client method logs an entry when it is invoked. * It logs a second entry once the REST invocation has completed, and provided a runtime * exception was not thrown by the REST library. *

*

* The second log entry either logs the response code (success), or logs the response * code and the response message (error in the result). *

* * @author Vincent Zurczak - Linagora */ public class WsClient { private final ApplicationWsDelegate applicationDelegate; private final ManagementWsDelegate managementDelegate; private final DebugWsDelegate debugDelegate; private final TargetWsDelegate targetWsDelegate; private final SchedulerWsDelegate schedulerDelegate; private final PreferencesWsDelegate preferencesWsDelegate; private final AuthenticationWsDelegate authenticationWsDelegate; private final Client client; private String sessionId; /** * Constructor. * @param rootUrl the root URL (example: http://192.168.1.18:9007/dm/) */ public WsClient( String rootUrl ) { ClientConfig cc = new DefaultClientConfig(); cc.getClasses().add( JacksonJsonProvider.class ); cc.getClasses().add( ObjectMapperProvider.class ); this.client = Client.create( cc ); this.client.setFollowRedirects( true ); WebResource resource = this.client.resource( rootUrl ); this.applicationDelegate = new ApplicationWsDelegate( resource, this ); this.managementDelegate = new ManagementWsDelegate( resource, this ); this.debugDelegate = new DebugWsDelegate( resource, this ); this.targetWsDelegate = new TargetWsDelegate( resource, this ); this.schedulerDelegate = new SchedulerWsDelegate( resource, this ); this.preferencesWsDelegate = new PreferencesWsDelegate( resource, this ); this.authenticationWsDelegate = new AuthenticationWsDelegate( resource, this ); } /** * Force the destruction of the JAX-RS client. */ public void destroy() { this.client.destroy(); } /** * @return the applicationDelegate */ public ApplicationWsDelegate getApplicationDelegate() { return this.applicationDelegate; } /** * @return the appManagementDelegate */ public ManagementWsDelegate getManagementDelegate() { return this.managementDelegate; } /** * @return the debugDelegate */ public DebugWsDelegate getDebugDelegate() { return this.debugDelegate; } /** * @return the targetWsDelegate */ public TargetWsDelegate getTargetWsDelegate() { return this.targetWsDelegate; } /** * @return the schedulerDelegate */ public SchedulerWsDelegate getSchedulerDelegate() { return this.schedulerDelegate; } /** * @return the preferencesWsDelegate */ public PreferencesWsDelegate getPreferencesWsDelegate() { return this.preferencesWsDelegate; } /** * @return the authenticationWsDelegate */ public AuthenticationWsDelegate getAuthenticationWsDelegate() { return this.authenticationWsDelegate; } /** * @return the Jersey client (useful to configure it) */ public Client getJerseyClient() { return this.client; } /** * Sets the session ID to use when authenticated. * @param sessionId */ public void setSessionId( String sessionId ) { this.sessionId = sessionId; } /** * @return the sessionId */ public String getSessionId() { return this.sessionId; } /** * @param resource a web resource * @return a builder with an optional cookie (for authentication) */ public WebResource.Builder createBuilder( WebResource resource ) { WebResource.Builder result; if( this.sessionId != null ) result = resource.cookie( new Cookie( UrlConstants.SESSION_ID, this.sessionId )); else result = resource.getRequestBuilder(); return result; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy