예제 #1
0
파일: REST.java 프로젝트: kipz/Flickr4Java
  /**
   * Invoke an HTTP GET request on a remote host. You must close the InputStream after you are done
   * with.
   *
   * @param path The request path
   * @param parameters The parameters (collection of Parameter objects)
   * @return The Response
   */
  @Override
  public com.flickr4java.flickr.Response get(
      String path, Map<String, Object> parameters, String sharedSecret) {

    OAuthRequest request = new OAuthRequest(Verb.GET, API_HOST + path);
    for (Map.Entry<String, Object> entry : parameters.entrySet()) {
      request.addQuerystringParameter(entry.getKey(), String.valueOf(entry.getValue()));
    }

    if (proxyAuth) {
      request.addHeader("Proxy-Authorization", "Basic " + getProxyCredentials());
    }

    RequestContext requestContext = RequestContext.getRequestContext();
    Auth auth = requestContext.getAuth();
    if (auth != null) {
      Token requestToken = new Token(auth.getToken(), auth.getTokenSecret());
      OAuthService service = createOAuthService(parameters, sharedSecret);
      service.signRequest(requestToken, request);
    }

    if (Flickr.debugRequest) {
      logger.debug("GET: " + request.getCompleteUrl());
    }
    setTimeouts(request);
    org.scribe.model.Response scribeResponse = request.send();

    try {

      com.flickr4java.flickr.Response response = null;
      synchronized (mutex) {
        String strXml = scribeResponse.getBody();
        if (Flickr.debugStream) {
          logger.debug(strXml);
        }
        Document document = builder.parse(new InputSource(new StringReader(strXml)));
        response = (com.flickr4java.flickr.Response) responseClass.newInstance();
        response.parse(document);
      }
      return response;
    } catch (IllegalAccessException e) {
      throw new FlickrRuntimeException(e);
    } catch (InstantiationException e) {
      throw new FlickrRuntimeException(e);
    } catch (SAXException e) {
      throw new FlickrRuntimeException(e);
    } catch (IOException e) {
      throw new FlickrRuntimeException(e);
    }
  }
예제 #2
0
파일: REST.java 프로젝트: kipz/Flickr4Java
  /**
   * Invoke an HTTP POST request on a remote host.
   *
   * @param path The request path
   * @param parameters The parameters (collection of Parameter objects)
   * @return The Response object
   */
  @Override
  public com.flickr4java.flickr.Response post(
      String path, Map<String, Object> parameters, String sharedSecret, boolean multipart) {

    OAuthRequest request = new OAuthRequest(Verb.POST, API_HOST + path);

    if (multipart) {
      buildMultipartRequest(parameters, request);
    } else {
      buildNormalPostRequest(parameters, request);
    }

    RequestContext requestContext = RequestContext.getRequestContext();
    Auth auth = requestContext.getAuth();
    if (auth != null) {
      Token requestToken = new Token(auth.getToken(), auth.getTokenSecret());
      OAuthService service = createOAuthService(parameters, sharedSecret);
      service.signRequest(requestToken, request);
    }

    if (multipart) {
      // Ensure all parameters (including oauth) are added to payload so signature matches
      parameters.putAll(request.getOauthParameters());
      request.addPayload(buildMultipartBody(parameters, getMultipartBoundary()));
    }

    if (proxyAuth) {
      request.addHeader("Proxy-Authorization", "Basic " + getProxyCredentials());
    }

    if (Flickr.debugRequest) {
      logger.debug("POST: " + request.getCompleteUrl());
    }

    org.scribe.model.Response scribeResponse = request.send();

    try {
      com.flickr4java.flickr.Response response = null;
      synchronized (mutex) {
        String strXml = scribeResponse.getBody();
        if (Flickr.debugStream) {
          logger.debug(strXml);
        }
        if (strXml.startsWith("oauth_problem=")) {
          throw new FlickrRuntimeException(strXml);
        }
        Document document = builder.parse(new InputSource(new StringReader(strXml)));
        response = (com.flickr4java.flickr.Response) responseClass.newInstance();
        response.parse(document);
      }
      return response;
    } catch (IllegalAccessException e) {
      throw new FlickrRuntimeException(e);
    } catch (InstantiationException e) {
      throw new FlickrRuntimeException(e);
    } catch (SAXException e) {
      throw new FlickrRuntimeException(e);
    } catch (IOException e) {
      throw new FlickrRuntimeException(e);
    }
  }