use of org.apache.commons.httpclient.HttpException in project tdi-studio-se by Talend.
the class MDMTransaction method commit.
public void commit() throws IOException {
HttpClient client = new HttpClient();
client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
HttpMethod method = new PostMethod(url + "/" + id);
method.setDoAuthentication(true);
try {
//$NON-NLS-1$ //$NON-NLS-2$
method.setRequestHeader("Cookie", getStickySession() + "=" + sessionId);
client.executeMethod(method);
} catch (HttpException e) {
throw e;
} catch (IOException e) {
throw e;
} finally {
method.releaseConnection();
}
int statuscode = method.getStatusCode();
if (statuscode >= 400) {
throw new MDMTransactionException("Commit failed. The commit operation has returned the code " + statuscode + ".");
}
}
use of org.apache.commons.httpclient.HttpException in project tdi-studio-se by Talend.
the class SpagoBITalendEngineClient_0_5_0 method deployJob.
/*
* (non-Javadoc)
*
* @see
* it.eng.spagobi.engines.talend.client.ISpagoBITalendEngineClient#deployJob(it.eng.spagobi.engines.talend.client
* .JobDeploymentDescriptor, java.io.File)
*/
public boolean deployJob(JobDeploymentDescriptor jobDeploymentDescriptor, File executableJobFiles) throws EngineUnavailableException, AuthenticationFailedException, ServiceInvocationFailedException {
HttpClient client;
PostMethod method;
File deploymentDescriptorFile;
boolean result = false;
client = new HttpClient();
method = new PostMethod(getServiceUrl(JOB_UPLOAD_SERVICE));
deploymentDescriptorFile = null;
try {
//$NON-NLS-1$ //$NON-NLS-2$
deploymentDescriptorFile = File.createTempFile("deploymentDescriptor", ".xml");
FileWriter writer = new FileWriter(deploymentDescriptorFile);
writer.write(jobDeploymentDescriptor.toXml());
writer.flush();
writer.close();
Part[] parts = { new FilePart(executableJobFiles.getName(), executableJobFiles), //$NON-NLS-1$
new FilePart("deploymentDescriptor", deploymentDescriptorFile) };
method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
int status = client.executeMethod(method);
if (status == HttpStatus.SC_OK) {
if (//$NON-NLS-1$
method.getResponseBodyAsString().equalsIgnoreCase("OK"))
result = true;
} else {
throw new ServiceInvocationFailedException(Messages.getString("SpagoBITalendEngineClient_0_5_0.serviceExcFailed") + JOB_UPLOAD_SERVICE, //$NON-NLS-1$
method.getStatusLine().toString(), method.getResponseBodyAsString());
}
} catch (HttpException e) {
//$NON-NLS-1$
throw new EngineUnavailableException(Messages.getString("SpagoBITalendEngineClient_0_5_0.protocolViolation") + e.getMessage());
} catch (IOException e) {
//$NON-NLS-1$
throw new EngineUnavailableException(Messages.getString("SpagoBITalendEngineClient_0_5_0.transportError") + e.getMessage());
} finally {
method.releaseConnection();
if (deploymentDescriptorFile != null)
deploymentDescriptorFile.delete();
}
return result;
}
use of org.apache.commons.httpclient.HttpException in project zm-mailbox by Zimbra.
the class ElasticSearchIndexTest method indexStoreAvailable.
@Override
protected boolean indexStoreAvailable() {
String indexUrl = String.format("%s?_status", LC.zimbra_index_elasticsearch_url_base.value());
HttpMethod method = new GetMethod(indexUrl);
try {
ElasticSearchConnector connector = new ElasticSearchConnector();
connector.executeMethod(method);
} catch (HttpException e) {
ZimbraLog.index.error("Problem accessing the ElasticSearch Index store", e);
return false;
} catch (IOException e) {
ZimbraLog.index.error("Problem accessing the ElasticSearch Index store", e);
return false;
}
return true;
}
use of org.apache.commons.httpclient.HttpException in project bigbluebutton by bigbluebutton.
the class PresentationUrlDownloadService method savePresentation.
public boolean savePresentation(final String meetingId, final String filename, final String urlString) {
String finalUrl = followRedirect(meetingId, urlString, 0, urlString);
if (finalUrl == null)
return false;
boolean success = false;
GetMethod method = new GetMethod(finalUrl);
HttpClient client = new HttpClient();
try {
int statusCode = client.executeMethod(method);
if (statusCode == HttpStatus.SC_OK) {
FileUtils.copyInputStreamToFile(method.getResponseBodyAsStream(), new File(filename));
log.info("Downloaded presentation at [{}]", finalUrl);
success = true;
}
} catch (HttpException e) {
log.error("HttpException while downloading presentation at [{}]", finalUrl);
} catch (IOException e) {
log.error("IOException while downloading presentation at [{}]", finalUrl);
} finally {
method.releaseConnection();
}
return success;
}
use of org.apache.commons.httpclient.HttpException in project zm-mailbox by Zimbra.
the class ProxyServlet method doProxy.
private void doProxy(HttpServletRequest req, HttpServletResponse resp) throws IOException {
ZimbraLog.clearContext();
boolean isAdmin = isAdminRequest(req);
AuthToken authToken = isAdmin ? getAdminAuthTokenFromCookie(req, resp, true) : getAuthTokenFromCookie(req, resp, true);
if (authToken == null) {
String zAuthToken = req.getParameter(QP_ZAUTHTOKEN);
if (zAuthToken != null) {
try {
authToken = AuthProvider.getAuthToken(zAuthToken);
if (authToken.isExpired()) {
resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "authtoken expired");
return;
}
if (!authToken.isRegistered()) {
resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "authtoken is invalid");
return;
}
if (isAdmin && !authToken.isAdmin()) {
resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "permission denied");
return;
}
} catch (AuthTokenException e) {
resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "unable to parse authtoken");
return;
}
}
}
if (authToken == null) {
resp.sendError(HttpServletResponse.SC_UNAUTHORIZED, "no authtoken cookie");
return;
}
// get the posted body before the server read and parse them.
byte[] body = copyPostedData(req);
// sanity check
String target = req.getParameter(TARGET_PARAM);
if (target == null) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
// check for permission
URL url = new URL(target);
if (!isAdmin && !checkPermissionOnTarget(url, authToken)) {
resp.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
// determine whether to return the target inline or store it as an upload
String uploadParam = req.getParameter(UPLOAD_PARAM);
boolean asUpload = uploadParam != null && (uploadParam.equals("1") || uploadParam.equalsIgnoreCase("true"));
HttpMethod method = null;
try {
HttpClient client = ZimbraHttpConnectionManager.getExternalHttpConnMgr().newHttpClient();
HttpProxyUtil.configureProxy(client);
String reqMethod = req.getMethod();
if (reqMethod.equalsIgnoreCase("GET")) {
method = new GetMethod(target);
} else if (reqMethod.equalsIgnoreCase("POST")) {
PostMethod post = new PostMethod(target);
if (body != null)
post.setRequestEntity(new ByteArrayRequestEntity(body, req.getContentType()));
method = post;
} else if (reqMethod.equalsIgnoreCase("PUT")) {
PutMethod put = new PutMethod(target);
if (body != null)
put.setRequestEntity(new ByteArrayRequestEntity(body, req.getContentType()));
method = put;
} else if (reqMethod.equalsIgnoreCase("DELETE")) {
method = new DeleteMethod(target);
} else {
ZimbraLog.zimlet.info("unsupported request method: " + reqMethod);
resp.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
return;
}
// handle basic auth
String auth, user, pass;
auth = req.getParameter(AUTH_PARAM);
user = req.getParameter(USER_PARAM);
pass = req.getParameter(PASS_PARAM);
if (auth != null && user != null && pass != null) {
if (!auth.equals(AUTH_BASIC)) {
ZimbraLog.zimlet.info("unsupported auth type: " + auth);
resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
return;
}
HttpState state = new HttpState();
state.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, pass));
client.setState(state);
method.setDoAuthentication(true);
}
Enumeration headers = req.getHeaderNames();
while (headers.hasMoreElements()) {
String hdr = (String) headers.nextElement();
ZimbraLog.zimlet.debug("incoming: " + hdr + ": " + req.getHeader(hdr));
if (canProxyHeader(hdr)) {
ZimbraLog.zimlet.debug("outgoing: " + hdr + ": " + req.getHeader(hdr));
if (hdr.equalsIgnoreCase("x-host"))
method.getParams().setVirtualHost(req.getHeader(hdr));
else
method.addRequestHeader(hdr, req.getHeader(hdr));
}
}
try {
if (!(reqMethod.equalsIgnoreCase("POST") || reqMethod.equalsIgnoreCase("PUT"))) {
method.setFollowRedirects(true);
}
HttpClientUtil.executeMethod(client, method);
} catch (HttpException ex) {
ZimbraLog.zimlet.info("exception while proxying " + target, ex);
resp.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
int status = method.getStatusLine() == null ? HttpServletResponse.SC_INTERNAL_SERVER_ERROR : method.getStatusCode();
// workaround for Alexa Thumbnails paid web service, which doesn't bother to return a content-type line
Header ctHeader = method.getResponseHeader("Content-Type");
String contentType = ctHeader == null || ctHeader.getValue() == null ? DEFAULT_CTYPE : ctHeader.getValue();
InputStream targetResponseBody = method.getResponseBodyAsStream();
if (asUpload) {
String filename = req.getParameter(FILENAME_PARAM);
if (filename == null || filename.equals(""))
filename = new ContentType(contentType).getParameter("name");
if ((filename == null || filename.equals("")) && method.getResponseHeader("Content-Disposition") != null)
filename = new ContentDisposition(method.getResponseHeader("Content-Disposition").getValue()).getParameter("filename");
if (filename == null || filename.equals(""))
filename = "unknown";
List<Upload> uploads = null;
if (targetResponseBody != null) {
try {
Upload up = FileUploadServlet.saveUpload(targetResponseBody, filename, contentType, authToken.getAccountId());
uploads = Arrays.asList(up);
} catch (ServiceException e) {
if (e.getCode().equals(MailServiceException.UPLOAD_REJECTED))
status = HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE;
else
status = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
}
}
resp.setStatus(status);
FileUploadServlet.sendResponse(resp, status, req.getParameter(FORMAT_PARAM), null, uploads, null);
} else {
resp.setStatus(status);
resp.setContentType(contentType);
for (Header h : method.getResponseHeaders()) if (canProxyHeader(h.getName()))
resp.addHeader(h.getName(), h.getValue());
if (targetResponseBody != null)
ByteUtil.copy(targetResponseBody, true, resp.getOutputStream(), true);
}
} finally {
if (method != null)
method.releaseConnection();
}
}
Aggregations