use of org.apache.shindig.gadgets.http.HttpResponseBuilder in project liferay-ide by liferay.
the class OAuthRequest method fetchData.
/**
* Get honest-to-goodness user data.
*
* @throws OAuthProtocolException if the service provider returns an OAuth
* related error instead of user data.
*/
private HttpResponseBuilder fetchData() throws OAuthRequestException, OAuthProtocolException {
HttpResponseBuilder builder = null;
if (accessTokenData != null) {
// This is a request for access token data, return it.
builder = formatAccessTokenData();
} else {
HttpRequest signed = sanitizeAndSign(realRequest, null, false);
HttpResponse response = fetchFromServer(signed);
checkForProtocolProblem(response);
builder = new HttpResponseBuilder(response);
}
return builder;
}
use of org.apache.shindig.gadgets.http.HttpResponseBuilder in project liferay-ide by liferay.
the class OAuthRequest method formatAccessTokenData.
/**
* Access token data is returned to the gadget as json key/value pairs:
*
* { "user_id": "12345678" }
*/
private HttpResponseBuilder formatAccessTokenData() {
HttpResponseBuilder builder = new HttpResponseBuilder();
builder.addHeader("Content-Type", "application/json; charset=utf-8");
builder.setHttpStatusCode(HttpResponse.SC_OK);
// no need to cache this, these requests should be fairly rare, and the results should be
// cached in gadget.
builder.setStrictNoCache();
JSONObject json = new JSONObject(accessTokenData);
builder.setResponseString(json.toString());
return builder;
}
use of org.apache.shindig.gadgets.http.HttpResponseBuilder in project liferay-ide by liferay.
the class OAuthRequest method fetchWithRetry.
/**
* Fetch data, retrying in the event that that the service provider returns an error and we think
* we can recover by restarting the protocol flow.
*/
private HttpResponseBuilder fetchWithRetry() throws OAuthRequestException {
int attempts = 0;
boolean retry;
HttpResponseBuilder response = null;
do {
retry = false;
++attempts;
try {
response = attemptFetch();
} catch (OAuthProtocolException pe) {
retry = handleProtocolException(pe, attempts);
if (!retry) {
if (pe.getProblemCode() != null) {
throw new OAuthRequestException(pe.getProblemCode(), "Service provider rejected request", pe);
} else {
throw new OAuthRequestException(OAuthError.UNKNOWN_PROBLEM, "Service provider rejected request", pe);
}
}
}
} while (retry);
return response;
}
use of org.apache.shindig.gadgets.http.HttpResponseBuilder in project liferay-ide by liferay.
the class OAuthRequest method fetchNoThrow.
/**
* Fetch data and build a response to return to the client. We try to always return something
* reasonable to the calling app no matter what kind of madness happens along the way. If an
* unchecked exception occurs, well, then the client is out of luck.
*/
private HttpResponse fetchNoThrow() {
HttpResponseBuilder response = null;
try {
accessorInfo = fetcherConfig.getTokenStore().getOAuthAccessor(realRequest.getSecurityToken(), realRequest.getOAuthArguments(), clientState, responseParams, fetcherConfig);
response = fetchWithRetry();
} catch (OAuthRequestException e) {
// No data for us.
if (OAuthError.UNAUTHENTICATED.name().equals(e.getError())) {
responseParams.logDetailedInfo("Unauthenticated OAuth fetch", e);
} else if (OAuthError.BAD_OAUTH_TOKEN_URL.name().equals(e.getError())) {
responseParams.logDetailedInfo("Invalid OAuth fetch request", e);
} else {
responseParams.logDetailedWarning("OAuth fetch fatal error", e);
}
responseParams.setSendTraceToClient(true);
response = new HttpResponseBuilder().setHttpStatusCode(HttpResponse.SC_FORBIDDEN).setStrictNoCache();
responseParams.addToResponse(response, e);
return response.create();
}
// OK, got some data back, annotate it as necessary.
if (response.getHttpStatusCode() >= 400) {
responseParams.logDetailedWarning("OAuth fetch fatal error");
responseParams.setSendTraceToClient(true);
} else if (responseParams.getAznUrl() != null && responseParams.sawErrorResponse()) {
responseParams.logDetailedWarning("OAuth fetch error, reprompting for user approval");
responseParams.setSendTraceToClient(true);
}
responseParams.addToResponse(response, null);
return response.create();
}
Aggregations