예제 #1
0
  final <T extends ServerResponse> T doPost(CommandArguments args, Class<T> clazz, String url)
      throws FlickrException {
    try {
      OAuthRequest request = new OAuthRequest(Verb.POST, url);

      // check for proxy, use if available
      if (proxy != null) {
        request.setProxy(proxy);
      }

      for (Map.Entry<String, Object> param : args.getParameters().entrySet()) {
        if (param.getValue() instanceof String) {
          request.addQuerystringParameter(param.getKey(), (String) param.getValue());
        }
      }

      oauth.signRequest(request);

      MultipartEntity multipart = args.getBody(request.getOauthParameters());
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      multipart.writeTo(baos);

      request.addPayload(baos.toByteArray());
      request.addHeader("Content-type", multipart.getContentType().getValue());

      Response response = request.send();
      String body = response.getBody();

      return parseBody(args, clazz, body);

    } catch (IOException ex) {
      throw new UnsupportedOperationException("Error preparing multipart request", ex);
    }
  }
예제 #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);
    }
  }