use of javax.xml.ws.http.HTTPException in project Rubicon by Rubicon-Bot.
the class Bitly method shorten.
/**
* Shortens a URL with bit.ly.
*
* @param longURL the URL to shorten.
* @return the shortened URL.
* @throws IllegalArgumentException if the long URI was invalid.
* @throws HTTPException if the bit.ly API returns a response code unlike 200 'OK'.
* @throws RuntimeException if the http request threw an unknown error.
*/
public static String shorten(String longURL) {
HttpRequestBuilder request = new HttpRequestBuilder(API_URL, RequestType.GET);
request.addParameter("access_token", Info.BITLY_TOKEN);
request.addParameter("longUrl", longURL);
request.addParameter("format", "json");
RequestResponse result;
try {
result = request.sendRequest();
} catch (Exception e) {
// catch 'anonymous' exceptions
throw new RuntimeException("An unknown exception occurred while fetching a bit.ly http request", e);
}
JSONObject response = new JSONObject(result.getResponseMessage());
// check if uri was valid
if (response.getString("status_txt").equals("INVALID_URI"))
throw new IllegalArgumentException("'" + longURL + "' is not a valid URL.");
else // ensure 'OK' status response
if (response.getInt("status_code") == 400)
throw new HTTPException(response.getInt("status_code"));
// return shortened url
return response.getJSONObject("data").getString("url");
}
use of javax.xml.ws.http.HTTPException in project scout.rt by eclipse.
the class AuthenticationHandler method handleMessage.
@Override
@SuppressWarnings("squid:S1181")
public boolean handleMessage(final SOAPMessageContext messageContext) {
if (MessageContexts.isOutboundMessage(messageContext)) {
return true;
}
try {
final HttpServletRequest servletRequest = Assertions.assertNotNull(m_implementorSpecifics.getServletRequest(messageContext), "ServletRequest must not be null");
// Check whether the request was pre-authenticated, e.g. by application server or Servlet filter.
final Subject preAuthSubject = BEANS.get(PreAuthenticationMethod.class).getRequestSubject(servletRequest, m_principalProducer);
if (preAuthSubject != null) {
MessageContexts.putRunContext(messageContext, m_runContextProducer.produce(preAuthSubject));
return true;
}
// Authenticate the request.
final Principal principal = authenticateRequest(messageContext);
if (principal != null) {
final Subject subject = BEANS.get(ServletFilterHelper.class).createSubject(principal);
BEANS.get(ServletFilterHelper.class).putPrincipalOnSession(servletRequest, principal);
MessageContexts.putRunContext(messageContext, m_runContextProducer.produce(subject));
return true;
}
// Ensure HTTP status code to be set.
final int httpStatusCode;
final Integer currentHttpResponseCode = m_implementorSpecifics.getHttpResponseCode(messageContext);
if (currentHttpResponseCode != null) {
httpStatusCode = currentHttpResponseCode.intValue();
} else {
httpStatusCode = HttpServletResponse.SC_FORBIDDEN;
m_implementorSpecifics.setHttpResponseCode(messageContext, httpStatusCode);
}
m_implementorSpecifics.interceptWebServiceRequestRejected(messageContext, httpStatusCode);
return false;
} catch (final WebServiceRequestRejectedException e) {
// NOSONAR
throw new HTTPException(e.getHttpStatusCode());
} catch (final Throwable t) {
m_implementorSpecifics.setHttpResponseCode(messageContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
// Log exception with information about the webservice request associated.
BEANS.get(ExceptionHandler.class).handle(BEANS.get(PlatformExceptionTranslator.class).translate(t).withContextInfo("service", messageContext.get(SOAPMessageContext.WSDL_SERVICE)).withContextInfo("port", messageContext.get(SOAPMessageContext.WSDL_PORT)).withContextInfo("operation", messageContext.get(SOAPMessageContext.WSDL_OPERATION)));
try {
m_implementorSpecifics.interceptWebServiceRequestRejected(messageContext, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
} catch (final WebServiceRequestRejectedException e) {
// SECURITY: Do not propagate cause to the caller.
throw new HTTPException(e.getHttpStatusCode());
}
// SECURITY: Do not propagate cause to the caller.
return false;
}
}
use of javax.xml.ws.http.HTTPException in project hbase by apache.
the class RESTApiClusterManager method getJsonNodeFromURIGet.
// Execute GET against URI, returning a JsonNode object to be traversed.
private JsonNode getJsonNodeFromURIGet(URI uri) throws IOException {
LOG.info("Executing GET against " + uri + "...");
ClientResponse response = client.resource(uri).accept(MediaType.APPLICATION_JSON_TYPE).get(ClientResponse.class);
int statusCode = response.getStatus();
if (statusCode != Response.Status.OK.getStatusCode()) {
throw new HTTPException(statusCode);
}
// This API folds information as the value to an "items" attribute.
return new ObjectMapper().readTree(response.getEntity(String.class)).get("items");
}
use of javax.xml.ws.http.HTTPException in project cxf by apache.
the class DispatchImpl method mapException.
private RuntimeException mapException(Exception ex) {
if (ex instanceof Fault && ex.getCause() instanceof IOException) {
throw new WebServiceException(ex.getMessage(), ex.getCause());
}
if (getBinding() instanceof HTTPBinding) {
HTTPException exception = new HTTPException(HttpURLConnection.HTTP_INTERNAL_ERROR);
exception.initCause(ex);
return exception;
} else if (getBinding() instanceof SOAPBinding) {
SOAPFault soapFault = null;
try {
soapFault = JaxWsClientProxy.createSoapFault((SOAPBinding) getBinding(), ex);
} catch (SOAPException e) {
// ignore
}
if (soapFault == null) {
return new WebServiceException(ex);
}
SOAPFaultException exception = new SOAPFaultException(soapFault);
exception.initCause(ex);
return exception;
}
return new WebServiceException(ex);
}
use of javax.xml.ws.http.HTTPException in project cxf by apache.
the class JaxWsClientProxy method mapException.
Exception mapException(Method method, BindingOperationInfo boi, Exception ex) {
if (method != null) {
for (Class<?> excls : method.getExceptionTypes()) {
if (excls.isInstance(ex)) {
return ex;
}
}
} else if (boi != null) {
for (BindingFaultInfo fi : boi.getFaults()) {
Class<?> c = fi.getFaultInfo().getProperty(Class.class.getName(), Class.class);
if (c != null && c.isInstance(ex)) {
return ex;
}
}
if (ex instanceof IOException) {
return ex;
}
}
if (ex instanceof Fault && ex.getCause() instanceof IOException) {
return new WebServiceException(ex.getMessage(), ex.getCause());
}
if (getBinding() instanceof HTTPBinding) {
HTTPException exception = new HTTPException(HttpURLConnection.HTTP_INTERNAL_ERROR);
exception.initCause(ex);
return exception;
} else if (getBinding() instanceof SOAPBinding) {
try {
SOAPFault soapFault = createSoapFault((SOAPBinding) getBinding(), ex);
if (soapFault == null) {
throw new WebServiceException(ex);
}
SOAPFaultException exception = new SOAPFaultException(soapFault);
if (ex instanceof Fault && ex.getCause() != null) {
exception.initCause(ex.getCause());
} else {
exception.initCause(ex);
}
return exception;
} catch (SOAPException e) {
return new WebServiceException(ex);
}
}
return new WebServiceException(ex);
}
Aggregations