/**
   * Maps ugly urls to pretty urls as specified in the Google specification for Making AJAX
   * Applications Crawlable:
   * https://developers.google.com/webmasters/ajax-crawling/docs/specification?hl=en
   *
   * @param ugly the ugly url that the search engine crawler is requesting
   * @return pretty url
   */
  public static URI uglyToPretty(URI ugly) {
    String uglyQuery = ugly.getRawQuery();
    List<NameValuePair> uglyParams = URLEncodedUtils.parse(uglyQuery, Charset.forName("utf-8"));
    URIBuilder uriBuilder = new URIBuilder(ugly);

    // rebuild the query using the rules from Google's Spec.
    uriBuilder.removeQuery();
    for (NameValuePair uglyParamPair : uglyParams) {
      String lowerCaseParamName = uglyParamPair.getName().toLowerCase(Locale.ENGLISH);
      if (SEARCHBOT_ESCAPED_FRAGMENT_PARAM_NAME.equals(lowerCaseParamName)) {
        String fragmentValue = uglyParamPair.getValue();
        if (!fragmentValue.isEmpty()) {
          uriBuilder.setFragment(BANG + uglyParamPair.getValue());
        }
      } else {
        uriBuilder.addParameter(uglyParamPair.getName(), uglyParamPair.getValue());
      }
    }

    URI builtUri;
    try {
      builtUri = uriBuilder.build();
    } catch (URISyntaxException ex) {
      throw new IllegalArgumentException(ex);
    }

    return builtUri;
  }
Ejemplo n.º 2
0
  public static void main(String[] args) {
    MongoService mongoService = new MongoService();
    MongoCollection<Document> collection = mongoService.database.getCollection("facebookPosts");

    OAuth2Client client = CommonOAuth2.getOAuth2Client("facebook");
    // String appAccessToken = client.getAppAccessToken();
    String accessToken =
        "CAACEdEose0cBAAg15ALJYKJLYkcE3VcqqR735CMZCSX5txnfyhZBgVDqErYc0LvagNA0zmAKDMWDoe4XqbdhpAKzjiI2A8xGxJAZBcKbNZBOLczm7vlMlqZCLn0hG5zfoQRDfRrkxnXhwiAZBdXXrv9Q2UfYxSDMmGeq8PwIKgEPaz6dXjnXIBxnpNaMOcchgZD";
    client.setProperty("access_token", accessToken);

    String api =
        new ApiBuilder("/v2.0/643588696/feed").limit(25).until(2014, 6, 1, 0, 0).toString();
    System.out.println(api);

    while (true) {
      HashMap<String, Object> response = process(collection, client, api);
      if (!response.containsKey("paging")) break;
      api = get(response, "paging.next");
      List<NameValuePair> list = URLEncodedUtils.parse(api, Charset.forName("UTF-8"));
      for (NameValuePair nameValuePair : list) {
        System.out.println(nameValuePair);
      }
    }

    // 643588696/feed?until=1401351351
  }
Ejemplo n.º 3
0
 public static zzol zza(zzaf com_google_android_gms_analytics_internal_zzaf, String str) {
   zzol com_google_android_gms_internal_zzol = null;
   zzx.zzv(com_google_android_gms_analytics_internal_zzaf);
   if (TextUtils.isEmpty(str)) {
     return com_google_android_gms_internal_zzol;
   }
   try {
     List<NameValuePair> parse = URLEncodedUtils.parse(new URI("?" + str), HTTP.UTF_8);
     Map hashMap = new HashMap(parse.size());
     for (NameValuePair nameValuePair : parse) {
       hashMap.put(nameValuePair.getName(), nameValuePair.getValue());
     }
     zzol com_google_android_gms_internal_zzol2 = new zzol();
     com_google_android_gms_internal_zzol2.zzdL((String) hashMap.get("utm_content"));
     com_google_android_gms_internal_zzol2.zzdJ((String) hashMap.get("utm_medium"));
     com_google_android_gms_internal_zzol2.setName((String) hashMap.get("utm_campaign"));
     com_google_android_gms_internal_zzol2.zzdI((String) hashMap.get("utm_source"));
     com_google_android_gms_internal_zzol2.zzdK((String) hashMap.get("utm_term"));
     com_google_android_gms_internal_zzol2.zzdM((String) hashMap.get("utm_id"));
     com_google_android_gms_internal_zzol2.zzdN((String) hashMap.get("anid"));
     com_google_android_gms_internal_zzol2.zzdO((String) hashMap.get("gclid"));
     com_google_android_gms_internal_zzol2.zzdP((String) hashMap.get("dclid"));
     com_google_android_gms_internal_zzol2.zzdQ((String) hashMap.get("aclid"));
     return com_google_android_gms_internal_zzol2;
   } catch (URISyntaxException e) {
     com_google_android_gms_analytics_internal_zzaf.zzd("No valid campaign data found", e);
     return com_google_android_gms_internal_zzol;
   }
 }
Ejemplo n.º 4
0
 public static zzkq zza(zzae com_google_android_gms_analytics_internal_zzae, String str) {
   zzkq com_google_android_gms_internal_zzkq = null;
   zzv.zzr(com_google_android_gms_analytics_internal_zzae);
   if (TextUtils.isEmpty(str)) {
     return com_google_android_gms_internal_zzkq;
   }
   try {
     List<NameValuePair> parse =
         URLEncodedUtils.parse(new URI("?" + str), HttpRequest.CHARSET_UTF8);
     Map hashMap = new HashMap(parse.size());
     for (NameValuePair nameValuePair : parse) {
       hashMap.put(nameValuePair.getName(), nameValuePair.getValue());
     }
     zzkq com_google_android_gms_internal_zzkq2 = new zzkq();
     com_google_android_gms_internal_zzkq2.zzcU((String) hashMap.get("utm_content"));
     com_google_android_gms_internal_zzkq2.zzcS((String) hashMap.get("utm_medium"));
     com_google_android_gms_internal_zzkq2.setName((String) hashMap.get("utm_campaign"));
     com_google_android_gms_internal_zzkq2.zzcR((String) hashMap.get("utm_source"));
     com_google_android_gms_internal_zzkq2.zzcT((String) hashMap.get("utm_term"));
     com_google_android_gms_internal_zzkq2.zzcV((String) hashMap.get("utm_id"));
     com_google_android_gms_internal_zzkq2.zzcW((String) hashMap.get("anid"));
     com_google_android_gms_internal_zzkq2.zzcX((String) hashMap.get("gclid"));
     com_google_android_gms_internal_zzkq2.zzcY((String) hashMap.get("dclid"));
     com_google_android_gms_internal_zzkq2.zzcZ((String) hashMap.get("aclid"));
     return com_google_android_gms_internal_zzkq2;
   } catch (URISyntaxException e) {
     com_google_android_gms_analytics_internal_zzae.zzd("No valid campaign data found", e);
     return com_google_android_gms_internal_zzkq;
   }
 }
Ejemplo n.º 5
0
 /**
  * Returns a list of {@link NameValuePair NameValuePairs} as built from the URI's query portion.
  * For example, a URI of http://example.org/path/to/file?a=1&b=2&c=3 would return a list of three
  * NameValuePairs, one for a=1, one for b=2, and one for c=3.
  *
  * <p>This is typically useful while parsing an HTTP PUT.
  *
  * @param uri uri to parse
  * @param encoding encoding to use while parsing the query
  */
 public static List<NameValuePair> parse(final URI uri, final String encoding) {
   List<NameValuePair> result = Collections.emptyList();
   final String query = uri.getRawQuery();
   if (query != null && query.length() > 0) {
     result = new ArrayList<NameValuePair>();
     parse(result, new Scanner(query), encoding);
   }
   return result;
 }
 private String getQueryStringParam(String query, String key) {
   List<NameValuePair> params = URLEncodedUtils.parse(query, Charset.defaultCharset());
   for (NameValuePair pair : params) {
     if (key.equals(pair.getName())) {
       return pair.getValue();
     }
   }
   return null;
 }
Ejemplo n.º 7
0
  private static QueryHashMap<String, String> parseEventUrl(String url) throws Exception {
    QueryHashMap<String, String> values = new QueryHashMap<String, String>();

    List<NameValuePair> params = URLEncodedUtils.parse(new URI("http://localhost/" + url), "UTF-8");

    for (NameValuePair param : params) {
      values.put(param.getName(), param.getValue());
    }

    return values;
  }
  public ServerResponse preProcess(HttpRequest request, ResourceMethod method)
      throws Failure, WebApplicationException {
    ServerResponse response = null;
    Long requestId = RequestLogger.getInstance().getLogger().getRequestId();
    logger.info("Request Interceptor: Processing request #%d", requestId);

    List<NameValuePair> params = URLEncodedUtils.parse(request.getUri().getRequestUri(), "UTF-8");

    // Handle paging parameters.
    processPaging(request, params, requestId);
    return response;
  }
Ejemplo n.º 9
0
 /**
  * Returns a list of {@link NameValuePair NameValuePairs} as parsed from an {@link HttpEntity}.
  * The encoding is taken from the entity's Content-Encoding header.
  *
  * <p>This is typically used while parsing an HTTP POST.
  *
  * @param entity The entity to parse
  * @throws IOException If there was an exception getting the entity's data.
  */
 public static List<NameValuePair> parse(final HttpEntity entity) throws IOException {
   List<NameValuePair> result = Collections.emptyList();
   if (isEncoded(entity)) {
     final String content = EntityUtils.toString(entity);
     final Header encoding = entity.getContentEncoding();
     if (content != null && content.length() > 0) {
       result = new ArrayList<NameValuePair>();
       parse(result, new Scanner(content), encoding != null ? encoding.getValue() : null);
     }
   }
   return result;
 }
 @Override
 protected PocketAuthInfo buildInfo(final Response r) throws AccessTokenException {
   if (r.getStatus() >= 400) {
     throw new AccessTokenException(r.asJson().asText());
   } else {
     final List<NameValuePair> list = URLEncodedUtils.parse(r.getBody(), Charset.forName("UTF-8"));
     final Map<String, String> map = new HashMap<String, String>(list.size());
     for (final NameValuePair nameValuePair : list) {
       map.put(nameValuePair.getName(), nameValuePair.getValue());
     }
     return new PocketAuthInfo(map);
   }
 }
Ejemplo n.º 11
0
  public static URI uriFromString(String s) throws URISyntaxException {
    String scheme = null;
    int port = -1;
    String userinfo = null;
    String host;
    String path = "";
    String query = null;
    String fragment = null;
    URLEncodedUtils.parse(s, Charset.defaultCharset());
    Matcher m = Pattern.compile("([^:]+)://.*").matcher(s);
    if (m.matches()) {
      scheme = m.group(1);
    }

    m = Pattern.compile("[^:]+://([^@]+)@.*").matcher(s);
    if (m.matches()) {
      userinfo = m.group(1);
      m = Pattern.compile("[^:]+://" + userinfo + "@([^:/]+).*").matcher(s);
    } else {
      m = Pattern.compile("[^:]+://([^:/]+).*").matcher(s);
    }

    if (m.matches()) {
      host = m.group(1);
    } else {
      throw new URISyntaxException(s, "No host specified");
    }

    m = Pattern.compile("[^:]+://.*" + host + ":([0-9]+).*").matcher(s);
    if (m.matches()) {
      port = Integer.parseInt(m.group(1));
    }

    m = Pattern.compile("[^:]+://[^/]+(/[^?#]*).*").matcher(s);
    if (m.matches()) {
      path = m.group(1);
    }

    m = Pattern.compile("[^:]+://[^?]+\\?([^#]*).*").matcher(s);
    if (m.matches()) {
      query = m.group(1);
    }

    m = Pattern.compile("[^:]+://[^#]+#(.*)").matcher(s);
    if (m.matches()) {
      fragment = m.group(1);
    }

    return new URI(scheme, userinfo, host, port, path, query, fragment);
  }
Ejemplo n.º 12
0
 /**
  * Returns a list of {@link NameValuePair NameValuePairs} as built from the URI's query portion.
  * For example, a URI of http://example.org/path/to/file?a=1&b=2&c=3 would return a list of three
  * NameValuePairs, one for a=1, one for b=2, and one for c=3.
  *
  * <p>This is typically useful while parsing an HTTP PUT.
  *
  * @param uri uri to parse
  * @param encoding encoding to use while parsing the query
  */
 @DSGenerator(
     tool_name = "Doppelganger",
     tool_version = "2.0",
     generated_on = "2013-12-30 13:01:44.452 -0500",
     hash_original_method = "75763A91BDF0B02A517E106BC03551A8",
     hash_generated_method = "9C1AB2355DE68597D0C7FD33A48C9930")
 public static List<NameValuePair> parse(final URI uri, final String encoding) {
   List<NameValuePair> result = Collections.emptyList();
   final String query = uri.getRawQuery();
   if (query != null && query.length() > 0) {
     result = new ArrayList<NameValuePair>();
     parse(result, new Scanner(query), encoding);
   }
   return result;
 }
Ejemplo n.º 13
0
 public static boolean cifsCredentialsPresent(URI uri) {
   List<NameValuePair> args = URLEncodedUtils.parse(uri, "UTF-8");
   boolean foundUser = false;
   boolean foundPswd = false;
   for (NameValuePair nvp : args) {
     String name = nvp.getName();
     if (name.equals("user")) {
       foundUser = true;
       s_logger.debug("foundUser is" + foundUser);
     } else if (name.equals("password")) {
       foundPswd = true;
       s_logger.debug("foundPswd is" + foundPswd);
     }
   }
   return (foundUser && foundPswd);
 }
Ejemplo n.º 14
0
  private boolean tryCommand(URI uri) {
    String commandType = uri.getHost();
    List<NameValuePair> list = URLEncodedUtils.parse(uri, "UTF-8");
    Map<String, String> params = new HashMap<String, String>();
    for (NameValuePair pair : list) {
      params.put(pair.getName(), pair.getValue());
    }

    MraidCommand command = MraidCommandRegistry.createCommand(commandType, params, this);
    if (command == null) {
      fireNativeCommandCompleteEvent(commandType);
      return false;
    } else {
      command.execute();
      fireNativeCommandCompleteEvent(commandType);
      return true;
    }
  }
Ejemplo n.º 15
0
  private int getAxsUrlParameterValue() {
    if (mContentViewCore.getUrl() == null) return ACCESSIBILITY_SCRIPT_INJECTION_UNDEFINED;

    try {
      List<NameValuePair> params = URLEncodedUtils.parse(new URI(mContentViewCore.getUrl()), null);

      for (NameValuePair param : params) {
        if ("axs".equals(param.getName())) {
          return Integer.parseInt(param.getValue());
        }
      }
    } catch (URISyntaxException ex) {
    } catch (NumberFormatException ex) {
    } catch (IllegalArgumentException ex) {
    }

    return ACCESSIBILITY_SCRIPT_INJECTION_UNDEFINED;
  }
Ejemplo n.º 16
0
 /**
  * Returns a list of {@link NameValuePair NameValuePairs} as parsed from an {@link HttpEntity}.
  * The encoding is taken from the entity's Content-Encoding header.
  *
  * <p>This is typically used while parsing an HTTP POST.
  *
  * @param entity The entity to parse
  * @throws IOException If there was an exception getting the entity's data.
  */
 @DSGenerator(
     tool_name = "Doppelganger",
     tool_version = "2.0",
     generated_on = "2013-12-30 13:01:44.455 -0500",
     hash_original_method = "3125ACC43E08755650BD72975049DA9D",
     hash_generated_method = "57408A403AC39C54BE0F6AE05B140555")
 public static List<NameValuePair> parse(final HttpEntity entity) throws IOException {
   List<NameValuePair> result = Collections.emptyList();
   if (isEncoded(entity)) {
     final String content = EntityUtils.toString(entity);
     final Header encoding = entity.getContentEncoding();
     if (content != null && content.length() > 0) {
       result = new ArrayList<NameValuePair>();
       parse(result, new Scanner(content), encoding != null ? encoding.getValue() : null);
     }
   }
   return result;
 }
Ejemplo n.º 17
0
    public void handle(HttpRequest request, HttpResponse response, HttpContext arg2)
        throws HttpException, IOException {

      final String uri = URLDecoder.decode(request.getRequestLine().getUri());
      final List<NameValuePair> params = URLEncodedUtils.parse(URI.create(uri), "UTF-8");
      final String[] content = {"Error"};
      int soundID;

      response.setStatusCode(HttpStatus.SC_NOT_FOUND);

      if (params.size() > 0) {
        try {
          for (Iterator<NameValuePair> it = params.iterator(); it.hasNext(); ) {
            NameValuePair param = it.next();
            // Load sound with appropriate name
            if (param.getName().equals("name")) {
              for (int i = 0; i < raws.length; i++) {
                if (raws[i].getName().equals(param.getValue())) {
                  soundID = soundPool.load(context, raws[i].getInt(null), 0);
                  response.setStatusCode(HttpStatus.SC_OK);
                  content[0] = "OK";
                }
              }
            }
          }
        } catch (Exception e) {
          Log.e(TAG, "Error !");
          e.printStackTrace();
        }
      }

      EntityTemplate body =
          new EntityTemplate(
              new ContentProducer() {
                public void writeTo(final OutputStream outstream) throws IOException {
                  OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
                  writer.write(content[0]);
                  writer.flush();
                }
              });
      body.setContentType("text/plain; charset=UTF-8");
      response.setEntity(body);
    }
Ejemplo n.º 18
0
  public HashMap<String, Object> getAccessToken(String code) {
    String url = config.getProperty("access_token_url");
    try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
      URIBuilder builder = new URIBuilder(url);
      URI uri = builder.build();
      HttpPost httpPost = new HttpPost(uri);
      List<NameValuePair> nvps = new ArrayList<NameValuePair>();
      String[] keys = {"client_id", "client_secret", "redirect_uri", "grant_type"};
      for (String key : keys) {
        nvps.add(new BasicNameValuePair(key, config.getProperty(key)));
      }
      nvps.add(new BasicNameValuePair("code", code));
      httpPost.setEntity(new UrlEncodedFormEntity(nvps));
      CloseableHttpResponse response = httpclient.execute(httpPost);
      StatusLine statusLine = response.getStatusLine();
      System.out.println(response.getStatusLine());
      if (200 != statusLine.getStatusCode()) {
        throw new BadResponse(response);
      }

      HttpEntity entity = response.getEntity();
      String contentType = response.getHeaders("Content-Type")[0].getValue();
      if (contentType.contains("json")) {
        HashMap<String, Object> map =
            om.readValue(entity.getContent(), new TypeReference<HashMap<String, ?>>() {});
        long expiresIn = (int) map.get("expires_in");
        int expiration = (int) (expiresIn + System.currentTimeMillis() / 1000);
        map.put("expiration", expiration);
        return map;
      } else {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        IOUtils.copy(entity.getContent(), os);
        List<NameValuePair> list = URLEncodedUtils.parse(entity);
        HashMap<String, Object> map = new HashMap<String, Object>();
        for (NameValuePair nameValuePair : list) {
          map.put(nameValuePair.getName(), nameValuePair.getValue());
        }
        return map;
      }
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }
Ejemplo n.º 19
0
  public static SolrQuery toQuery(String queryString) {

    if (queryString == null || queryString.length() == 0) queryString = "*:*";

    SolrQuery q = new SolrQuery();
    if (queryString.indexOf("=") == -1) {
      // no name-value pairs ... just assume this single clause is the q part
      q.setQuery(queryString);
    } else {
      NamedList<Object> params = new NamedList<Object>();
      for (NameValuePair nvp : URLEncodedUtils.parse(queryString, StandardCharsets.UTF_8)) {
        String value = nvp.getValue();
        if (value != null && value.length() > 0) {
          String name = nvp.getName();
          if ("sort".equals(name)) {
            if (value.indexOf(" ") == -1) {
              q.addSort(SolrQuery.SortClause.asc(value));
            } else {
              String[] split = value.split(" ");
              q.addSort(SolrQuery.SortClause.create(split[0], split[1]));
            }
          } else {
            params.add(name, value);
          }
        }
      }
      q.add(ModifiableSolrParams.toSolrParams(params));
    }

    Integer rows = q.getRows();
    if (rows == null) q.setRows(DEFAULT_PAGE_SIZE);

    List<SolrQuery.SortClause> sorts = q.getSorts();
    if (sorts == null || sorts.isEmpty()) q.addSort(SolrQuery.SortClause.asc("id"));

    return q;
  }
    public void handle(HttpRequest request, HttpResponse response, HttpContext arg2)
        throws HttpException, IOException {

      final String uri = URLDecoder.decode(request.getRequestLine().getUri());
      final List<NameValuePair> params = URLEncodedUtils.parse(URI.create(uri), "UTF-8");
      final String[] content = {"Error"};
      int soundID;

      response.setStatusCode(HttpStatus.SC_NOT_FOUND);

      if (params.size() > 0) {
        try {
          for (Iterator<NameValuePair> it = params.iterator(); it.hasNext(); ) {
            NameValuePair param = it.next();
            // Load sound with appropriate name
            if (param.getName().equals("name")) {
              for (int i = 0; i < raws.length; i++) {
                if (raws[i].getName().equals(param.getValue())) {
                  soundID = soundPool.load(context, raws[i].getInt(null), 0);
                  response.setStatusCode(HttpStatus.SC_OK);
                  content[0] = "OK";
                }
              }
              if (param.getValue().equals("forward")) {
                handler.obtainMessage(MOVE_FORWARD).sendToTarget();
                // soundID = soundPool.load(context, raws[0].getInt(null), 0);
                response.setStatusCode(HttpStatus.SC_OK);
                content[0] = "OK";
              }
              if (param.getValue().equals("backward")) {
                handler.obtainMessage(MOVE_BACKWARD).sendToTarget();
                // soundID = soundPool.load(context, raws[0].getInt(null), 0);
                response.setStatusCode(HttpStatus.SC_OK);
                content[0] = "OK";
              }
              if (param.getValue().equals("left")) {
                handler.obtainMessage(MOVE_LEFT).sendToTarget();
                // soundID = soundPool.load(context, raws[0].getInt(null), 0);
                response.setStatusCode(HttpStatus.SC_OK);
                content[0] = "OK";
              }
              if (param.getValue().equals("right")) {
                handler.obtainMessage(MOVE_RIGHT).sendToTarget();
                // soundID = soundPool.load(context, raws[0].getInt(null), 0);
                response.setStatusCode(HttpStatus.SC_OK);
                content[0] = "OK";
              }
              if (param.getValue().equals("stop")) {
                handler.obtainMessage(STOP).sendToTarget();
                // soundID = soundPool.load(context, raws[0].getInt(null), 0);
                response.setStatusCode(HttpStatus.SC_OK);
                content[0] = "OK";
              }
              if (param.getValue().equals("obstacleOn")) {
                handler.obtainMessage(ENABLE_OBSTACLE_AVOIDANCE).sendToTarget();
                // soundID = soundPool.load(context, raws[0].getInt(null), 0);
                response.setStatusCode(HttpStatus.SC_OK);
                content[0] = "OK";
              }
              if (param.getValue().equals("obstacleOff")) {
                handler.obtainMessage(DISABLE_OBSTACLE_AVOIDANCE).sendToTarget();
                // soundID = soundPool.load(context, raws[1].getInt(null), 0);
                response.setStatusCode(HttpStatus.SC_OK);
                content[0] = "OK";
              }
              if (param.getValue().equals("cliffOn")) {
                handler.obtainMessage(ENABLE_CLIFF_AVOIDANCE).sendToTarget();
                // soundID = soundPool.load(context, raws[2].getInt(null), 0);
                response.setStatusCode(HttpStatus.SC_OK);
                content[0] = "OK";
              }
              if (param.getValue().equals("cliffOff")) {
                handler.obtainMessage(DISABLE_CLIFF_AVOIDANCE).sendToTarget();
                // soundID = soundPool.load(context, raws[3].getInt(null), 0);
                response.setStatusCode(HttpStatus.SC_OK);
                content[0] = "OK";
              }
            }
          }
        } catch (Exception e) {
          Log.e(TAG, "Error !");
          e.printStackTrace();
        }
      }

      EntityTemplate body =
          new EntityTemplate(
              new ContentProducer() {
                public void writeTo(final OutputStream outstream) throws IOException {
                  OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
                  writer.write(content[0]);
                  writer.flush();
                }
              });
      body.setContentType("text/plain; charset=UTF-8");
      response.setEntity(body);
    }
Ejemplo n.º 21
0
  protected SearchRequestResult parse_search(Document doc, int page) throws OpacErrorException {
    doc.setBaseUri(opac_url + "/Search/Results");

    if (doc.select("p.error, p.errorMsg, .alert-error").size() > 0) {
      throw new OpacErrorException(doc.select("p.error, p.errorMsg, .alert-error").text());
    } else if (doc.select("div.result").size() == 0 && doc.select(".main p").size() > 0) {
      throw new OpacErrorException(doc.select(".main p").first().text());
    }

    int rescount = -1;
    if (doc.select(".resulthead").size() == 1) {
      rescount = Integer.parseInt(doc.select(".resulthead strong").get(2).text());
    }
    List<SearchResult> reslist = new ArrayList<>();

    for (Element row : doc.select("div.result")) {
      SearchResult res = new SearchResult();
      Element z3988el = null;
      if (row.select("span.Z3988").size() == 1) {
        z3988el = row.select("span.3988").first();
      } else if (row.parent().tagName().equals("li")
          && row.parent().select("span.Z3988").size() > 0) {
        z3988el = row.parent().select("span.3988").first();
      }
      if (z3988el != null) {
        List<NameValuePair> z3988data;
        try {
          StringBuilder description = new StringBuilder();
          z3988data =
              URLEncodedUtils.parse(
                  new URI("http://dummy/?" + z3988el.select("span.Z3988").attr("title")), "UTF-8");
          for (NameValuePair nv : z3988data) {
            if (nv.getValue() != null) {
              if (!nv.getValue().trim().equals("")) {
                if (nv.getName().equals("rft.btitle")) {
                  description.append("<b>").append(nv.getValue()).append("</b>");
                } else if (nv.getName().equals("rft.atitle")) {
                  description.append("<b>").append(nv.getValue()).append("</b>");
                } else if (nv.getName().equals("rft.au")) {
                  description.append("<br />").append(nv.getValue());
                } else if (nv.getName().equals("rft.date")) {
                  description.append("<br />").append(nv.getValue());
                }
              }
            }
          }
          res.setInnerhtml(description.toString());
        } catch (URISyntaxException e) {
          e.printStackTrace();
        }
      } else {
        res.setInnerhtml(row.select("a.title").text());
      }

      if (row.hasClass("available") || row.hasClass("internet")) {
        res.setStatus(SearchResult.Status.GREEN);
      } else if (row.hasClass("reservable")) {
        res.setStatus(SearchResult.Status.YELLOW);
      } else if (row.hasClass("not-available")) {
        res.setStatus(SearchResult.Status.RED);
      } else if (row.select(".status.available").size() > 0) {
        res.setStatus(SearchResult.Status.GREEN);
      } else if (row.select(".status .label-success").size() > 0) {
        res.setStatus(SearchResult.Status.GREEN);
      } else if (row.select(".status .label-important").size() > 0) {
        res.setStatus(SearchResult.Status.RED);
      } else if (row.select(".status.checkedout").size() > 0) {
        res.setStatus(SearchResult.Status.RED);
      }

      for (Map.Entry<String, SearchResult.MediaType> entry : mediaTypeSelectors.entrySet()) {
        if (row.select(entry.getKey()).size() > 0) {
          res.setType(entry.getValue());
          break;
        }
      }

      for (Element img : row.select("img")) {
        String src = img.absUrl("src");
        if (src.contains("over")) {
          if (!src.contains("Unavailable")) {
            res.setCover(src);
          }
          break;
        }
      }

      res.setPage(page);
      String href = row.select("a.title").first().absUrl("href");
      try {
        URL idurl = new URL(href);
        String path = idurl.getPath();
        Matcher matcher = idPattern.matcher(path);
        if (matcher.find()) {
          res.setId(matcher.group(1));
        }
      } catch (MalformedURLException e) {
        e.printStackTrace();
      }
      reslist.add(res);
    }

    return new SearchRequestResult(reslist, rescount, page);
  }
Ejemplo n.º 22
0
  /**
   * API Access to cross dimensional data sets
   *
   * @param request List of data series required. Filter options
   * @param response Initially will return json only
   * @throws java.io.IOException
   */
  @GET
  public Object get(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ExecutionException, InterruptedException, URISyntaxException {

    Path requestPath = Path.newInstance(request);
    String cubeName = "";

    // Top level lists all cubes
    System.out.println(requestPath);
    if (requestPath.segments().size() == 1) {
      response.setCharacterEncoding("UTF8");
      response.setContentType("application/json");

      response.getWriter().println(DataCubeWriterJSON.dataCubeSetAsJSON(Root.cubeMaster));
      response.setStatus(HttpStatus.OK_200);
    }

    // Second level lists details of a single cube
    else if (requestPath.segments().size() == 2) {
      cubeName = requestPath.segments().get(1);
      if (Root.cubeMaster.cubes.containsKey(cubeName)) {
        response.setCharacterEncoding("UTF8");
        response.setContentType("application/json");
        response
            .getWriter()
            .println(
                DataCubeWriterJSON.dataCubeSummaryAsJSON(
                    Root.cubeMaster.cubes.get(cubeName).get()));
      }
      return null;
    }

    // Third level lists
    else if (requestPath.segments().size() == 3) {
      cubeName = requestPath.segments().get(1);
      if (Root.cubeMaster.cubes.containsKey(cubeName)) {
        String requestType = requestPath.segments().get(2);

        if (requestType.equalsIgnoreCase("data")) {
          String queryString = request.getQueryString();
          System.out.println(queryString);
          if (queryString == null) {
            response.setCharacterEncoding("UTF8");
            response.setContentType("application/json");
            response
                .getWriter()
                .println(
                    DataCubeWriterJSON.dataCubeAsJSON(Root.cubeMaster.cubes.get(cubeName).get()));
          } else {
            List<NameValuePair> parsedFilter =
                URLEncodedUtils.parse(request.getQueryString(), Charset.forName("utf8"));
            response.setContentType("application/json");
            response
                .getWriter()
                .println(
                    DataCubeWriterJSON.dataCubeSlicedAsJSON(
                        Root.cubeMaster.cubes.get(cubeName).get(), parsedFilter));
          }
        }
      }
      return null;
    }

    // Third level lists with parameters

    response.setStatus(HttpStatus.NOT_FOUND_404);
    return null;
  }
Ejemplo n.º 23
0
  // NOTE: handle() only handles over the wire (OTW) requests from integration.api.port 8096
  // If integration api port is not configured, actual OTW requests will be received by ApiServlet
  @SuppressWarnings({"unchecked", "rawtypes"})
  @Override
  public void handle(HttpRequest request, HttpResponse response, HttpContext context)
      throws HttpException, IOException {

    // Create StringBuffer to log information in access log
    StringBuffer sb = new StringBuffer();
    HttpServerConnection connObj = (HttpServerConnection) context.getAttribute("http.connection");
    if (connObj instanceof SocketHttpServerConnection) {
      InetAddress remoteAddr = ((SocketHttpServerConnection) connObj).getRemoteAddress();
      sb.append(remoteAddr.toString() + " -- ");
    }
    sb.append(StringUtils.cleanString(request.getRequestLine().toString()));

    try {
      List<NameValuePair> paramList = null;
      try {
        paramList = URLEncodedUtils.parse(new URI(request.getRequestLine().getUri()), "UTF-8");
      } catch (URISyntaxException e) {
        s_logger.error("Error parsing url request", e);
      }

      // Use Multimap as the parameter map should be in the form (name=String, value=String[])
      // So parameter values are stored in a list for the same name key
      // APITODO: Use Guava's (import com.google.common.collect.Multimap;)
      // (Immutable)Multimap<String, String> paramMultiMap = HashMultimap.create();
      // Map<String, Collection<String>> parameterMap = paramMultiMap.asMap();
      Map parameterMap = new HashMap<String, String[]>();
      String responseType = BaseCmd.RESPONSE_TYPE_XML;
      for (NameValuePair param : paramList) {
        if (param.getName().equalsIgnoreCase("response")) {
          responseType = param.getValue();
          continue;
        }
        parameterMap.put(param.getName(), new String[] {param.getValue()});
      }

      // Get the type of http method being used.
      parameterMap.put("httpmethod", new String[] {request.getRequestLine().getMethod()});

      // Check responseType, if not among valid types, fallback to JSON
      if (!(responseType.equals(BaseCmd.RESPONSE_TYPE_JSON)
          || responseType.equals(BaseCmd.RESPONSE_TYPE_XML))) {
        responseType = BaseCmd.RESPONSE_TYPE_XML;
      }

      try {
        // always trust commands from API port, user context will always be
        // UID_SYSTEM/ACCOUNT_ID_SYSTEM
        CallContext.register(_accountMgr.getSystemUser(), _accountMgr.getSystemAccount());
        sb.insert(
            0,
            "(userId="
                + User.UID_SYSTEM
                + " accountId="
                + Account.ACCOUNT_ID_SYSTEM
                + " sessionId="
                + null
                + ") ");
        String responseText = handleRequest(parameterMap, responseType, sb);
        sb.append(" 200 " + ((responseText == null) ? 0 : responseText.length()));

        writeResponse(response, responseText, HttpStatus.SC_OK, responseType, null);
      } catch (ServerApiException se) {
        String responseText = getSerializedApiError(se, parameterMap, responseType);
        writeResponse(
            response,
            responseText,
            se.getErrorCode().getHttpCode(),
            responseType,
            se.getDescription());
        sb.append(" " + se.getErrorCode() + " " + se.getDescription());
      } catch (RuntimeException e) {
        // log runtime exception like NullPointerException to help identify the source easier
        s_logger.error("Unhandled exception, ", e);
        throw e;
      }
    } finally {
      s_accessLogger.info(sb.toString());
      CallContext.unregister();
    }
  }
Ejemplo n.º 24
0
  /**
   * Parses the input row String into a Java object. For performance reasons this works in-place
   * updating the fields within this SnowPlowEventStruct, rather than creating a new one.
   *
   * @param row The raw String containing the row contents
   * @return true if row was successfully parsed, false otherwise
   * @throws SerDeException For any exception during parsing
   */
  public boolean updateByParsing(String row) throws SerDeException {

    // First we reset the object's fields
    nullify();

    // We have to handle any header rows
    if (row.startsWith("#Version:") || row.startsWith("#Fields:")) {
      return false; // Empty row will be discarded by Hive
    }

    final Matcher m = cfRegex.matcher(row);

    // 0. First check our row is kosher

    // -> is row from a CloudFront log? Will throw an exception if not
    if (!m.matches())
      throw new SerDeException("Row does not match expected CloudFront regexp pattern");

    // -> was generated by sp.js? If not (e.g. favicon.ico request), then silently return
    final String object = m.group(8);
    final String querystring = m.group(12);
    if (!(object.startsWith("/ice.png") || object.equals("/i") || object.startsWith("/i?"))
        || isNullField(querystring)) { // Also works if Forward Query String = yes
      return false;
    }

    // 1. Now we retrieve the fields which get directly passed through
    this.dt = m.group(1);
    this.tm = m.group(2); // CloudFront date format matches Hive's
    this.user_ipaddress = m.group(5);

    // 2. Now grab the user agent
    final String ua = m.group(11);
    try {
      this.useragent = decodeSafeString(ua);
    } catch (Exception e) {
      getLog().warn(e.getClass().getSimpleName() + " on { useragent: " + ua + " }");
    }

    // 3. Next we dis-assemble the user agent...
    final UserAgent userAgent = UserAgent.parseUserAgentString(ua);

    // -> browser fields
    final Browser b = userAgent.getBrowser();
    this.br_name = b.getName();
    this.br_family = b.getGroup().getName();
    final Version v = userAgent.getBrowserVersion();
    this.br_version = (v == null) ? null : v.getVersion();
    this.br_type = b.getBrowserType().getName();
    this.br_renderengine = b.getRenderingEngine().toString();

    // -> OS-related fields
    final OperatingSystem os = userAgent.getOperatingSystem();
    this.os_name = os.getName();
    this.os_family = os.getGroup().getName();
    this.os_manufacturer = os.getManufacturer().getName();

    // -> device/hardware-related fields
    this.dvce_type = os.getDeviceType().getName();
    this.dvce_ismobile = os.isMobileDevice();
    this.dvce_ismobile_bt = (byte) (this.dvce_ismobile ? 1 : 0);

    // 4. Now for the versioning (tracker versioning is handled below)
    this.v_collector = collectorVersion;
    this.v_etl = "serde-" + ProjectSettings.VERSION;

    // 5. Now we generate the event ID
    this.event_id = generateEventId();

    // 6. Now we dis-assemble the querystring.
    String qsUrl = null; // We use this in the block below, and afterwards
    List<NameValuePair> params = null; // We re-use this for efficiency
    try {
      params = URLEncodedUtils.parse(URI.create("http://localhost/?" + querystring), cfEncoding);

      // For performance, don't convert to a map, just loop through and match to our variables as we
      // go
      for (NameValuePair pair : params) {

        final String name = pair.getName();
        final String value = pair.getValue();

        final QuerystringFields field;
        try {
          field =
              QuerystringFields.valueOf(
                  name.toUpperCase()); // Java pre-7 can't switch on a string, so hash the string
        } catch (IllegalArgumentException e) {
          getLog().warn("Unexpected params { " + name + ": " + value + " }");
          continue;
        }

        try {
          switch (field) {
              // Common fields
            case E:
              this.event = asEventType(value);
              break;
            case IP:
              this.user_ipaddress = value;
              break;
            case AID:
              this.app_id = value;
              break;
            case P:
              this.platform = value;
              break;
            case TID:
              this.txn_id = value;
              break;
            case UID:
              this.user_id = value;
              break;
            case FP:
              this.user_fingerprint = value;
              break;
            case VID:
              this.visit_id = Integer.parseInt(value);
              break;
            case TSTAMP:
              // Replace our timestamp fields with the client's timestamp
              String[] timestamp = value.split(" ");
              this.dt = timestamp[0];
              this.tm = timestamp[1];
              break;
            case TV:
              this.v_tracker = value;
              break;
            case LANG:
              this.br_lang = value;
              break;
            case F_PDF:
              if ((this.br_features_pdf = stringToByte(value)) == 1) this.br_features.add("pdf");
              break;
            case F_FLA:
              if ((this.br_features_flash = stringToByte(value)) == 1) this.br_features.add("fla");
              break;
            case F_JAVA:
              if ((this.br_features_java = stringToByte(value)) == 1) this.br_features.add("java");
              break;
            case F_DIR:
              if ((this.br_features_director = stringToByte(value)) == 1)
                this.br_features.add("dir");
              break;
            case F_QT:
              if ((this.br_features_quicktime = stringToByte(value)) == 1)
                this.br_features.add("qt");
              break;
            case F_REALP:
              if ((this.br_features_realplayer = stringToByte(value)) == 1)
                this.br_features.add("realp");
              break;
            case F_WMA:
              if ((this.br_features_windowsmedia = stringToByte(value)) == 1)
                this.br_features.add("wma");
              break;
            case F_GEARS:
              if ((this.br_features_gears = stringToByte(value)) == 1)
                this.br_features.add("gears");
              break;
            case F_AG:
              if ((this.br_features_silverlight = stringToByte(value)) == 1)
                this.br_features.add("ag");
              break;
            case COOKIE:
              this.br_cookies = stringToBoolean(value);
              this.br_cookies_bt = (byte) (this.br_cookies ? 1 : 0);
              break;
            case RES:
              String[] resolution = value.split("x");
              if (resolution.length != 2) throw new Exception("Couldn't parse res field");
              this.dvce_screenwidth = Integer.parseInt(resolution[0]);
              this.dvce_screenheight = Integer.parseInt(resolution[1]);
              break;
            case CD:
              this.br_colordepth = value;
              break;
            case TZ:
              this.os_timezone = decodeSafeString(value);
              break;
            case REFR:
              this.page_referrer = decodeSafeString(value);
              break;
            case URL:
              qsUrl = pair.getValue(); // We might use this later for the page URL
              break;

              // Page-view only
            case PAGE:
              this.page_title = decodeSafeString(value);
              break;

              // Event only
            case EV_CA:
              this.ev_category = decodeSafeString(value);
              break;
            case EV_AC:
              this.ev_action = decodeSafeString(value);
              break;
            case EV_LA:
              this.ev_label = decodeSafeString(value);
              break;
            case EV_PR:
              this.ev_property = decodeSafeString(value);
              break;
            case EV_VA:
              this.ev_value = decodeSafeString(value);
              break;

              // Ecommerce
            case TR_ID:
              this.tr_orderid = decodeSafeString(value);
              break;
            case TR_AF:
              this.tr_affiliation = decodeSafeString(value);
              break;
            case TR_TT:
              this.tr_total = decodeSafeString(value);
              break;
            case TR_TX:
              this.tr_tax = decodeSafeString(value);
              break;
            case TR_SH:
              this.tr_shipping = decodeSafeString(value);
              break;
            case TR_CI:
              this.tr_city = decodeSafeString(value);
              break;
            case TR_ST:
              this.tr_state = decodeSafeString(value);
              break;
            case TR_CO:
              this.tr_country = decodeSafeString(value);
              break;
            case TI_ID:
              this.ti_orderid = decodeSafeString(value);
              break;
            case TI_SK:
              this.ti_sku = decodeSafeString(value);
              break;
            case TI_NA:
              this.ti_name = decodeSafeString(value);
              break;
            case TI_CA:
              this.ti_category = decodeSafeString(value);
              break;
            case TI_PR:
              this.ti_price = decodeSafeString(value);
              break;
            case TI_QU:
              this.ti_quantity = decodeSafeString(value);
              break;
          }
        } catch (Exception e) {
          getLog().warn(e.getClass().getSimpleName() + " on { " + name + ": " + value + " }");
        }
      }
    } catch (IllegalArgumentException e) {
      getLog().warn("Corrupted querystring { " + querystring + " }");
    }

    // 7. Choose the page_url
    final String cfUrl = m.group(10);
    if (!isNullField(cfUrl)) { // CloudFront didn't provide the URL as cs(Referer)
      this.page_url = cfUrl; // The CloudFront cs(Referer) URL
    } else {
      try {
        this.page_url =
            decodeSafeString(
                qsUrl); // Use the decoded querystring URL. Might be null (returned as null)
      } catch (Exception e) {
        getLog().warn(e.getClass().getSimpleName() + " on { qsUrl: " + qsUrl + " }");
      }
    }

    // 8. Finally handle the marketing fields in the page_url
    // Re-use params to avoid creating another object
    if (this.page_url != null) {
      params = null;
      try {
        params = URLEncodedUtils.parse(URI.create(this.page_url), "UTF-8");
      } catch (IllegalArgumentException e) {
        getLog().warn("Couldn't parse page_url: " + page_url);
      }
      if (params != null) {
        // For performance, don't convert to a map, just loop through and match to our variables as
        // we go
        for (NameValuePair pair : params) {

          final String name = pair.getName();
          final String value = pair.getValue();

          final MarketingFields field;
          try {
            field =
                MarketingFields.valueOf(
                    name.toUpperCase()); // Java pre-7 can't switch on a string, so hash the string
          } catch (IllegalArgumentException e) {
            // Do nothing: non-marketing related querystring fields are not an issue.
            continue;
          }

          try {
            switch (field) {
                // Marketing fields
              case UTM_MEDIUM:
                this.mkt_medium = decodeSafeString(value);
                break;
              case UTM_SOURCE:
                this.mkt_source = decodeSafeString(value);
                break;
              case UTM_TERM:
                this.mkt_term = decodeSafeString(value);
                break;
              case UTM_CONTENT:
                this.mkt_content = decodeSafeString(value);
                break;
              case UTM_CAMPAIGN:
                this.mkt_campaign = decodeSafeString(value);
                break;
            }
          } catch (Exception e) {
            getLog().warn(e.getClass().getSimpleName() + " on { " + name + ": " + value + " }");
          }
        }
      }
    }

    return true; // Successfully updated the row.
  }
Ejemplo n.º 25
0
    public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext)
        throws HttpException, IOException {

      SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context);
      final String uri = URLDecoder.decode(request.getRequestLine().getUri());
      final List<NameValuePair> params = URLEncodedUtils.parse(URI.create(uri), "UTF-8");
      String result = "Error";

      if (params.size() > 0) {
        try {

          // Set the configuration
          if (params.get(0).getName().equals("set")) {
            Editor editor = settings.edit();
            editor.putBoolean("stream_audio", false);
            editor.putBoolean("stream_video", false);
            for (Iterator<NameValuePair> it = params.iterator(); it.hasNext(); ) {
              NameValuePair param = it.next();
              if (param.getName().equals("h263") || param.getName().equals("h264")) {
                editor.putBoolean("stream_video", true);
                Session.defaultVideoQuality = VideoQuality.parseQuality(param.getValue());
                editor.putInt("video_resX", Session.defaultVideoQuality.resX);
                editor.putInt("video_resY", Session.defaultVideoQuality.resY);
                editor.putString(
                    "video_framerate", String.valueOf(Session.defaultVideoQuality.frameRate));
                editor.putString(
                    "video_bitrate", String.valueOf(Session.defaultVideoQuality.bitRate / 1000));
                editor.putString("video_encoder", param.getName().equals("h263") ? "2" : "1");
              }
              if (param.getName().equals("amr") || param.getName().equals("aac")) {
                editor.putBoolean("stream_audio", true);
                Session.defaultVideoQuality = VideoQuality.parseQuality(param.getValue());
                editor.putString("audio_encoder", param.getName().equals("amr") ? "3" : "5");
              }
            }
            editor.commit();
            result = "[]";
          }

          // Send the current streaming configuration to the client
          else if (params.get(0).getName().equals("get")) {
            result =
                "{\""
                    + HX_DETECt_TAG
                    + "\": true,"
                    + "\"streamAudio\":"
                    + settings.getBoolean("stream_audio", false)
                    + ","
                    + "\"audioEncoder\":\""
                    + (Integer.parseInt(settings.getString("audio_encoder", "3")) == 3
                        ? "AMR-NB"
                        : "AAC")
                    + "\","
                    + "\"streamVideo\":"
                    + settings.getBoolean("stream_video", true)
                    + ","
                    + "\"videoEncoder\":\""
                    + (Integer.parseInt(settings.getString("video_encoder", "2")) == 2
                        ? "H.263"
                        : "H.264")
                    + "\","
                    + "\"videoResolution\":\""
                    + settings.getInt("video_resX", Session.defaultVideoQuality.resX)
                    + "x"
                    + settings.getInt("video_resY", Session.defaultVideoQuality.resY)
                    + "\","
                    + "\"videoFramerate\":\""
                    + settings.getString(
                        "video_framerate", String.valueOf(Session.defaultVideoQuality.frameRate))
                    + " fps\","
                    + "\"videoBitrate\":\""
                    + settings.getString(
                        "video_bitrate", String.valueOf(Session.defaultVideoQuality.bitRate / 1000))
                    + " kbps\"}";
          }
        } catch (Exception e) {
          Log.e(TAG, "Error !");
          e.printStackTrace();
        }
      }

      final String finalResult = result;
      EntityTemplate body =
          new EntityTemplate(
              new ContentProducer() {
                public void writeTo(final OutputStream outstream) throws IOException {
                  OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
                  writer.write(finalResult);
                  writer.flush();
                }
              });

      response.setStatusCode(HttpStatus.SC_OK);
      body.setContentType("application/json; charset=UTF-8");
      response.setEntity(body);
    }