com.marvelution.hudson.plugins.apiv2.client.HudsonClient Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hudson-apiv2-wsclient Show documentation
Show all versions of hudson-apiv2-wsclient Show documentation
This library can be used to connect to the new REST API.
/*
* Licensed to Marvelution under one or more contributor license
* agreements. See the NOTICE file distributed with this work
* for additional information regarding copyright ownership.
* Marvelution licenses this file to you 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 com.marvelution.hudson.plugins.apiv2.client;
import java.util.List;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import com.marvelution.hudson.plugins.apiv2.client.connectors.Connector;
import com.marvelution.hudson.plugins.apiv2.client.connectors.ConnectorFactory;
import com.marvelution.hudson.plugins.apiv2.client.connectors.ConnectorResponse;
import com.marvelution.hudson.plugins.apiv2.client.services.ListableQuery;
import com.marvelution.hudson.plugins.apiv2.client.services.Query;
import com.marvelution.hudson.plugins.apiv2.client.unmarshallers.Unmarshallers;
import com.marvelution.hudson.plugins.apiv2.client.Host;
import com.marvelution.hudson.plugins.apiv2.resources.model.ListableModel;
import com.marvelution.hudson.plugins.apiv2.resources.model.Model;
/**
* Client implementation for connecting to the Hudson API version 2 plugin
*
* By default the client implementation will use the commons-httpclient for the connection between the client and the server.
* To instantiate a HudsonClient using commons-httpclient
*
import com.marvelution.hudson.plugins.apiv2.client.HudsonClient;
HudsonClient hudson = HudsonClient.create("http://localhost:8080");
*
* If the security is enabled, you have to define the credentials:
*
HudsonClient hudson = HudsonClient.create("http://localhost:8080", "login", "password");
*
* When using the lib Commons HttpClient 4.0, the constructor is slightly different:
*
HudsonClient hudson = new HudsonClient(new HttpClient4Connector(new Host("http://localhost:8080")));
*
*
* @author Mark Rekveld
*/
public class HudsonClient {
private Connector connector;
/**
* Default Constructor
*
* @param connector the {@link Connector} implementation
*/
public HudsonClient(Connector connector) {
this.connector = connector;
}
/**
* Getter for the {@link Connector}
*
* @return the {@link Connector} implementation
*/
public Connector getConnector() {
return connector;
}
/**
* Method to find a {@link Model} type using its {@link Query}
*
* @param the {@link Model} type
* @param query the {@link Query} implementation for the {@link Model} type
* @return the {@link Model} response from the Hudson server, may be null
* @throws ClientException in case of unmarshalling exceptions
*/
@SuppressWarnings("unchecked")
public MODEL find(Query query) throws ClientException {
ConnectorResponse response = connector.execute(query);
if (response != null) {
try {
Unmarshaller unmarshaller = Unmarshallers.forModel(query.getModelClass());
return (MODEL) unmarshaller.unmarshal(response.getResponseAsStream());
} catch (JAXBException e) {
throw new ClientException("Failed to unmarshal Hudson response. Original Request: "
+ query.getUrl(), e, response);
}
}
return null;
}
/**
* Method to find all the {@link Model} types using its {@link Query}
*
* @param the {@link Model} type
* @param query the {@link Query} implementation for the {@link Model} type
* @return the {@link List} of {@link Model} objects from the response from the Hudson server, may be
* null
or an empty
{@link List}
* @throws ClientException in case of unmarshalling exceptions
*/
@SuppressWarnings("unchecked")
public > LISTMODEL findAll(ListableQuery query) throws ClientException {
ConnectorResponse response = connector.execute(query);
if (response != null) {
try {
Unmarshaller unmarshaller = Unmarshallers.forModel(query.getListableModelClass());
return (LISTMODEL) unmarshaller.unmarshal(response.getResponseAsStream());
} catch (JAXBException e) {
throw new ClientException("Failed to unmarshal Hudson response. Original Request: "
+ query.getUrl(), e, response);
}
}
return null;
}
/**
* Static method to create a {@link HudsonClient} for the unsecured host base-url given using a
* Commons-HttpClient version 3 connector
*
* @param host the host base-url to connect to
* @return the {@link HudsonClient} object
*/
public static HudsonClient create(String host) {
return new HudsonClient(ConnectorFactory.create(new Host(host)));
}
/**
* Static method to create a {@link HudsonClient} for the secured host base-url given using a Commons-HttpClient version 3 connector
*
* @param host the host base-url to connect to
* @param username the username
* @param password the password
* @return the {@link HudsonClient} object
*/
public static HudsonClient create(String host, String username, String password) {
return new HudsonClient(ConnectorFactory.create(new Host(host, username, password)));
}
}