use of org.apache.commons.httpclient.HttpException in project cloudstack by apache.
the class RegionsApiUtil method makeDomainAPICall.
/**
* Makes an api call using region service end_point, api command and params
* Returns Domain object on success
* @param region
* @param command
* @param params
* @return
*/
protected static RegionDomain makeDomainAPICall(Region region, String command, List<NameValuePair> params) {
try {
String url = buildUrl(buildParams(command, params), region);
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
if (client.executeMethod(method) == 200) {
InputStream is = method.getResponseBodyAsStream();
XStream xstream = new XStream(new DomDriver());
//Translate response to Domain object
xstream.alias("domain", RegionDomain.class);
xstream.aliasField("id", RegionDomain.class, "uuid");
xstream.aliasField("parentdomainid", RegionDomain.class, "parentUuid");
xstream.aliasField("networkdomain", DomainVO.class, "networkDomain");
try (ObjectInputStream in = xstream.createObjectInputStream(is)) {
return (RegionDomain) in.readObject();
} catch (IOException e) {
s_logger.error(e.getMessage());
return null;
}
} else {
return null;
}
} catch (HttpException e) {
s_logger.error(e.getMessage());
return null;
} catch (IOException e) {
s_logger.error(e.getMessage());
return null;
} catch (ClassNotFoundException e) {
s_logger.error(e.getMessage());
return null;
}
}
use of org.apache.commons.httpclient.HttpException in project cloudstack by apache.
the class RegionsApiUtil method makeUserAccountAPICall.
/**
* Makes an api call using region service end_point, api command and params
* Returns UserAccount object on success
* @param region
* @param command
* @param params
* @return
*/
protected static UserAccount makeUserAccountAPICall(Region region, String command, List<NameValuePair> params) {
try {
String url = buildUrl(buildParams(command, params), region);
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
if (client.executeMethod(method) == 200) {
InputStream is = method.getResponseBodyAsStream();
XStream xstream = new XStream(new DomDriver());
xstream.alias("useraccount", UserAccountVO.class);
xstream.aliasField("id", UserAccountVO.class, "uuid");
try (ObjectInputStream in = xstream.createObjectInputStream(is)) {
return (UserAccountVO) in.readObject();
} catch (IOException e) {
s_logger.error(e.getMessage());
return null;
}
} else {
return null;
}
} catch (HttpException e) {
s_logger.error(e.getMessage());
return null;
} catch (IOException e) {
s_logger.error(e.getMessage());
return null;
} catch (ClassNotFoundException e) {
s_logger.error(e.getMessage());
return null;
}
}
use of org.apache.commons.httpclient.HttpException in project cloudstack by apache.
the class RegionsApiUtil method makeAccountAPICall.
/**
* Makes an api call using region service end_point, api command and params
* Returns Account object on success
* @param region
* @param command
* @param params
* @return
*/
protected static RegionAccount makeAccountAPICall(Region region, String command, List<NameValuePair> params) {
try {
String url = buildUrl(buildParams(command, params), region);
HttpClient client = new HttpClient();
HttpMethod method = new GetMethod(url);
if (client.executeMethod(method) == 200) {
InputStream is = method.getResponseBodyAsStream();
//Translate response to Account object
XStream xstream = new XStream(new DomDriver());
xstream.alias("account", RegionAccount.class);
xstream.alias("user", RegionUser.class);
xstream.aliasField("id", RegionAccount.class, "uuid");
xstream.aliasField("name", RegionAccount.class, "accountName");
xstream.aliasField("accounttype", RegionAccount.class, "type");
xstream.aliasField("domainid", RegionAccount.class, "domainUuid");
xstream.aliasField("networkdomain", RegionAccount.class, "networkDomain");
xstream.aliasField("id", RegionUser.class, "uuid");
xstream.aliasField("accountId", RegionUser.class, "accountUuid");
try (ObjectInputStream in = xstream.createObjectInputStream(is)) {
return (RegionAccount) in.readObject();
} catch (IOException e) {
s_logger.error(e.getMessage());
return null;
}
} else {
return null;
}
} catch (HttpException e) {
s_logger.error(e.getMessage());
return null;
} catch (IOException e) {
s_logger.error(e.getMessage());
return null;
} catch (ClassNotFoundException e) {
s_logger.error(e.getMessage());
return null;
}
}
use of org.apache.commons.httpclient.HttpException in project maven-plugins by apache.
the class ClassicJiraDownloader method download.
/**
* Downloads the given link using the configured HttpClient, possibly following redirects.
*
* @param cl the HttpClient
* @param link the URL to JIRA
*/
private void download(final HttpClient cl, final String link) {
InputStream in = null;
OutputStream out = null;
try {
GetMethod gm = new GetMethod(link);
getLog().info("Downloading from JIRA at: " + link);
gm.setFollowRedirects(true);
cl.executeMethod(gm);
StatusLine sl = gm.getStatusLine();
if (sl == null) {
getLog().error("Unknown error validating link: " + link);
return;
}
// if we get a redirect, do so
if (gm.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
Header locationHeader = gm.getResponseHeader("Location");
if (locationHeader == null) {
getLog().warn("Site sent redirect, but did not set Location header");
} else {
String newLink = locationHeader.getValue();
getLog().debug("Following redirect to " + newLink);
download(cl, newLink);
}
}
if (gm.getStatusCode() == HttpStatus.SC_OK) {
in = gm.getResponseBodyAsStream();
if (!output.getParentFile().exists()) {
output.getParentFile().mkdirs();
}
// write the response to file
out = new FileOutputStream(output);
IOUtil.copy(in, out);
out.close();
out = null;
in.close();
in = null;
getLog().debug("Downloading from JIRA was successful");
} else {
getLog().warn("Downloading from JIRA failed. Received: [" + gm.getStatusCode() + "]");
}
} catch (HttpException e) {
if (getLog().isDebugEnabled()) {
getLog().error("Error downloading issues from JIRA:", e);
} else {
getLog().error("Error downloading issues from JIRA url: " + e.getLocalizedMessage());
}
} catch (IOException e) {
if (getLog().isDebugEnabled()) {
getLog().error("Error downloading issues from JIRA:", e);
} else {
getLog().error("Error downloading issues from JIRA. Cause is " + e.getLocalizedMessage());
}
} finally {
IOUtil.close(out);
IOUtil.close(in);
}
}
use of org.apache.commons.httpclient.HttpException in project sling by apache.
the class FsMountHelper method getCurrentConfigurations.
/**
* Return all file provider configs for this project
* @param targetUrl The targetUrl of the webconsole
* @return A map (may be empty) with the pids as keys and the configurations as values
* @throws MojoExecutionException
*/
public Map<String, FsResourceConfiguration> getCurrentConfigurations(final String targetUrl) throws MojoExecutionException {
log.debug("Getting current file provider configurations.");
final Map<String, FsResourceConfiguration> result = new HashMap<>();
final String getUrl = targetUrl + "/configMgr/(service.factoryPid=" + FS_FACTORY + ").json";
final GetMethod get = new GetMethod(getUrl);
try {
final int status = httpClient.executeMethod(get);
if (status == 200) {
String contentType = get.getResponseHeader(HEADER_CONTENT_TYPE).getValue();
int pos = contentType.indexOf(';');
if (pos != -1) {
contentType = contentType.substring(0, pos);
}
if (!JsonSupport.JSON_MIME_TYPE.equals(contentType)) {
log.debug("Response type from web console is not JSON, but " + contentType);
throw new MojoExecutionException("The Apache Felix Web Console is too old to mount " + "the initial content through file system provider configs. " + "Either upgrade the web console or disable this feature.");
}
final String jsonText;
try (InputStream jsonResponse = get.getResponseBodyAsStream()) {
jsonText = IOUtils.toString(jsonResponse, CharEncoding.UTF_8);
}
try {
JsonArray array = JsonSupport.parseArray(jsonText);
for (int i = 0; i < array.size(); i++) {
final JsonObject obj = array.getJsonObject(i);
final String pid = obj.getString("pid");
final JsonObject properties = obj.getJsonObject("properties");
final String fsmode = getConfigPropertyValue(properties, PROPERTY_FSMODE);
final String path = getConfigPropertyValue(properties, PROPERTY_PATH);
final String initialContentImportOptions = getConfigPropertyValue(properties, PROPERTY_INITIAL_CONTENT_IMPORT_OPTIONS);
final String fileVaultFilterXml = getConfigPropertyValue(properties, PROPERTY_FILEVAULT_FILTER_XML);
String root = getConfigPropertyValue(properties, PROPERTY_ROOTS);
if (root == null) {
root = getConfigPropertyValue(properties, PROPERTY_ROOT);
}
if (path != null && path.startsWith(this.project.getBasedir().getAbsolutePath()) && root != null) {
FsResourceConfiguration cfg = new FsResourceConfiguration().fsMode(fsmode).providerRootPath(path).contentRootDir(root).initialContentImportOptions(initialContentImportOptions).fileVaultFilterXml(fileVaultFilterXml);
log.debug("Found configuration with pid: " + pid + ", " + cfg);
result.put(pid, cfg);
}
}
} catch (JsonException ex) {
throw new MojoExecutionException("Reading configuration from " + getUrl + " failed, cause: " + ex.getMessage(), ex);
}
}
} catch (HttpException ex) {
throw new MojoExecutionException("Reading configuration from " + getUrl + " failed, cause: " + ex.getMessage(), ex);
} catch (IOException ex) {
throw new MojoExecutionException("Reading configuration from " + getUrl + " failed, cause: " + ex.getMessage(), ex);
} finally {
get.releaseConnection();
}
return result;
}
Aggregations