Java Code Examples for org.scribe.model.Response#getCode()

The following examples show how to use org.scribe.model.Response#getCode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResourceClient.java    From jerseyoauth2 with MIT License 7 votes vote down vote up
public SampleEntity retrieveEntitySample1(Token accessToken) throws ClientException
{
	OAuthService service = getOAuthService(scopes, null);
	
	OAuthRequest request = new OAuthRequest(Verb.GET,
			"http://localhost:9998/testsuite/rest/sample/1");
	service.signRequest(accessToken, request);
	Response response = request.send();
	if (response.getCode()!=200)
		throwClientException(response);
	
	ObjectMapper mapper = new ObjectMapper();
	try {
		SampleEntity entity = mapper.readValue(response.getBody(), SampleEntity.class);
		return entity;
	} catch (IOException e) {
		throw new ClientException(response.getBody());
	}
}
 
Example 2
Source File: SmugMugInterface.java    From data-transfer-project with Apache License 2.0 6 votes vote down vote up
private <T> SmugMugResponse<T> makeRequest(
    String url, TypeReference<SmugMugResponse<T>> typeReference) throws IOException {
  // Note: there are no request params that need to go here, because smugmug fully specifies
  // which resource to get in the URL of a request, without using query params.
  String fullUrl;
  if (!url.contains("https://")) {
    fullUrl = BASE_URL + url;
  } else {
    fullUrl = url;
  }
  OAuthRequest request = new OAuthRequest(Verb.GET, fullUrl + "?_accept=application%2Fjson");
  oAuthService.signRequest(accessToken, request);
  final Response response = request.send();

  if (response.getCode() < 200 || response.getCode() >= 300) {
    throw new IOException(
        String.format("Error occurred in request for %s : %s", url, response.getMessage()));
  }

  return mapper.readValue(response.getBody(), typeReference);
}
 
Example 3
Source File: XeroClient.java    From xero-java-client with Apache License 2.0 6 votes vote down vote up
protected XeroApiException newApiException(Response response) {
  ApiException exception = null;
  try {
    exception = unmarshallResponse(response.getBody(), ApiException.class);
  } catch (Exception e) {
  }
  // Jibx doesn't support xsi:type, so we pull out errors this somewhat-hacky way
  Matcher matcher = MESSAGE_PATTERN.matcher(response.getBody());
  StringBuilder messages = new StringBuilder();
  while (matcher.find()) {
    if (messages.length() > 0) {
      messages.append(", ");
    }
    messages.append(matcher.group(1));
  }
  if (exception == null) {
    if (messages.length() > 0) {
      return new XeroApiException(response.getCode(), messages.toString());
    }
    return new XeroApiException(response.getCode());
  }
  return new XeroApiException(response.getCode(), "Error number " + exception.getErrorNumber() + ". " + messages);
}
 
Example 4
Source File: XeroClient.java    From xero-java-client with Apache License 2.0 6 votes vote down vote up
protected com.connectifier.xeroclient.models.Response get(String endPoint, Date modifiedAfter, Map<String,String> params) {
  OAuthRequest request = new OAuthRequest(Verb.GET, BASE_URL + endPoint);
  if (modifiedAfter != null) {
    request.addHeader("If-Modified-Since", utcFormatter.format(modifiedAfter));
  }
  if (params != null) {
    for (Map.Entry<String,String> param : params.entrySet()) {
      request.addQuerystringParameter(param.getKey(), param.getValue());
    }
  }
  service.signRequest(token, request);
  Response response = request.send();
  if (response.getCode() != 200) {
    throw newApiException(response);
  }
  return unmarshallResponse(response.getBody(), com.connectifier.xeroclient.models.Response.class);
}
 
Example 5
Source File: RequestBuilder.java    From jumblr with Apache License 2.0 6 votes vote down vote up
ResponseWrapper clear(Response response) {
    if (response.getCode() == 200 || response.getCode() == 201) {
        String json = response.getBody();
        try {
            Gson gson = new GsonBuilder().
                    registerTypeAdapter(JsonElement.class, new JsonElementDeserializer()).
                    create();
            ResponseWrapper wrapper = gson.fromJson(json, ResponseWrapper.class);
            if (wrapper == null) {
                throw new JumblrException(response);
            }
            wrapper.setClient(client);
            return wrapper;
        } catch (JsonSyntaxException ex) {
            throw new JumblrException(response);
        }
    } else {
        throw new JumblrException(response);
    }
}
 
Example 6
Source File: JumblrException.java    From jumblr with Apache License 2.0 6 votes vote down vote up
/**
 * Instantiate a new JumblrException given a bad response to wrap
 * @param response the response to wrap
 */
public JumblrException(Response response) {
    this.responseCode = response.getCode();
    String body = response.getBody();

    JsonParser parser = new JsonParser();
    try {
        final JsonElement element = parser.parse(body);
        if (element.isJsonObject()) {
            JsonObject object = element.getAsJsonObject();
            this.extractMessage(object);
            this.extractErrors(object);
        } else {
            this.message = body;
        }
    } catch (JsonParseException ex) {
        this.message = body;
    }
}
 
Example 7
Source File: FacebookAPI.java    From mamute with Apache License 2.0 5 votes vote down vote up
private Response makeRequest(String url) {
	OAuthRequest request = new OAuthRequest(Verb.GET, url);
	service.signRequest(accessToken, request);
	Response response = request.send();
	String body = response.getBody();
	if (response.getCode() / 100 != 2) {
		throw new IllegalArgumentException("http error: " + response.getCode() + ", facebook response body: " + body);
	}
	return response;
}
 
Example 8
Source File: XeroClient.java    From xero-java-client with Apache License 2.0 5 votes vote down vote up
protected com.connectifier.xeroclient.models.Response put(String endPoint, JAXBElement<?> object) {
  OAuthRequest request = new OAuthRequest(Verb.PUT, BASE_URL + endPoint);
  String contents = marshallRequest(object);
  request.setCharset("UTF-8");
  request.addBodyParameter("xml", contents);
  service.signRequest(token, request);
  Response response = request.send();
  if (response.getCode() != 200) {
    throw newApiException(response);
  }
  return unmarshallResponse(response.getBody(), com.connectifier.xeroclient.models.Response.class);
}
 
Example 9
Source File: XeroClient.java    From xero-java-client with Apache License 2.0 5 votes vote down vote up
protected com.connectifier.xeroclient.models.Response post(String endPoint, JAXBElement<?> object) {
  OAuthRequest request = new OAuthRequest(Verb.POST, BASE_URL + endPoint);
  String contents = marshallRequest(object);
  request.setCharset("UTF-8");
  request.addBodyParameter("xml", contents);
  service.signRequest(token, request);
  Response response = request.send();
  if (response.getCode() != 200) {
    throw newApiException(response);
  }
  return unmarshallResponse(response.getBody(), com.connectifier.xeroclient.models.Response.class);
}
 
Example 10
Source File: RequestBuilder.java    From jumblr with Apache License 2.0 5 votes vote down vote up
public String getRedirectUrl(String path) {
    OAuthRequest request = this.constructGet(path, null);
    sign(request);
    boolean presetVal = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    Response response = request.send();
    HttpURLConnection.setFollowRedirects(presetVal);
    if (response.getCode() == 301 || response.getCode() == 302) {
        return response.getHeader("Location");
    } else {
        throw new JumblrException(response);
    }
}
 
Example 11
Source File: RequestBuilder.java    From jumblr with Apache License 2.0 5 votes vote down vote up
Token clearXAuth(Response response) {
    if (response.getCode() == 200 || response.getCode() == 201) {
        return parseXAuthResponse(response);
    } else {
        throw new JumblrException(response);
    }
}
 
Example 12
Source File: ResourceClient.java    From jerseyoauth2 with MIT License 5 votes vote down vote up
public String sendTestRequestSample1(Token accessToken) throws ClientException
{
	OAuthService service = getOAuthService(scopes, null);
	
	OAuthRequest request = new OAuthRequest(Verb.GET,
			"http://localhost:9998/testsuite/rest/sample/1");
	service.signRequest(accessToken, request);
	Response response = request.send();
	if (response.getCode()!=200)
		throwClientException(response);
	return response.getBody();
}
 
Example 13
Source File: ResourceClient.java    From jerseyoauth2 with MIT License 5 votes vote down vote up
public String sendTestRequestSample2(Token accessToken) throws ClientException
{
	OAuthService service = getOAuthService(null, null);
	
	OAuthRequest request = new OAuthRequest(Verb.GET,
			"http://localhost:9998/testsuite/rest/sample2/1");
	service.signRequest(accessToken, request);
	Response response = request.send();
	if (response.getCode()!=200)
		throwClientException(response);
	return response.getBody();
}
 
Example 14
Source File: SmugMugInterface.java    From data-transfer-project with Apache License 2.0 4 votes vote down vote up
private <T> T postRequest(
    String url,
    Map<String, String> contentParams,
    @Nullable byte[] contentBytes,
    Map<String, String> smugMugHeaders,
    TypeReference<T> typeReference)
    throws IOException {

  String fullUrl = url;
  if (!fullUrl.contains("://")) {
    fullUrl = BASE_URL + url;
  }
  OAuthRequest request = new OAuthRequest(Verb.POST, fullUrl);

  // Add payload
  if (contentBytes != null) {
    request.addPayload(contentBytes);
  }

  // Add body params
  for (Entry<String, String> param : contentParams.entrySet()) {
    request.addBodyParameter(param.getKey(), param.getValue());
  }

  // sign request before adding any of the headers since those shouldn't be included in the
  // signature
  oAuthService.signRequest(accessToken, request);

  // Add headers
  for (Entry<String, String> header : smugMugHeaders.entrySet()) {
    request.addHeader(header.getKey(), header.getValue());
  }
  // add accept and content type headers so the response comes back in json and not html
  request.addHeader(HttpHeaders.ACCEPT, "application/json");

  Response response = request.send();
  if (response.getCode() < 200 || response.getCode() >= 300) {
    if (response.getCode() == 400) {
      throw new IOException(
          String.format(
              "Error occurred in request for %s, code: %s, message: %s, request: %s, bodyParams: %s, payload: %s",
              fullUrl,
              response.getCode(),
              response.getMessage(),
              request,
              request.getBodyParams(),
              request.getBodyContents()));
    }
    throw new IOException(
        String.format(
            "Error occurred in request for %s, code: %s, message: %s",
            fullUrl, response.getCode(), response.getMessage()));
  }
  return mapper.readValue(response.getBody(), typeReference);
}
 
Example 15
Source File: ResourceClient.java    From jerseyoauth2 with MIT License 4 votes vote down vote up
protected void throwClientException(Response response) throws ClientException
{
	int code = response.getCode();
	String body = response.getBody();
	throw new ClientException(Integer.toString(code)+" "+body);
}