org.restlet.Client Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.servicemix.bundles.restlet
Show all versions of org.apache.servicemix.bundles.restlet
This OSGi bundle wraps org.restlet, and com.noelios.restlet ${pkgVersion} jar files.
The newest version!
/**
* Copyright 2005-2008 Noelios Technologies.
*
* The contents of this file are subject to the terms of the following open
* source licenses: LGPL 3.0 or LGPL 2.1 or CDDL 1.0 (the "Licenses"). You can
* select the license that you prefer but you may not use this file except in
* compliance with one of these Licenses.
*
* You can obtain a copy of the LGPL 3.0 license at
* http://www.gnu.org/licenses/lgpl-3.0.html
*
* You can obtain a copy of the LGPL 2.1 license at
* http://www.gnu.org/licenses/lgpl-2.1.html
*
* You can obtain a copy of the CDDL 1.0 license at
* http://www.sun.com/cddl/cddl.html
*
* See the Licenses for the specific language governing permissions and
* limitations under the Licenses.
*
* Alternatively, you can obtain a royaltee free commercial license with less
* limitations, transferable or non-transferable, directly at
* http://www.noelios.com/products/restlet-engine
*
* Restlet is a registered trademark of Noelios Technologies.
*/
package org.restlet;
import java.util.Arrays;
import java.util.List;
import org.restlet.data.Protocol;
import org.restlet.data.Request;
import org.restlet.data.Response;
import org.restlet.util.Engine;
import org.restlet.util.Helper;
/**
* Connector acting as a generic client. It internally uses one of the available
* connector helpers registered with the Restlet engine.
*
* Concurrency note: instances of this class or its subclasses can be invoked by
* several threads at the same time and therefore must be thread-safe. You
* should be especially careful when storing state in member variables.
*
* @author Jerome Louvel
*/
public class Client extends Connector {
/**
* The number of milliseconds the client should wait for a response before
* aborting the request and setting its status to an error status.
*/
private volatile int connectTimeout = 0;
/** The helper provided by the implementation. */
private volatile Helper helper;
/**
* Constructor.
*
* @param context
* The context.
* @param protocols
* The connector protocols.
*/
public Client(Context context, List protocols) {
this(context, protocols, null);
}
/**
* Constructor.
*
* @param context
* The context.
* @param protocols
* The connector protocols.
* @param helperClass
* Optional helper class name.
*/
public Client(Context context, List protocols, String helperClass) {
super(context, protocols);
if ((protocols != null) && (protocols.size() > 0)) {
if (Engine.getInstance() != null) {
this.helper = Engine.getInstance().createHelper(this,
helperClass);
}
}
}
/**
* Constructor.
*
* @param context
* The context.
* @param protocol
* The connector protocol.
*/
public Client(Context context, Protocol protocol) {
this(context, (protocol == null) ? null : Arrays.asList(protocol), null);
}
/**
* Constructor.
*
* @param protocols
* The connector protocols.
*/
public Client(List protocols) {
this(null, protocols, null);
}
/**
* Constructor.
*
* @param protocol
* The connector protocol.
*/
public Client(Protocol protocol) {
this(null, protocol);
}
/**
* Constructor.
*
* @param protocolName
* The connector protocol.
*/
public Client(String protocolName) {
this(Protocol.valueOf(protocolName));
}
/**
* Returns the connection timeout in milliseconds. The default value is 0,
* meaning an infinite timeout.
*
* @return The connection timeout.
*/
public int getConnectTimeout() {
return this.connectTimeout;
}
/**
* Returns the helper provided by the implementation.
*
* @return The helper provided by the implementation.
*/
private Helper getHelper() {
return this.helper;
}
@Override
public void handle(Request request, Response response) {
super.handle(request, response);
if (getHelper() != null) {
getHelper().handle(request, response);
}
}
/**
* Sets the connection timeout in milliseconds. The default value is 0,
* meaning an infinite timeout.
*
* @param connectTimeout
* The connection timeout.
*/
public void setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
}
@Override
public synchronized void start() throws Exception {
if (isStopped()) {
super.start();
if (getHelper() != null) {
getHelper().start();
}
}
}
@Override
public synchronized void stop() throws Exception {
if (isStarted()) {
if (getHelper() != null) {
getHelper().stop();
}
super.stop();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy