public String postFile(URLFetchService us, List<PostObj> postobjlist) throws Exception {
    int index;

    Gson gson;
    String linkID;
    List<String> linkList;
    HTTPRequest req;
    String param;

    gson = new Gson();

    linkID = createUID();
    linkList = new ArrayList<String>();
    for (index = 0; index < postobjlist.size(); index++) {
      linkList.add(postobjlist.get(index).filelink);
    }

    param =
        URLEncoder.encode("postlist", "UTF-8")
            + "="
            + URLEncoder.encode(gson.toJson(postobjlist), "UTF-8")
            + '&'
            + URLEncoder.encode("linkid", "UTF-8")
            + "="
            + URLEncoder.encode(linkID, "UTF-8")
            + '&'
            + URLEncoder.encode("linklist", "UTF-8")
            + "="
            + URLEncoder.encode(gson.toJson(linkList), "UTF-8");
    req =
        new HTTPRequest(
            new URL("http://tnfshmoe.appspot.com/postdf89ksfxsyx9sfdex09usdjksd"), HTTPMethod.POST);
    req.setPayload(param.getBytes());
    us.fetch(req);

    return linkID;
  }
Beispiel #2
0
  public UserId determineUserId(String code) throws IOException {
    Map<String, String> parameters = Maps.newHashMap();
    parameters.put("code", code);
    parameters.put("client_id", clientId);
    parameters.put("client_secret", clientSecret);
    parameters.put("redirect_uri", uriBuilder.forPath(OAuthCallbackServlet.PATH).toString());
    parameters.put("grant_type", "authorization_code");

    HTTPRequest fetchRequest =
        new HTTPRequest(new URL("https://accounts.google.com/o/oauth2/token"), HTTPMethod.POST);
    fetchRequest.setPayload(buildKeyValueString(parameters, true).getBytes());
    HTTPResponse response = urlFetchService.fetch(fetchRequest);
    JsonObject object = jsonParser.parse(new String(response.getContent())).getAsJsonObject();
    String access_token = object.get("access_token").getAsString();

    HTTPRequest secondRequest =
        new HTTPRequest(new URL("https://www.googleapis.com/oauth2/v1/userinfo"));
    secondRequest.addHeader(
        new HTTPHeader("Authorization", String.format("Bearer %s", access_token)));
    response = urlFetchService.fetch(secondRequest);
    object = jsonParser.parse(new String(response.getContent())).getAsJsonObject();

    return UserId.fromString(object.get("id").getAsString());
  }
 @Override
 public LowLevelHttpResponse execute() throws IOException {
   // write content
   if (getStreamingContent() != null) {
     String contentType = getContentType();
     if (contentType != null) {
       addHeader("Content-Type", contentType);
     }
     String contentEncoding = getContentEncoding();
     if (contentEncoding != null) {
       addHeader("Content-Encoding", contentEncoding);
     }
     ByteArrayOutputStream out = new ByteArrayOutputStream();
     getStreamingContent().writeTo(out);
     byte[] payload = out.toByteArray();
     if (payload.length != 0) {
       request.setPayload(payload);
     }
   }
   // connect
   URLFetchService service = URLFetchServiceFactory.getURLFetchService();
   HTTPResponse response = service.fetch(request);
   return new UrlFetchResponse(response);
 }
 private Future<HTTPResponse> postBlobs(
     String filename,
     String contentType,
     String sha1,
     byte[] data,
     String metadataSize,
     HttpServletRequest request,
     int width,
     int height)
     throws MessagingException, IOException {
   try {
     URLFetchService fetchService = URLFetchServiceFactory.getURLFetchService();
     BlobstoreService blobstore = BlobstoreServiceFactory.getBlobstoreService();
     URI reqUri = new URI(request.getScheme(), request.getServerName(), "", "");
     URI uri =
         reqUri.resolve(
             "/blob?"
                 + NamespaceParameter
                 + "="
                 + NamespaceManager.get()
                 + "&"
                 + SizeParameter
                 + "="
                 + metadataSize
                 + "&"
                 + WidthParameter
                 + "="
                 + width
                 + "&"
                 + HeightParameter
                 + "="
                 + height);
     URL uploadUrl = new URL(blobstore.createUploadUrl(uri.toASCIIString()));
     log("post blob to " + uploadUrl);
     HTTPRequest httpRequest =
         new HTTPRequest(uploadUrl, HTTPMethod.POST, FetchOptions.Builder.withDeadline(60));
     String uid = UUID.randomUUID().toString();
     httpRequest.addHeader(new HTTPHeader("Content-Type", "multipart/form-data; boundary=" + uid));
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     PrintStream ps = new PrintStream(baos);
     ps.append("--" + uid);
     ps.append(CRLF);
     ps.append(
         "Content-Disposition: form-data; name=\"" + sha1 + "\"; filename=\"" + filename + "\"");
     ps.append(CRLF);
     ps.append("Content-Type: " + contentType);
     ps.append(CRLF);
     ps.append("Content-Transfer-Encoding: binary");
     ps.append(CRLF);
     ps.append(CRLF);
     ps.write(data);
     ps.append(CRLF);
     ps.append("--" + uid + "--");
     ps.append(CRLF);
     ps.close();
     log("sending blob size=" + baos.size());
     httpRequest.setPayload(baos.toByteArray());
     return fetchService.fetchAsync(httpRequest);
   } catch (URISyntaxException ex) {
     throw new IOException(ex);
   }
 }