@Test
  public void shouldNotAddAdditionalSlashWhenEndpointContainsOneButPathIsEmpty() throws Exception {
    Request<?> amazonRequest = HttpClient.convertToRequest(new HttpRequest(HttpMethodName.GET));
    amazonRequest.setEndpoint(new URI("https://endpoint.with.hostname/and_a_path"));
    amazonRequest.setResourcePath("");

    HTTPRequest googleRequest =
        new AmazonHttpRequestToGoogleHttpRequestAdaptor().convert(amazonRequest);
    assertEquals("https://endpoint.with.hostname/and_a_path", googleRequest.getURL().toString());
  }
  @Test
  public void shouldAppendTrailingSlashWhenEndpointLacksOneAndNeitherPathNorQueryStringSupplied()
      throws Exception {
    Request<?> amazonRequest = HttpClient.convertToRequest(new HttpRequest(HttpMethodName.GET));
    amazonRequest.setEndpoint(new URI("https://hostname.without.a.trailing.slash"));

    HTTPRequest googleRequest =
        new AmazonHttpRequestToGoogleHttpRequestAdaptor().convert(amazonRequest);
    assertEquals("https://hostname.without.a.trailing.slash/", googleRequest.getURL().toString());
  }
  @Test
  public void shouldAvoidDoubleSlashWhenBothEndpointAndPathSupplyOne() throws Exception {
    Request<?> amazonRequest = HttpClient.convertToRequest(new HttpRequest(HttpMethodName.GET));
    amazonRequest.setEndpoint(new URI("https://endpoint/"));
    amazonRequest.setResourcePath("/resource/path/");

    HTTPRequest googleRequest =
        new AmazonHttpRequestToGoogleHttpRequestAdaptor().convert(amazonRequest);
    assertEquals("https://endpoint/resource/path/", googleRequest.getURL().toString());
  }
  @Test
  public void shouldAddSlashBetweenHostAndQueryStringWhenEndpointHasNoSlashAndPathIsEmpty()
      throws Exception {
    Request<?> amazonRequest = HttpClient.convertToRequest(new HttpRequest(HttpMethodName.GET));
    amazonRequest.setEndpoint(new URI("https://hostname.without.a.trailing.slash"));
    amazonRequest.addParameter("key", "value");

    HTTPRequest googleRequest =
        new AmazonHttpRequestToGoogleHttpRequestAdaptor().convert(amazonRequest);
    assertEquals(
        "https://hostname.without.a.trailing.slash/?key=value", googleRequest.getURL().toString());
  }
  @Test
  public void shouldPutContentIntoPayload() throws Exception {
    HttpRequest amazonRequest = new HttpRequest(HttpMethodName.POST);
    amazonRequest.setEndpoint(new URI("https://endpoint/"));
    amazonRequest.setContent(new ByteArrayInputStream("PAYLOAD".getBytes()));

    HTTPRequest googleRequest =
        new AmazonHttpRequestToGoogleHttpRequestAdaptor()
            .convert(HttpClient.convertToRequest(amazonRequest));
    assertEquals("https://endpoint/", googleRequest.getURL().toString());
    assertEquals("PAYLOAD", new String(googleRequest.getPayload()));
  }
  @Test
  public void shouldInsertSlashBetweenEndpointAndPathWhenNeitherHasOne() throws Exception {
    Request<?> amazonRequest = HttpClient.convertToRequest(new HttpRequest(HttpMethodName.GET));
    amazonRequest.setEndpoint(new URI("https://hostname.without.a.trailing.slash"));
    amazonRequest.setResourcePath("resource/without/a/leading/slash/");

    HTTPRequest googleRequest =
        new AmazonHttpRequestToGoogleHttpRequestAdaptor().convert(amazonRequest);
    assertEquals(
        "https://hostname.without.a.trailing.slash/resource/without/a/leading/slash/",
        googleRequest.getURL().toString());
  }
  @Test
  public void shouldIncludeParametersInRequestBodyForPostRequestIfNoContentSupplied()
      throws Exception {
    HttpRequest amazonRequest = new HttpRequest(HttpMethodName.POST);
    amazonRequest.setEndpoint(new URI("https://endpoint/"));
    amazonRequest.addParameter("key", "value");

    HTTPRequest googleRequest =
        new AmazonHttpRequestToGoogleHttpRequestAdaptor()
            .convert(HttpClient.convertToRequest(amazonRequest));
    assertEquals("https://endpoint/", googleRequest.getURL().toString());
    assertEquals("key=value", new String(googleRequest.getPayload()));
  }
  @Test
  public void shouldPutParamatersInQueryStringWhenContentAlsoSupplied() throws Exception {
    HttpRequest amazonRequest = new HttpRequest(HttpMethodName.POST);
    amazonRequest.setEndpoint(new URI("https://endpoint/"));
    amazonRequest.addParameter("key", "value");
    amazonRequest.setContent(new ByteArrayInputStream("PAYLOAD".getBytes()));

    HTTPRequest googleRequest =
        new AmazonHttpRequestToGoogleHttpRequestAdaptor()
            .convert(HttpClient.convertToRequest(amazonRequest));
    assertEquals("https://endpoint/?key=value", googleRequest.getURL().toString());
    assertEquals("PAYLOAD", new String(googleRequest.getPayload()));
  }
  @Test
  public void shouldConcatenateEndpointWithPathAndQueryString() throws Exception {
    Request<?> amazonRequest = HttpClient.convertToRequest(new HttpRequest(HttpMethodName.GET));
    amazonRequest.setEndpoint(new URI("https://endpoint"));
    amazonRequest.setResourcePath("/resource/path/");
    amazonRequest.addParameter("key1", "value1");
    amazonRequest.addParameter("key2", "value2");

    HTTPRequest googleRequest =
        new AmazonHttpRequestToGoogleHttpRequestAdaptor().convert(amazonRequest);
    assertEquals(
        "https://endpoint/resource/path/?key2=value2&key1=value1",
        googleRequest.getURL().toString());
  }
 @Override
 public void setTimeout(int connectTimeout, int readTimeout) {
   request
       .getFetchOptions()
       .setDeadline(
           connectTimeout == 0 || readTimeout == 0
               ? Double.MAX_VALUE
               : (connectTimeout + readTimeout) / 1000.0);
 }
  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;
  }
Exemple #12
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);
 }
Exemple #14
0
  /** Profile info operation. */
  private long profInfoOp(
      final HttpServletRequest request,
      final HttpServletResponse response,
      final PersistenceManager pm,
      final ApiAccount apiAccount)
      throws IOException {
    LOGGER.fine("API account: " + apiAccount.getUser().getEmail());

    final Integer bnetId;
    final Integer bnetSubId;
    final String gatewayString;
    final String playerName;
    final Gateway gateway;

    final String bnetProfileUrlParam = request.getParameter(PARAM_BNET_PROFILE_URL);
    if (bnetProfileUrlParam == null || bnetProfileUrlParam.isEmpty()) {
      // Player id is provided explicitly
      bnetId = getIntParam(request, PARAM_BNET_ID);
      bnetSubId = getIntParam(request, PARAM_BNET_SUBID);
      gatewayString = request.getParameter(PARAM_GATEWAY);
      playerName = request.getParameter(PARAM_PLAYER_NAME);
      if (bnetId == null
          || bnetSubId == null
          || gatewayString == null
          || gatewayString.isEmpty()
          || playerName == null
          || playerName.isEmpty()) {
        LOGGER.warning("Missing parameters!");
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing parameters!");
        return 0;
      }
      gateway = Gateway.fromBinaryValue(gatewayString);
      if (gateway == Gateway.UNKNOWN || gateway == Gateway.PUBLIC_TEST) {
        LOGGER.warning("Invalid gateway parameter: " + gatewayString);
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid gateway parameter!");
        return 0;
      }
    } else {
      // Player id is provided through his/her Battle.net profile URL
      try {
        Gateway foundGateway = null;
        for (final Gateway gateway_ : EnumCache.GATEWAYS)
          if (bnetProfileUrlParam.startsWith(gateway_.bnetUrl)) {
            foundGateway = gateway_;
            break;
          }
        if (foundGateway == null
            || foundGateway == Gateway.UNKNOWN
            || foundGateway == Gateway.PUBLIC_TEST) throw new Exception("No matching gateway!");
        gateway = foundGateway;

        final String[] urlParts = bnetProfileUrlParam.split("/");
        if (urlParts.length < 3) throw new Exception("Not enough parts in URL!");

        playerName = URLDecoder.decode(urlParts[urlParts.length - 1], "UTF-8");
        bnetSubId = Integer.valueOf(urlParts[urlParts.length - 2]);
        bnetId = Integer.valueOf(urlParts[urlParts.length - 3]);

      } catch (final Exception e) {
        LOGGER.log(Level.SEVERE, "Invalid Battle.net profile URL: " + bnetProfileUrlParam, e);
        response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid Battle.net profile URL!");
        return 0;
      }
    }

    long opsCharged = 1;

    final Boolean retrieveExtInfoParam = getBooleanParam(request, PARAM_RETRIEVE_EXT_INFO);
    // Default or requested values:
    final boolean retrieveExtInfo = retrieveExtInfoParam == null ? false : retrieveExtInfoParam;

    try {
      final PlayerId playerId = new PlayerId();
      playerId.battleNetId = bnetId;
      playerId.battleNetSubId = bnetSubId;
      playerId.gateway = gateway;
      playerId.name = playerName;

      final String bnetProfileUrl = playerId.getBattleNetProfileUrl(BnetLanguage.ENGLISH);

      LOGGER.fine("Bnet profile URL: " + bnetProfileUrl);

      final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();

      // Default deadline: 5 seconds... increase it!
      final HTTPRequest profileRequest = new HTTPRequest(new URL(bnetProfileUrl));
      profileRequest
          .getFetchOptions()
          .setDeadline(50.0); // 50-sec deadline (leaving 10 seconds to process...
      final Future<HTTPResponse> profileFetchAsync = urlFetchService.fetchAsync(profileRequest);

      final Future<HTTPResponse> extProfileFetchAsync;
      if (retrieveExtInfo) {
        opsCharged++;
        // Start retrieving extended profile info in parallel
        final HTTPRequest extProfileRequest =
            new HTTPRequest(new URL(bnetProfileUrl + "ladder/leagues"));
        extProfileRequest
            .getFetchOptions()
            .setDeadline(50.0); // 50-sec deadline (leaving 10 seconds to process...
        extProfileFetchAsync = urlFetchService.fetchAsync(extProfileRequest);
      } else extProfileFetchAsync = null;

      final XmlBuilder xb = new XmlBuilder("1.1");

      HTTPResponse profileResponse = null;
      Profile profile = null;
      Element profInfoElement = null;
      try {
        profileResponse = profileFetchAsync.get();

        switch (profileResponse.getResponseCode()) {
          case HttpServletResponse.SC_OK:
            {
              final byte[] content = profileResponse.getContent();
              if (content.length == 0) throw new Exception("Content length = 0!");
              profile = BnetUtils.retrieveProfile(null, new ByteArrayInputStream(content));
              if (profile != null) {
                LOGGER.fine("Parse OK");

                xb.createResultElement(ProfInfoResult.OK);
                opsCharged += 2;

                profInfoElement = xb.setParentElement(xb.createElement(XTAG_PROFILE_INFO));
                // Re-include player id
                final Element playerElement =
                    xb.createElement(XTAG_PLAYER_ID, XATTR_NAME, playerName);
                playerElement.setAttribute(XATTR_BNET_ID, bnetId.toString());
                playerElement.setAttribute(XATTR_BNET_SUBID, bnetSubId.toString());
                playerElement.setAttribute(XATTR_GATEWAY, gateway.toString());
                playerElement.setAttribute(XATTR_GW_CODE, gateway.binaryValue);
                playerElement.setAttribute(XATTR_REGION, playerId.getRegion().toString());
                playerElement.setAttribute(XATTR_PROFILE_URL, bnetProfileUrl);
                final Element portraitElement =
                    xb.createElement(XTAG_PORTRAIT, XATTR_GROUP, profile.portraitGroup);
                portraitElement.setAttribute(XATTR_ROW, Integer.toString(profile.portraitRow));
                portraitElement.setAttribute(
                    XATTR_COLUMN, Integer.toString(profile.portraitColumn));
                xb.createElement(XTAG_ACHIEVEMENT_POINTS, profile.achievementPoints);
                xb.createElement(XTAG_TOTAL_CAREER_GAMES, profile.totalCareerGames);
                xb.createElement(XTAG_GAMES_THIS_SEASON, profile.gamesThisSeason);
                xb.createElement(XTAG_TERRAN_WINS, profile.terranWins);
                xb.createElement(XTAG_ZERG_WINS, profile.zergWins);
                xb.createElement(XTAG_PROTOSS_WINS, profile.protossWins);
                final Element highestSoloFlElement =
                    xb.createElement(XTAG_HIGHEST_SOLO_FL, profile.highestSoloFinishLeague);
                if (profile.highestSoloFinishTimes > 0)
                  highestSoloFlElement.setAttribute(
                      XATTR_TIMES_ACHIEVED, Integer.toString(profile.highestSoloFinishTimes));
                final Element highestTeamFlElement =
                    xb.createElement(XTAG_HIGHEST_TEAM_FL, profile.highestTeamFinishLeague);
                if (profile.highestTeamFinishTimes > 0)
                  highestTeamFlElement.setAttribute(
                      XATTR_TIMES_ACHIEVED, Integer.toString(profile.highestTeamFinishTimes));

                break;
              } else {
                LOGGER.fine("Parse error!");
                xb.createResultElement(ProfInfoResult.PARSING_ERROR); // Parse fails
              }
            }
          case HttpServletResponse.SC_NOT_FOUND:
            LOGGER.fine("Invalid player!");
            xb.createResultElement(ProfInfoResult.INVALID_PLAYER);
            break;
          default:
            // Treat other response HTTP status codes as BNET_ERROR
            throw new Exception("Response code: " + profileResponse.getResponseCode());
        }

      } catch (final Exception e) {
        LOGGER.log(Level.SEVERE, "", e);
        xb.createResultElement(ProfInfoResult.BNET_ERROR);
      } finally {
        if (retrieveExtInfo && profile == null) extProfileFetchAsync.cancel(true);
      }

      if (retrieveExtInfo && profile != null) {
        try {
          profileResponse = extProfileFetchAsync.get();

          final byte[] content;
          if (profileResponse.getResponseCode() == HttpServletResponse.SC_OK
              && (content = profileResponse.getContent()).length > 0) {
            profile =
                BnetUtils.retrieveExtProfile(null, new ByteArrayInputStream(content), profile);
            if (profile != null) {
              LOGGER.fine("Parse extended OK");

              opsCharged += 2;
              xb.setParentElement(profInfoElement);
              final Element allRankGroupsElement =
                  xb.setParentElement(xb.createElement(XTAG_ALL_RANK_GROUPS));
              int allRankGroupsCount = 0;
              for (int bracket = 0; bracket < profile.allRankss.length; bracket++) {
                final TeamRank[] allRanks = profile.allRankss[bracket];
                if (allRanks != null && allRanks.length > 0) {
                  allRankGroupsCount++;
                  xb.setParentElement(allRankGroupsElement);
                  final Element allRankGroupElement =
                      xb.createElement(XTAG_ALL_RANK_GROUP, XATTR_COUNT, allRanks.length);
                  allRankGroupElement.setAttribute(
                      XATTR_FORMAT, (bracket + 1) + "v" + (bracket + 1));

                  for (int i = 0; i < allRanks.length; i++) {
                    xb.setParentElement(allRankGroupElement);
                    final Element teamRankElement =
                        xb.setParentElement(
                            xb.createElement(
                                XTAG_TEAM_RANK, XATTR_LEAGUE, allRanks[i].league.stringValue));
                    teamRankElement.setAttribute(
                        XATTR_DIVISION_RANK, Integer.toString(allRanks[i].divisionRank));
                    // Team members
                    xb.setParentElement(
                        xb.createElement(
                            XTAG_TEAM_MEMBERS, XATTR_COUNT, allRanks[i].teamMembers.length));
                    for (final String memberName : allRanks[i].teamMembers)
                      xb.createElement(XTAG_TEAM_MEMBER, XATTR_NAME, memberName);
                  }
                }
              }
              allRankGroupsElement.setAttribute(XATTR_COUNT, Integer.toString(allRankGroupsCount));
            } else LOGGER.fine("Parse extended error!");
          }
        } catch (final Exception e) {
          LOGGER.log(
              Level.SEVERE,
              "Failed to get extended profile info, we return the basic profile info silently.",
              e);
          // Failed to get extended profile info, we return the basic profile info silently
        }
      }

      xb.printDocument(response);

      return opsCharged;

    } catch (final Exception e) {
      LOGGER.log(Level.SEVERE, "", e);
      response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
      return 0;
    }
  }
 @Override
 public void addHeader(String name, String value) {
   request.addHeader(new HTTPHeader(name, value));
 }
 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);
   }
 }