protected String buildStatsUrl(StatsParameters searchParameters, String baseUrl, Locale locale)
      throws UnsupportedEncodingException {
    URLCodec encoder = new URLCodec();
    StringBuilder url = new StringBuilder();
    String languageUrlFragment = extractLanguageFragment(searchParameters.getLanguage());

    url.append(baseUrl);
    url.append("/").append(languageUrlFragment);
    url.append("?wt=").append(encoder.encode("json", "UTF-8"));
    url.append("&locale=").append(encoder.encode(locale.toString(), "UTF-8"));

    url.append(buildSortParameters(searchParameters, encoder));

    url.append("&stats=true");
    url.append("&rows=0");
    if (!StringUtils.isBlank(searchParameters.getFilterQuery())) {
      url.append("?fq=").append(encoder.encode(searchParameters.getFilterQuery(), "UTF-8"));
    }

    for (Entry<String, String> entry : searchParameters.getStatsParameters().entrySet()) {
      url.append("&stats.")
          .append(entry.getKey())
          .append("=")
          .append(encoder.encode(entry.getValue(), "UTF-8"));
    }

    return url.toString();
  }
 private StringBuffer buildSortParameters(BasicSearchParameters searchParameters, URLCodec encoder)
     throws UnsupportedEncodingException {
   StringBuffer sortBuffer = new StringBuffer();
   for (SortDefinition sortDefinition : searchParameters.getSortDefinitions()) {
     if (sortBuffer.length() == 0) {
       sortBuffer.append("&sort=");
     } else {
       sortBuffer.append(encoder.encode(", ", "UTF-8"));
     }
     // MNT-8557 fix, manually replace ' ' with '%20'
     // The sort can be different, see MNT-13742
     switch (sortDefinition.getSortType()) {
       case DOCUMENT:
         sortBuffer
             .append(encoder.encode("_docid_", "UTF-8"))
             .append(encoder.encode(" ", "UTF-8"));
         break;
       case SCORE:
         sortBuffer.append(encoder.encode("score", "UTF-8")).append(encoder.encode(" ", "UTF-8"));
         break;
       case FIELD:
       default:
         sortBuffer
             .append(encoder.encode(sortDefinition.getField().replaceAll(" ", "%20"), "UTF-8"))
             .append(encoder.encode(" ", "UTF-8"));
         break;
     }
     if (sortDefinition.isAscending()) {
       sortBuffer.append(encoder.encode("asc", "UTF-8"));
     } else {
       sortBuffer.append(encoder.encode("desc", "UTF-8"));
     }
   }
   return sortBuffer;
 }
 @Override
 public String createSearchString(File file) {
   String fileBaseName;
   if (file.isFile()) fileBaseName = FilenameUtils.getBaseName(Movie.getUnstackedMovieName(file));
   else fileBaseName = file.getName();
   String[] splitBySpace = fileBaseName.split(" ");
   if (splitBySpace.length > 1) {
     // check if last word in filename contains a year like (2012) or [2012]
     // we want to remove this from our search because it freaks out the search on excalibur films
     // and gives no results
     if (splitBySpace[splitBySpace.length - 1].matches("[\\(\\[]\\d{4}[\\)\\]]")) {
       fileBaseName = fileBaseName.replaceFirst("[\\(\\[]\\d{4}[\\)\\]]", "").trim();
     }
   }
   URLCodec codec = new URLCodec();
   try {
     fileBaseName = codec.encode(fileBaseName);
   } catch (EncoderException e) {
     e.printStackTrace();
   }
   fileBaseName =
       "http://www.excaliburfilms.com/search/adultSearch.htm?searchString="
           + fileBaseName
           + "&Case=ExcalMovies&Search=AdultDVDMovies&SearchFor=Title.x";
   return fileBaseName;
 }
 protected static String _encode(String s) {
   try {
     return codec.encode(s, "UTF-8");
   } catch (UnsupportedEncodingException e) {
     return s; // should not happen
   }
 }
Esempio n. 5
0
 /**
  * @throws RuntimeException Thrown if URL encoding is unsuccessful. This only happens in the case
  *     of a UnsupportedEncodingException which should never occur in reality.
  */
 public static String encodeURL(String url) {
   try {
     return urlCodec.encode(url);
   } catch (EncoderException exp) {
     //            NLogger.error( URLCodecUtils.class, exp, exp );
     throw new RuntimeException(exp.toString() + ": " + exp.getMessage());
   }
 }
Esempio n. 6
0
 public static String encodeURL(String str) {
   URLCodec codec = new URLCodec();
   try {
     return codec.encode(str).replaceAll("\\+", "%20");
   } catch (EncoderException ee) {
     return str;
   }
 }
 protected String encode(String reference) {
   synchronized (urlCodec) {
     try {
       return urlCodec.encode(reference);
     } catch (EncoderException e) {
       log.error("encode() caught exception", e);
       throw new RuntimeException(e);
     }
   }
 }
  /**
   * Get a cache file to be used for the given parameters. This function will not create the file on
   * disk
   *
   * @param genomeName the genome name
   * @param strain1 the 1st strain
   * @param strain2 the 2nd strain
   * @param chromosomeNumber the chromosome number
   * @param minimumExtentInSnps minimum extent in snps
   * @param minimumExtentInBasePairs minimum extent in base pairs
   * @return the file
   */
  private File getCacheFile(
      String genomeName,
      String strain1,
      String strain2,
      int chromosomeNumber,
      int minimumExtentInSnps,
      long minimumExtentInBasePairs) {
    final String lesserStrain;
    final String greaterStrain;
    if (strain1.compareTo(strain2) >= 0) {
      lesserStrain = strain2;
      greaterStrain = strain1;
    } else {
      lesserStrain = strain1;
      greaterStrain = strain2;
    }

    String cacheHashString =
        genomeName
            + CONCATINATION_STRING
            + lesserStrain
            + CONCATINATION_STRING
            + greaterStrain
            + CONCATINATION_STRING
            + chromosomeNumber
            + CONCATINATION_STRING
            + minimumExtentInSnps
            + CONCATINATION_STRING
            + minimumExtentInBasePairs;

    try {
      cacheHashString = URL_CODEC.encode(cacheHashString);
    } catch (EncoderException ex) {
      LOG.log(Level.WARNING, "File encoding failed... continuing", ex);
    }

    File directory = new File(System.getProperty("java.io.tmpdir"));
    return new File(directory, cacheHashString);
  }
  public ResultSet executeQuery(final SearchParameters searchParameters, String language) {
    if (repositoryState.isBootstrapping()) {
      throw new AlfrescoRuntimeException(
          "SOLR queries can not be executed while the repository is bootstrapping");
    }

    try {
      StoreRef store = extractStoreRef(searchParameters);
      SolrStoreMappingWrapper mapping = extractMapping(store);
      Locale locale = extractLocale(searchParameters);

      URLCodec encoder = new URLCodec();
      StringBuilder url = new StringBuilder();

      Pair<HttpClient, String> httpClientAndBaseUrl = mapping.getHttpClientAndBaseUrl();
      HttpClient httpClient = httpClientAndBaseUrl.getFirst();

      url.append(httpClientAndBaseUrl.getSecond());

      String languageUrlFragment = extractLanguageFragment(language);
      url.append("/").append(languageUrlFragment);

      // Send the query in JSON only
      // url.append("?q=");
      // url.append(encoder.encode(searchParameters.getQuery(), "UTF-8"));
      url.append("?wt=").append(encoder.encode("json", "UTF-8"));
      url.append("&fl=").append(encoder.encode("DBID,score", "UTF-8"));

      if ((searchParameters.getStores().size() > 1) || (mapping.isSharded())) {
        boolean requiresSeparator = false;
        url.append("&shards=");
        for (StoreRef storeRef : searchParameters.getStores()) {
          SolrStoreMappingWrapper storeMapping = extractMapping(storeRef);

          if (requiresSeparator) {
            url.append(',');
          } else {
            requiresSeparator = true;
          }

          url.append(storeMapping.getShards());
        }
      }

      // Emulate old limiting behaviour and metadata
      final LimitBy limitBy;
      int maxResults = -1;
      if (searchParameters.getMaxItems() >= 0) {
        maxResults = searchParameters.getMaxItems();
        limitBy = LimitBy.FINAL_SIZE;
      } else if (searchParameters.getLimitBy() == LimitBy.FINAL_SIZE
          && searchParameters.getLimit() >= 0) {
        maxResults = searchParameters.getLimit();
        limitBy = LimitBy.FINAL_SIZE;
      } else {
        maxResults = searchParameters.getMaxPermissionChecks();
        if (maxResults < 0) {
          maxResults = maximumResultsFromUnlimitedQuery;
        }
        limitBy = LimitBy.NUMBER_OF_PERMISSION_EVALUATIONS;
      }
      url.append("&rows=").append(String.valueOf(maxResults));

      url.append("&df=").append(encoder.encode(searchParameters.getDefaultFieldName(), "UTF-8"));
      url.append("&start=").append(encoder.encode("" + searchParameters.getSkipCount(), "UTF-8"));

      url.append("&locale=");
      url.append(encoder.encode(locale.toString(), "UTF-8"));
      url.append("&")
          .append(SearchParameters.ALTERNATIVE_DICTIONARY)
          .append("=")
          .append(alternativeDictionary);
      for (String paramName : searchParameters.getExtraParameters().keySet()) {
        url.append("&")
            .append(paramName)
            .append("=")
            .append(searchParameters.getExtraParameters().get(paramName));
      }
      StringBuffer sortBuffer = buildSortParameters(searchParameters, encoder);
      url.append(sortBuffer);

      if (searchParameters.getPermissionEvaluation() != PermissionEvaluationMode.NONE) {
        url.append("&fq=").append(encoder.encode("{!afts}AUTHORITY_FILTER_FROM_JSON", "UTF-8"));
      }

      if (searchParameters.getExcludeTenantFilter() == false) {
        url.append("&fq=").append(encoder.encode("{!afts}TENANT_FILTER_FROM_JSON", "UTF-8"));
      }

      if (searchParameters.getFieldFacets().size() > 0) {
        url.append("&facet=").append(encoder.encode("true", "UTF-8"));
        for (FieldFacet facet : searchParameters.getFieldFacets()) {
          url.append("&facet.field=").append(encoder.encode(facet.getField(), "UTF-8"));
          if (facet.getEnumMethodCacheMinDF() != 0) {
            url.append("&")
                .append(
                    encoder.encode("f." + facet.getField() + ".facet.enum.cache.minDf", "UTF-8"))
                .append("=")
                .append(encoder.encode("" + facet.getEnumMethodCacheMinDF(), "UTF-8"));
          }
          url.append("&")
              .append(encoder.encode("f." + facet.getField() + ".facet.limit", "UTF-8"))
              .append("=")
              .append(encoder.encode("" + facet.getLimit(), "UTF-8"));
          if (facet.getMethod() != null) {
            url.append("&")
                .append(encoder.encode("f." + facet.getField() + ".facet.method", "UTF-8"))
                .append("=")
                .append(
                    encoder.encode(
                        facet.getMethod() == FieldFacetMethod.ENUM ? "enum" : "fc", "UTF-8"));
          }
          if (facet.getMinCount() != 0) {
            url.append("&")
                .append(encoder.encode("f." + facet.getField() + ".facet.mincount", "UTF-8"))
                .append("=")
                .append(encoder.encode("" + facet.getMinCount(), "UTF-8"));
          }
          if (facet.getOffset() != 0) {
            url.append("&")
                .append(encoder.encode("f." + facet.getField() + ".facet.offset", "UTF-8"))
                .append("=")
                .append(encoder.encode("" + facet.getOffset(), "UTF-8"));
          }
          if (facet.getPrefix() != null) {
            url.append("&")
                .append(encoder.encode("f." + facet.getField() + ".facet.prefix", "UTF-8"))
                .append("=")
                .append(encoder.encode("" + facet.getPrefix(), "UTF-8"));
          }
          if (facet.getSort() != null) {
            url.append("&")
                .append(encoder.encode("f." + facet.getField() + ".facet.sort", "UTF-8"))
                .append("=")
                .append(
                    encoder.encode(
                        facet.getSort() == FieldFacetSort.COUNT ? "count" : "index", "UTF-8"));
          }
        }
        for (String facetQuery : searchParameters.getFacetQueries()) {
          url.append("&facet.query=").append(encoder.encode("{!afts}" + facetQuery, "UTF-8"));
        }
      }
      // end of field facets

      final String searchTerm = searchParameters.getSearchTerm();
      String spellCheckQueryStr = null;
      if (searchTerm != null && searchParameters.isSpellCheck()) {
        StringBuilder builder = new StringBuilder();
        builder.append("&spellcheck.q=").append(encoder.encode(searchTerm, "UTF-8"));
        builder.append("&spellcheck=").append(encoder.encode("true", "UTF-8"));
        spellCheckQueryStr = builder.toString();
        url.append(spellCheckQueryStr);
      }

      JSONObject body = new JSONObject();
      body.put("query", searchParameters.getQuery());

      // Authorities go over as is - and tenant mangling and query building takes place on the SOLR
      // side

      Set<String> allAuthorisations = permissionService.getAuthorisations();
      boolean includeGroups =
          includeGroupsForRoleAdmin
              ? true
              : !allAuthorisations.contains(PermissionService.ADMINISTRATOR_AUTHORITY);

      JSONArray authorities = new JSONArray();
      for (String authority : allAuthorisations) {
        if (includeGroups) {
          authorities.put(authority);
        } else {
          if (AuthorityType.getAuthorityType(authority) != AuthorityType.GROUP) {
            authorities.put(authority);
          }
        }
      }
      body.put("authorities", authorities);
      body.put("anyDenyDenies", anyDenyDenies);

      JSONArray tenants = new JSONArray();
      tenants.put(tenantService.getCurrentUserDomain());
      body.put("tenants", tenants);

      JSONArray locales = new JSONArray();
      for (Locale currentLocale : searchParameters.getLocales()) {
        locales.put(DefaultTypeConverter.INSTANCE.convert(String.class, currentLocale));
      }
      if (locales.length() == 0) {
        locales.put(I18NUtil.getLocale());
      }
      body.put("locales", locales);

      JSONArray templates = new JSONArray();
      for (String templateName : searchParameters.getQueryTemplates().keySet()) {
        JSONObject template = new JSONObject();
        template.put("name", templateName);
        template.put("template", searchParameters.getQueryTemplates().get(templateName));
        templates.put(template);
      }
      body.put("templates", templates);

      JSONArray allAttributes = new JSONArray();
      for (String attribute : searchParameters.getAllAttributes()) {
        allAttributes.put(attribute);
      }
      body.put("allAttributes", allAttributes);

      body.put("defaultFTSOperator", searchParameters.getDefaultFTSOperator());
      body.put("defaultFTSFieldOperator", searchParameters.getDefaultFTSFieldOperator());
      body.put("queryConsistency", searchParameters.getQueryConsistency());
      if (searchParameters.getMlAnalaysisMode() != null) {
        body.put("mlAnalaysisMode", searchParameters.getMlAnalaysisMode().toString());
      }
      body.put("defaultNamespace", searchParameters.getNamespace());

      JSONArray textAttributes = new JSONArray();
      for (String attribute : searchParameters.getTextAttributes()) {
        textAttributes.put(attribute);
      }
      body.put("textAttributes", textAttributes);

      final int maximumResults = maxResults; // just needed for the final parameter

      return (ResultSet)
          postSolrQuery(
              httpClient,
              url.toString(),
              body,
              new SolrJsonProcessor<SolrJSONResultSet>() {

                @Override
                public SolrJSONResultSet getResult(JSONObject json) {
                  return new SolrJSONResultSet(
                      json, searchParameters, nodeService, nodeDAO, limitBy, maximumResults);
                }
              },
              spellCheckQueryStr);
    } catch (UnsupportedEncodingException e) {
      throw new LuceneQueryParserException("", e);
    } catch (HttpException e) {
      throw new LuceneQueryParserException("", e);
    } catch (IOException e) {
      throw new LuceneQueryParserException("", e);
    } catch (JSONException e) {
      throw new LuceneQueryParserException("", e);
    }
  }
  /**
   * This method adds file or folder to tags list. 'target' param can be either 'file' or 'folder'
   * depending on what do you want to add, 'target_id' is the id of a file or folder to be added,
   * 'tags' array of tags where target will be added.
   *
   * <p>On successful a result, you will receive 'addtotag_ok'. If the result wasn't successful,
   * status field can be: addtotag_error.
   *
   * @param addToTagRequest request
   * @return response
   * @throws IOException IO exception
   * @throws BoxException box exception
   */
  public AddToTagResponse addToTag(AddToTagRequest addToTagRequest)
      throws IOException, BoxException {
    AddToTagResponse baseBoxResponse = BoxResponseFactory.createAddToTagResponse();

    String apiKey = addToTagRequest.getApiKey();
    String authToken = addToTagRequest.getAuthToken();
    String target = addToTagRequest.getTarget();
    String targetId = addToTagRequest.getTargetId();
    String[] tags = addToTagRequest.getTags();

    if (BoxConstant.CONFIG_API_REQUEST_FORMAT_REST.equals(apiRequestFormat)) {

      URLCodec codec = new URLCodec();
      if (tags != null) {
        for (int i = 0; i < tags.length; i++) {
          String tag = tags[i];
          tag = codec.encode(tag, "ISO-8859-1");
          tags[i] = tag;
        }
      }
      StringBuffer urlBuff = super.getRestUrl(BoxConstant.ACTION_NAME_ADD_TO_TAG);
      urlBuff.append(BoxConstant.AND_SIGN_STRING);
      urlBuff.append(BoxConstant.PARAM_NAME_API_KEY);
      urlBuff.append(BoxConstant.EQUALS_SIGN_STRING);
      urlBuff.append(apiKey);
      urlBuff.append(BoxConstant.AND_SIGN_STRING);
      urlBuff.append(BoxConstant.PARAM_NAME_AUTH_TOKEN);
      urlBuff.append(BoxConstant.EQUALS_SIGN_STRING);
      urlBuff.append(authToken);
      if (tags == null) {
        urlBuff.append(BoxConstant.AND_SIGN_STRING);
        urlBuff.append(BoxConstant.PARAM_NAME_PARAMS_TAGS);
        urlBuff.append(BoxConstant.EQUALS_SIGN_STRING);
      } else {
        for (int i = 0; i < tags.length; i++) {
          String tag = tags[i];
          urlBuff.append(BoxConstant.AND_SIGN_STRING);
          urlBuff.append(BoxConstant.PARAM_NAME_PARAMS_TAGS);
          urlBuff.append(BoxConstant.EQUALS_SIGN_STRING);
          urlBuff.append(tag);
        }
      }
      urlBuff.append(BoxConstant.AND_SIGN_STRING);
      urlBuff.append(BoxConstant.PARAM_NAME_TARGET);
      urlBuff.append(BoxConstant.EQUALS_SIGN_STRING);
      urlBuff.append(target);
      urlBuff.append(BoxConstant.AND_SIGN_STRING);
      urlBuff.append(BoxConstant.PARAM_NAME_TARGET_ID);
      urlBuff.append(BoxConstant.EQUALS_SIGN_STRING);
      urlBuff.append(targetId);
      try {
        Document doc = httpManager.doGet(urlBuff.toString());
        Element responseElm = doc.getRootElement();
        Element statusElm = responseElm.element(BoxConstant.PARAM_NAME_STATUS);
        String status = statusElm.getText();
        baseBoxResponse.setStatus(status);
        //                if (BoxConstant.STATUS_ADD_TO_TAG_OK.equals(status)) {
        //                }
      } catch (DocumentException e) {
        BoxException be = new BoxException("failed to parse to a document.", e);
        be.setStatus(baseBoxResponse.getStatus());
        throw be;
      }

    } else if (BoxConstant.CONFIG_API_REQUEST_FORMAT_XML.equals(apiRequestFormat)) {
      Document document = DocumentHelper.createDocument();
      Element requestElm = DocumentHelper.createElement(BoxConstant.PARAM_NAME_REQUEST);
      document.add(requestElm);

      Element actionElm = DocumentHelper.createElement(BoxConstant.PARAM_NAME_ACTION);
      Element apiKeyElm = DocumentHelper.createElement(BoxConstant.PARAM_NAME_API_KEY);
      Element authTokenElm = DocumentHelper.createElement(BoxConstant.PARAM_NAME_AUTH_TOKEN);
      Element tagsElm = DocumentHelper.createElement(BoxConstant.PARAM_NAME_TAGS);
      Element targetElm = DocumentHelper.createElement(BoxConstant.PARAM_NAME_TARGET);
      Element targetIdElm = DocumentHelper.createElement(BoxConstant.PARAM_NAME_TARGET_ID);
      requestElm.add(actionElm);
      requestElm.add(apiKeyElm);
      requestElm.add(authTokenElm);
      requestElm.add(tagsElm);
      requestElm.add(targetElm);
      requestElm.add(targetIdElm);
      //
      actionElm.setText(BoxConstant.ACTION_NAME_ADD_TO_TAG);
      apiKeyElm.setText(apiKey);
      authTokenElm.setText(authToken);
      targetElm.setText(target);
      targetIdElm.setText(targetId);
      if (tags != null) {
        for (int i = 0; i < tags.length; i++) {
          String tag = tags[i];
          Element tagElm = DocumentHelper.createElement(BoxConstant.PARAM_NAME_ITEM);
          tagElm.setText(tag);
          tagsElm.add(tagElm);
        }
      }
      String result = httpManager.doPostXML(xmlApiUrl, document.asXML());

      try {
        Document doc = DocumentHelper.parseText(result);
        Element responseElm = doc.getRootElement();
        Element statusElm = responseElm.element(BoxConstant.PARAM_NAME_STATUS);
        String status = statusElm.getText();
        baseBoxResponse.setStatus(status);
        //                if (BoxConstant.STATUS_ADD_TO_MY_BOX_OK.equals(status)) {
        //                }
      } catch (DocumentException e) {
        BoxException be = new BoxException("failed to parse to a document.", e);
        be.setStatus(baseBoxResponse.getStatus());
        throw be;
      }

    } else if (BoxConstant.CONFIG_API_REQUEST_FORMAT_SOAP.equals(apiRequestFormat)) {

    } else {
    }
    return baseBoxResponse;
  }