Java Code Examples for org.apache.solr.common.StringUtils

The following examples show how to use org.apache.solr.common.StringUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: istio-apim   Source File: IstioExecutor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Build the config for the client
 */
private Config buildConfig() throws APIManagementException {

    System.setProperty(TRY_KUBE_CONFIG, "false");
    System.setProperty(TRY_SERVICE_ACCOUNT, "true");
    ConfigBuilder configBuilder;

    configBuilder = new ConfigBuilder().withMasterUrl(kubernetesAPIServerUrl);

    if (!StringUtils.isEmpty(saTokenFileName)) {
        String token;
        String tokenFile = saTokenFilePath + "/" + saTokenFileName;
        try {
            token = FileUtil.readFileToString(tokenFile);
        } catch (IOException e) {
            throw new APIManagementException("Error while reading the SA Token FIle " + tokenFile);
        }
        configBuilder.withOauthToken(token);
    }

    return configBuilder.build();
}
 
Example 2
Source Project: istio-apim   Source File: IstioExecutor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Verify whether the endpoint url is non empty
 *
 * @param endpoints Endpoint objects
 * @return Returns if endpoint is not empty
 */
private static boolean isEndpointURLNonEmpty(Object endpoints) {
    if (endpoints instanceof JSONObject) {
        JSONObject endpointJson = (JSONObject) endpoints;
        if (endpointJson.containsKey("url") && endpointJson.get("url") != null) {
            String url = endpointJson.get("url").toString();
            if (org.apache.commons.lang.StringUtils.isNotBlank(url)) {
                return true;
            }
        }
    } else if (endpoints instanceof org.json.simple.JSONArray) {
        org.json.simple.JSONArray endpointsJson = (JSONArray) endpoints;

        for (int i = 0; i < endpointsJson.size(); ++i) {
            if (isEndpointURLNonEmpty(endpointsJson.get(i))) {
                return true;
            }
        }
    }

    return false;
}
 
Example 3
Source Project: istio-apim   Source File: IstioExecutor.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Extract Endpoint url from the endpoints object
 *
 * @param endpoints Endpoints object
 * @return Returns endpoint url
 */
private String getEPUrl(Object endpoints) {

    if (endpoints instanceof JSONObject) {
        JSONObject endpointJson = (JSONObject) endpoints;
        if (endpointJson.containsKey("url") && endpointJson.get("url") != null) {
            String url = endpointJson.get("url").toString();
            if (org.apache.commons.lang.StringUtils.isNotBlank(url)) {
                return url;
            }
        }
    } else if (endpoints instanceof org.json.simple.JSONArray) {
        org.json.simple.JSONArray endpointsJson = (JSONArray) endpoints;

        for (int i = 0; i < endpointsJson.size(); ++i) {
            if (isEndpointURLNonEmpty(endpointsJson.get(i))) {
                return endpointsJson.get(i).toString();
            }
        }
    }

    return "";
}
 
Example 4
Source Project: customized-symspell   Source File: SpellcheckComponent.java    License: MIT License 6 votes vote down vote up
private void loadDefault(String unigramsFile, String bigramsFile, String exclusionsFile,
    SpellChecker spellChecker,
    SolrCore core, String exclusionListSperatorRegex) {
  try {
    if (!StringUtils.isEmpty(unigramsFile)) {
      loadUniGramFile(core.getResourceLoader().openResource(unigramsFile),
          spellChecker.getDataHolder());
    }

    if (!StringUtils.isEmpty(bigramsFile)) {
      loadBiGramFile(core.getResourceLoader().openResource(bigramsFile),
          spellChecker.getDataHolder());
    }

    if (!StringUtils.isEmpty(exclusionsFile)) {
      loadExclusions(core.getResourceLoader().openResource(exclusionsFile),
          spellChecker.getDataHolder(), exclusionListSperatorRegex);
    }

  } catch (SpellCheckException | IOException ex) {
    log.error("Error occured while loading default Configs for Spellcheck");
  }
}
 
Example 5
@Test
public void testJAASClientAppNameValidation() {
    final TestRunner runner = TestRunners.newTestRunner(PutSolrContentStream.class);
    runner.setProperty(PutSolrContentStream.SOLR_TYPE, PutSolrContentStream.SOLR_TYPE_STANDARD.getValue());
    runner.setProperty(PutSolrContentStream.SOLR_LOCATION, "http://localhost:8443/solr");
    runner.assertValid();

    // clear the jaas config system property if it was set
    final String jaasConfig = System.getProperty(Krb5HttpClientConfigurer.LOGIN_CONFIG_PROP);
    if (!StringUtils.isEmpty(jaasConfig)) {
        System.clearProperty(Krb5HttpClientConfigurer.LOGIN_CONFIG_PROP);
    }

    // should be invalid if we have a client name but not config file
    runner.setProperty(PutSolrContentStream.JAAS_CLIENT_APP_NAME, "Client");
    runner.assertNotValid();

    // should be invalid if we have a client name that is not in the config file
    final File jaasConfigFile = new File("src/test/resources/jaas-client.conf");
    System.setProperty(Krb5HttpClientConfigurer.LOGIN_CONFIG_PROP, jaasConfigFile.getAbsolutePath());
    runner.assertNotValid();

    // should be valid now that the name matches up with the config file
    runner.setProperty(PutSolrContentStream.JAAS_CLIENT_APP_NAME, "SolrJClient");
    runner.assertValid();
}
 
Example 6
Source Project: lucene-solr   Source File: NodePreferenceRulesComparator.java    License: Apache License 2.0 6 votes vote down vote up
private boolean hasCoreUrlPrefix(Object o, String prefix) {
  final String s;
  if (o instanceof String) {
    s = (String)o;
  }
  else if (o instanceof Replica) {
    s = ((Replica)o).getCoreUrl();
  } else {
    return false;
  }
  if (prefix.equals(ShardParams.REPLICA_LOCAL)) {
    return !StringUtils.isEmpty(localHostAddress) && s.startsWith(localHostAddress);
  } else {
    return s.startsWith(prefix);
  }
}
 
Example 7
Source Project: lucene-solr   Source File: CloudDescriptor.java    License: Apache License 2.0 6 votes vote down vote up
public void reload(CloudDescriptor reloadFrom) {
  if (reloadFrom == null) return;

  setShardId(StringUtils.isEmpty(reloadFrom.getShardId()) ? getShardId() : reloadFrom.getShardId());
  setCollectionName(StringUtils.isEmpty(reloadFrom.getCollectionName()) ? getCollectionName() : reloadFrom.getCollectionName());
  setRoles(StringUtils.isEmpty(reloadFrom.getRoles()) ? getRoles() : reloadFrom.getRoles());
  if (reloadFrom.getNumShards() != null) {
    setNumShards(reloadFrom.getNumShards());
  }
  setCoreNodeName(StringUtils.isEmpty(reloadFrom.getCoreNodeName()) ? getCoreNodeName() : reloadFrom.getCoreNodeName());
  setLeader(reloadFrom.isLeader);
  setHasRegistered(reloadFrom.hasRegistered);
  setLastPublished(reloadFrom.getLastPublished());

  for (Map.Entry<String, String> ent : reloadFrom.getParams().entrySet()) {
    collectionParams.put(ent.getKey(), ent.getValue());
  }
}
 
Example 8
Source Project: lucene-solr   Source File: SolrLogLayout.java    License: Apache License 2.0 6 votes vote down vote up
private void appendMDC(StringBuilder sb) {
  if (!StringUtils.isEmpty(MDC.get(NODE_NAME_PROP)))  {
    sb.append(" n:").append(MDC.get(NODE_NAME_PROP));
  }
  if (!StringUtils.isEmpty(MDC.get(COLLECTION_PROP)))  {
    sb.append(" c:").append(MDC.get(COLLECTION_PROP));
  }
  if (!StringUtils.isEmpty(MDC.get(SHARD_ID_PROP))) {
    sb.append(" s:").append(MDC.get(SHARD_ID_PROP));
  }
  if (!StringUtils.isEmpty(MDC.get(REPLICA_PROP))) {
    sb.append(" r:").append(MDC.get(REPLICA_PROP));
  }
  if (!StringUtils.isEmpty(MDC.get(CORE_NAME_PROP))) {
    sb.append(" x:").append(MDC.get(CORE_NAME_PROP));
  }
}
 
Example 9
Source Project: istio-apim   Source File: IstioExecutor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param parameterMap Map of parameters which passes to the executor
 */
public void init(Map parameterMap) {

    if (parameterMap != null) {

        istioSystemNamespace = (String) parameterMap.get("istioSystemNamespace");
        appNamespace = (String) parameterMap.get("appNamespace");
        kubernetesAPIServerUrl = (String) parameterMap.get("kubernetesAPIServerUrl");
        saTokenFileName = (String) parameterMap.get("saTokenFileName");
        saTokenFilePath = (String) parameterMap.get("saTokenFilePath");
        kubernetesServiceDomain = (String) parameterMap.get("kubernetesServiceDomain");
    }

    if (StringUtils.isEmpty(istioSystemNamespace)) {
        istioSystemNamespace = ISTIO_SYSTEM_NAMESPACE;
    }

    if (StringUtils.isEmpty(appNamespace)) {
        appNamespace = APP_NAMESPACE;
    }

    if (StringUtils.isEmpty(kubernetesAPIServerUrl)) {
        kubernetesAPIServerUrl = K8S_MASTER_URL;
    }

    if (StringUtils.isEmpty(saTokenFilePath)) {
        saTokenFilePath = SATOKEN_FILE_PATH;
    }

    if (StringUtils.isEmpty(kubernetesServiceDomain)) {
        kubernetesServiceDomain = K8S_SERVICE_DOMAIN;
    }
}
 
Example 10
Source Project: atlas   Source File: GraphBackedSearchIndexer.java    License: Apache License 2.0 5 votes vote down vote up
private void createEdgeLabelUsingLabelName(final AtlasGraphManagement management, final String label) {
    if (StringUtils.isEmpty(label)) {
        return;
    }

    org.apache.atlas.repository.graphdb.AtlasEdgeLabel edgeLabel = management.getEdgeLabel(label);

    if (edgeLabel == null) {
        management.makeEdgeLabel(label);

        LOG.info("Created edge label {} ", label);
    }
}
 
Example 11
Source Project: lucene-solr   Source File: Http2SolrClient.java    License: Apache License 2.0 5 votes vote down vote up
boolean belongToThisStream(@SuppressWarnings({"rawtypes"})SolrRequest solrRequest, String collection) {
  ModifiableSolrParams solrParams = new ModifiableSolrParams(solrRequest.getParams());
  if (!origParams.toNamedList().equals(solrParams.toNamedList()) || !StringUtils.equals(origCollection, collection)) {
    return false;
  }
  return true;
}
 
Example 12
Source Project: lucene-solr   Source File: ZkDynamicConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Parse a raw multi line config string with the full content of znode /zookeeper/config.
 * @param lines the multi line config string. If empty or null, this will return an empty list
 * @return an instance of ZkDynamicConfig
 */
public static ZkDynamicConfig parseLines(String lines) {
  ZkDynamicConfig zkDynamicConfig = new ZkDynamicConfig();
  if (!StringUtils.isEmpty(lines)) {
    lines.lines().forEach(l -> {
      if (l.startsWith("version=")) {
        zkDynamicConfig.version = l.split("=")[1];
      }
      if (l.startsWith("server.")) {
        zkDynamicConfig.servers.add(Server.parseLine(l));
      }
    });
  }
  return zkDynamicConfig;
}
 
Example 13
@Override
protected Collection<ZkCredentials> createCredentials() {
  List<ZkCredentials> result = new ArrayList<ZkCredentials>();
  String digestUsername = System.getProperty(zkDigestUsernameVMParamName);
  String digestPassword = System.getProperty(zkDigestPasswordVMParamName);
  if (!StringUtils.isEmpty(digestUsername) && !StringUtils.isEmpty(digestPassword)) {
    result.add(new ZkCredentials("digest",
        (digestUsername + ":" + digestPassword).getBytes(StandardCharsets.UTF_8)));
  }
  return result;
}
 
Example 14
/**
 * Note: only used for tests
 */
protected List<ACL> createACLsToAdd(boolean includeReadOnly,
                                    String digestAllUsername, String digestAllPassword,
                                    String digestReadonlyUsername, String digestReadonlyPassword) {

    try {
    List<ACL> result = new ArrayList<ACL>();

    // Not to have to provide too much credentials and ACL information to the process it is assumed that you want "ALL"-acls
    // added to the user you are using to connect to ZK (if you are using VMParamsSingleSetCredentialsDigestZkCredentialsProvider)
    if (!StringUtils.isEmpty(digestAllUsername) && !StringUtils.isEmpty(digestAllPassword)) {
      result.add(new ACL(ZooDefs.Perms.ALL, new Id("digest", DigestAuthenticationProvider.generateDigest(digestAllUsername + ":" + digestAllPassword))));
    }

    if (includeReadOnly) {
      // Besides that support for adding additional "READONLY"-acls for another user
      if (!StringUtils.isEmpty(digestReadonlyUsername) && !StringUtils.isEmpty(digestReadonlyPassword)) {
        result.add(new ACL(ZooDefs.Perms.READ, new Id("digest", DigestAuthenticationProvider.generateDigest(digestReadonlyUsername + ":" + digestReadonlyPassword))));
      }
    }
    
    if (result.isEmpty()) {
      result = ZooDefs.Ids.OPEN_ACL_UNSAFE;
    }
    
    return result;
  } catch (NoSuchAlgorithmException e) {
    throw new RuntimeException(e);
  }
}
 
Example 15
Source Project: lucene-solr   Source File: SolrZkClient.java    License: Apache License 2.0 5 votes vote down vote up
protected ZkCredentialsProvider createZkCredentialsToAddAutomatically() {
  String zkCredentialsProviderClassName = System.getProperty(ZK_CRED_PROVIDER_CLASS_NAME_VM_PARAM_NAME);
  if (!StringUtils.isEmpty(zkCredentialsProviderClassName)) {
    try {
      log.info("Using ZkCredentialsProvider: {}", zkCredentialsProviderClassName);
      return (ZkCredentialsProvider)Class.forName(zkCredentialsProviderClassName).getConstructor().newInstance();
    } catch (Throwable t) {
      // just ignore - go default
      log.warn("VM param zkCredentialsProvider does not point to a class implementing ZkCredentialsProvider and with a non-arg constructor", t);
    }
  }
  log.debug("Using default ZkCredentialsProvider");
  return new DefaultZkCredentialsProvider();
}
 
Example 16
Source Project: lucene-solr   Source File: SolrZkClient.java    License: Apache License 2.0 5 votes vote down vote up
protected ZkACLProvider createZkACLProvider() {
  String zkACLProviderClassName = System.getProperty(ZK_ACL_PROVIDER_CLASS_NAME_VM_PARAM_NAME);
  if (!StringUtils.isEmpty(zkACLProviderClassName)) {
    try {
      log.info("Using ZkACLProvider: {}", zkACLProviderClassName);
      return (ZkACLProvider)Class.forName(zkACLProviderClassName).getConstructor().newInstance();
    } catch (Throwable t) {
      // just ignore - go default
      log.warn("VM param zkACLProvider does not point to a class implementing ZkACLProvider and with a non-arg constructor", t);
    }
  }
  log.debug("Using default ZkACLProvider");
  return new DefaultZkACLProvider();
}
 
Example 17
Source Project: lucene-solr   Source File: PublicKeyHandler.java    License: Apache License 2.0 5 votes vote down vote up
private CryptoKeys.RSAKeyPair createKeyPair(CloudConfig config) throws IOException, InvalidKeySpecException {
  if (config == null) {
    return new CryptoKeys.RSAKeyPair();
  }

  String publicKey = config.getPkiHandlerPublicKeyPath();
  String privateKey = config.getPkiHandlerPrivateKeyPath();

  // If both properties unset, then we fall back to generating a new key pair
  if (StringUtils.isEmpty(publicKey) && StringUtils.isEmpty(privateKey)) {
    return new CryptoKeys.RSAKeyPair();
  }

  return new CryptoKeys.RSAKeyPair(new URL(privateKey), new URL(publicKey));
}
 
Example 18
Source Project: lucene-solr   Source File: MDCLoggingContext.java    License: Apache License 2.0 5 votes vote down vote up
public static void setTracerId(String traceId) {
  if (!StringUtils.isEmpty(traceId)) {
    MDC.put(TRACE_ID, "t:" + traceId);
  } else {
    MDC.remove(TRACE_ID);
  }
}
 
Example 19
Source Project: lucene-solr   Source File: CatStream.java    License: Apache License 2.0 5 votes vote down vote up
public CatStream(String commaDelimitedFilepaths, int maxLines) {
  if (commaDelimitedFilepaths == null) {
    throw new IllegalArgumentException("No filepaths provided to stream");
  }
  final String filepathsWithoutSurroundingQuotes = stripSurroundingQuotesIfTheyExist(commaDelimitedFilepaths);
  if (StringUtils.isEmpty(filepathsWithoutSurroundingQuotes)) {
    throw new IllegalArgumentException("No filepaths provided to stream");
  }

  this.commaDelimitedFilepaths = filepathsWithoutSurroundingQuotes;
  this.maxLines = maxLines;
}
 
Example 20
Source Project: lucene-solr   Source File: RealTimeGetComponent.java    License: Apache License 2.0 5 votes vote down vote up
private List<Long> resolveVersionRanges(String versionsStr, UpdateLog ulog) {
  if (StringUtils.isEmpty(versionsStr)) {
    return Collections.emptyList();
  }
  
  List<String> ranges = StrUtils.splitSmart(versionsStr, ",", true);
  
  // TODO merge ranges.
  
  // get all the versions from updatelog and sort them
  List<Long> versionAvailable = null;
  try (UpdateLog.RecentUpdates recentUpdates = ulog.getRecentUpdates()) {
    versionAvailable = recentUpdates.getVersions(ulog.getNumRecordsToKeep());
  }
  // sort versions
  Collections.sort(versionAvailable, PeerSync.absComparator);
  
  // This can be done with single pass over both ranges and versionsAvailable, that would require 
  // merging ranges. We currently use Set to ensure there are no duplicates.
  Set<Long> versionsToRet = new HashSet<>(ulog.getNumRecordsToKeep());
  for (String range : ranges) {
    String[] rangeBounds = range.split("\\.{3}");
    int indexStart = Collections.binarySearch(versionAvailable, Long.valueOf(rangeBounds[1]), PeerSync.absComparator);
    int indexEnd = Collections.binarySearch(versionAvailable, Long.valueOf(rangeBounds[0]), PeerSync.absComparator); 
    if(indexStart >=0 && indexEnd >= 0) {
      versionsToRet.addAll(versionAvailable.subList(indexStart, indexEnd + 1)); // indexEnd is exclusive
    }
  }
  // TODO do we need to sort versions using PeerSync.absComparator?
  return new ArrayList<>(versionsToRet);
}
 
Example 21
Source Project: lucene-solr   Source File: HadoopSSLCredentialProvider.java    License: Apache License 2.0 5 votes vote down vote up
public HadoopSSLCredentialProvider(Configuration hadoopConfigurationProvider) {
  if (StringUtils.isEmpty(System.getProperty(CREDENTIAL_PROVIDER_PATH))) {
    throw new RuntimeException("Cannot initialize Hadoop configuration provider without credential provider path. Use " + CREDENTIAL_PROVIDER_PATH + " system property to configure.");
  }
  this.hadoopConfigurationProvider = hadoopConfigurationProvider;
  hadoopConfigurationProvider.set(CREDENTIAL_PROVIDER_PATH, System.getProperty(CREDENTIAL_PROVIDER_PATH));
}
 
Example 22
public SolrZkClient getSolrZkClient(String zkServerAddress, int zkClientTimeout) {
  return new SolrZkClient(zkServerAddress, zkClientTimeout) {
    
    @Override
    protected ZkCredentialsProvider createZkCredentialsToAddAutomatically() {
      return new DefaultZkCredentialsProvider() {
        @Override
        protected Collection<ZkCredentials> createCredentials() {
          List<ZkCredentials> result = new ArrayList<>();
          if (!StringUtils.isEmpty(digestUsername) && !StringUtils.isEmpty(digestPassword)) {
            result.add(new ZkCredentials("digest",
                (digestUsername + ":" + digestPassword).getBytes(StandardCharsets.UTF_8)));
          }
          return result;
        }

      };
    }

    @Override
    public ZkACLProvider createZkACLProvider() {
      return new VMParamsAllAndReadonlyDigestZkACLProvider() {
        @Override
        protected List<ACL> createNonSecurityACLsToAdd() {
          return createACLsToAdd(true, digestUsername, digestPassword, digestReadonlyUsername, digestReadonlyPassword);
        }

        /**
         * @return Set of ACLs to return security-related znodes
         */
        @Override
        protected List<ACL> createSecurityACLsToAdd() {
          return createACLsToAdd(false, digestUsername, digestPassword, digestReadonlyUsername, digestReadonlyPassword);
        }
      };
    }
    
  };
}
 
Example 23
Source Project: incubator-atlas   Source File: ZipFileResourceTestUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static String getModelJson(String fileName) throws IOException {
    final String userDir = System.getProperty("user.dir");
    String filePath = userDir + "/../addons/models/" + fileName;
    File f = new File(filePath);
    String s = FileUtils.readFileToString(f);
    assertFalse(StringUtils.isEmpty(s), "Model file read correctly!");

    return s;
}
 
Example 24
Source Project: skywalking   Source File: SolrClientInterceptorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGet() throws Throwable {
    ModifiableSolrParams reqParams = new ModifiableSolrParams();
    if (StringUtils.isEmpty(reqParams.get("qt"))) {
        reqParams.set("qt", new String[] {"/get"});
    }
    reqParams.set("ids", new String[] {
        "99",
        "98"
    });
    QueryRequest request = new QueryRequest(reqParams);

    arguments = new Object[] {
        request,
        null,
        collection
    };
    interceptor.beforeMethod(enhancedInstance, method, arguments, argumentType, null);
    interceptor.afterMethod(enhancedInstance, method, arguments, argumentType, getGetResponse());

    List<TraceSegment> segments = segmentStorage.getTraceSegments();
    List<AbstractTracingSpan> spans = SegmentHelper.getSpans(segments.get(0));
    Assert.assertEquals(segments.size(), 1);
    Assert.assertEquals(spans.size(), 1);

    AbstractTracingSpan span = spans.get(0);
    querySpanAssert(span, "/get", 1, "solrJ/collection/get");
}
 
Example 25
Source Project: ranger   Source File: ChangeUserNameUtil.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    logger.info("main()");
    try {
        ChangeUserNameUtil loader = (ChangeUserNameUtil) CLIUtil.getBean(ChangeUserNameUtil.class);
        loader.init();
        if (args.length == 3) {
            userLoginId = args[0];
            currentPassword = args[1];
            newUserName = args[2];
            if(StringUtils.isEmpty(userLoginId)){
                System.out.println("Invalid login ID. Exiting!!!");
                logger.info("Invalid login ID. Exiting!!!");
                System.exit(1);
            }
            if(StringUtils.isEmpty(currentPassword)){
                System.out.println("Invalid current password. Exiting!!!");
                logger.info("Invalid current password. Exiting!!!");
                System.exit(1);
            }
            if(StringUtils.isEmpty(newUserName)){
                System.out.println("Invalid new user name. Exiting!!!");
                logger.info("Invalid new user name. Exiting!!!");
                System.exit(1);
            }
            while (loader.isMoreToProcess()) {
                loader.load();
            }
            logger.info("Load complete. Exiting!!!");
            System.exit(0);
        }else{
            System.out.println("ChangeUserNameUtil: Incorrect Arguments \n Usage: \n <loginId> <current-password> <new-username>");
            logger.error("ChangeUserNameUtil: Incorrect Arguments \n Usage: \n <loginId> <current-password> <new-username>");
            System.exit(1);
        }
    }
    catch (Exception e) {
        logger.error("Error loading", e);
        System.exit(1);
    }
}
 
Example 26
Source Project: ranger   Source File: RangerPolicyResourceSignature.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
	// invalid/empty policy gets a deterministic signature as if it had an
	// empty resource string
	if (!isPolicyValidForResourceSignatureComputation()) {
		return "";
	}
	int type = RangerPolicy.POLICY_TYPE_ACCESS;
	if (_policy.getPolicyType() != null) {
		type = _policy.getPolicyType();
	}
	Map<String, ResourceSerializer> resources = new TreeMap<>();
	for (Map.Entry<String, RangerPolicyResource> entry : _policy.getResources().entrySet()) {
		String resourceName = entry.getKey();
		ResourceSerializer resourceView = new ResourceSerializer(entry.getValue());
		resources.put(resourceName, resourceView);
	}
	String resource = resources.toString();
	if (CollectionUtils.isNotEmpty(_policy.getValiditySchedules())) {
		resource += _policy.getValiditySchedules().toString();
	}
	if (_policy.getPolicyPriority() != null && _policy.getPolicyPriority() != RangerPolicy.POLICY_PRIORITY_NORMAL) {
		resource += _policy.getPolicyPriority();
	}
	if (!StringUtils.isEmpty(_policy.getZoneName())) {
	    resource += _policy.getZoneName();
          }

	if (_policy.getConditions() != null) {
		CustomConditionSerialiser customConditionSerialiser = new CustomConditionSerialiser(_policy.getConditions());
		resource += customConditionSerialiser.toString();
	}

	String result = String.format("{version=%d,type=%d,resource=%s}", _SignatureVersion, type, resource);
	return result;
}
 
Example 27
Source Project: lucene-solr   Source File: Http2SolrClient.java    License: Apache License 2.0 4 votes vote down vote up
private ContentType getContentType(Response response) {
  String contentType = response.getHeaders().get(HttpHeader.CONTENT_TYPE);
  return StringUtils.isEmpty(contentType)? null : ContentType.parse(contentType);
}
 
Example 28
Source Project: lucene-solr   Source File: JWTAuthPlugin.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void init(Map<String, Object> pluginConfig) {
  this.pluginConfig = pluginConfig;
  this.issuerConfigs = null;
  List<String> unknownKeys = pluginConfig.keySet().stream().filter(k -> !PROPS.contains(k)).collect(Collectors.toList());
  unknownKeys.remove("class");
  unknownKeys.remove("");
  if (!unknownKeys.isEmpty()) {
    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Invalid JwtAuth configuration parameter " + unknownKeys); 
  }

  blockUnknown = Boolean.parseBoolean(String.valueOf(pluginConfig.getOrDefault(PARAM_BLOCK_UNKNOWN, false)));
  requireIssuer = Boolean.parseBoolean(String.valueOf(pluginConfig.getOrDefault(PARAM_REQUIRE_ISSUER, "true")));
  requireExpirationTime = Boolean.parseBoolean(String.valueOf(pluginConfig.getOrDefault(PARAM_REQUIRE_EXPIRATIONTIME, "true")));
  if (pluginConfig.get(PARAM_REQUIRE_SUBJECT) != null) {
    log.warn("Parameter {} is no longer used and may generate error in a later version. A subject claim is now always required",
        PARAM_REQUIRE_SUBJECT);
  }
  principalClaim = (String) pluginConfig.getOrDefault(PARAM_PRINCIPAL_CLAIM, "sub");

  rolesClaim = (String) pluginConfig.get(PARAM_ROLES_CLAIM);
  algWhitelist = (List<String>) pluginConfig.get(PARAM_ALG_WHITELIST);
  realm = (String) pluginConfig.getOrDefault(PARAM_REALM, DEFAULT_AUTH_REALM);

  Map<String, String> claimsMatch = (Map<String, String>) pluginConfig.get(PARAM_CLAIMS_MATCH);
  claimsMatchCompiled = new HashMap<>();
  if (claimsMatch != null) {
    for (Map.Entry<String, String> entry : claimsMatch.entrySet()) {
      claimsMatchCompiled.put(entry.getKey(), Pattern.compile(entry.getValue()));
    }
  }

  String requiredScopesStr = (String) pluginConfig.get(PARAM_SCOPE);
  if (!StringUtils.isEmpty(requiredScopesStr)) {
    requiredScopes = Arrays.asList(requiredScopesStr.split("\\s+"));
  }

  long jwkCacheDuration = Long.parseLong((String) pluginConfig.getOrDefault(PARAM_JWK_CACHE_DURATION, "3600"));
  JWTIssuerConfig.setHttpsJwksFactory(new JWTIssuerConfig.HttpsJwksFactory(jwkCacheDuration, DEFAULT_REFRESH_REPRIEVE_THRESHOLD));

  issuerConfigs = new ArrayList<>();

  // Try to parse an issuer from top level config, and add first (primary issuer)
  Optional<JWTIssuerConfig> topLevelIssuer = parseIssuerFromTopLevelConfig(pluginConfig);
  topLevelIssuer.ifPresent(ic -> {
    issuerConfigs.add(ic);
    log.warn("JWTAuthPlugin issuer is configured using top-level configuration keys. Please consider using the 'issuers' array instead.");
  });

  // Add issuers from 'issuers' key
  issuerConfigs.addAll(parseIssuers(pluginConfig));
  verificationKeyResolver = new JWTVerificationkeyResolver(issuerConfigs, requireIssuer);

  if (issuerConfigs.size() > 0 && getPrimaryIssuer().getAuthorizationEndpoint() != null) {
    adminUiScope = (String) pluginConfig.get(PARAM_ADMINUI_SCOPE);
    if (adminUiScope == null && requiredScopes.size() > 0) {
      adminUiScope = requiredScopes.get(0);
      log.warn("No adminUiScope given, using first scope in 'scope' list as required scope for accessing Admin UI");
    }

    if (adminUiScope == null) {
      adminUiScope = "solr";
      log.info("No adminUiScope provided, fallback to 'solr' as required scope for Admin UI login may not work");
    }

    Object redirectUrisObj = pluginConfig.get(PARAM_REDIRECT_URIS);
    redirectUris = Collections.emptyList();
    if (redirectUrisObj != null) {
      if (redirectUrisObj instanceof String) {
        redirectUris = Collections.singletonList((String) redirectUrisObj);
      } else if (redirectUrisObj instanceof List) {
        redirectUris = (List<String>) redirectUrisObj;
      }
    }
  }

  initConsumer();

  lastInitTime = Instant.now();
}
 
Example 29
Source Project: lucene-solr   Source File: MoreLikeThisHandler.java    License: Apache License 2.0 4 votes vote down vote up
public MoreLikeThisHelper( SolrParams params, SolrIndexSearcher searcher )
{
  this.searcher = searcher;
  this.reader = searcher.getIndexReader();
  this.uniqueKeyField = searcher.getSchema().getUniqueKeyField();
  this.needDocSet = params.getBool(FacetParams.FACET,false);
  
  SolrParams required = params.required();
  String[] fl = required.getParams(MoreLikeThisParams.SIMILARITY_FIELDS);
  List<String> list = new ArrayList<>();
  for (String f : fl) {
    if (!StringUtils.isEmpty(f))  {
      String[] strings = splitList.split(f);
      for (String string : strings) {
        if (!StringUtils.isEmpty(string)) {
          list.add(string);
        }
      }
    }
  }
  String[] fields = list.toArray(new String[list.size()]);
  if( fields.length < 1 ) {
    throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, 
        "MoreLikeThis requires at least one similarity field: "+MoreLikeThisParams.SIMILARITY_FIELDS );
  }
  
  this.mlt = new MoreLikeThis( reader ); // TODO -- after LUCENE-896, we can use , searcher.getSimilarity() );
  mlt.setFieldNames(fields);
  mlt.setAnalyzer( searcher.getSchema().getIndexAnalyzer() );
  
  // configurable params
  
  mlt.setMinTermFreq(       params.getInt(MoreLikeThisParams.MIN_TERM_FREQ,         MoreLikeThis.DEFAULT_MIN_TERM_FREQ));
  mlt.setMinDocFreq(        params.getInt(MoreLikeThisParams.MIN_DOC_FREQ,          MoreLikeThis.DEFAULT_MIN_DOC_FREQ));
  mlt.setMaxDocFreq(        params.getInt(MoreLikeThisParams.MAX_DOC_FREQ,          MoreLikeThis.DEFAULT_MAX_DOC_FREQ));
  mlt.setMinWordLen(        params.getInt(MoreLikeThisParams.MIN_WORD_LEN,          MoreLikeThis.DEFAULT_MIN_WORD_LENGTH));
  mlt.setMaxWordLen(        params.getInt(MoreLikeThisParams.MAX_WORD_LEN,          MoreLikeThis.DEFAULT_MAX_WORD_LENGTH));
  mlt.setMaxQueryTerms(     params.getInt(MoreLikeThisParams.MAX_QUERY_TERMS,       MoreLikeThis.DEFAULT_MAX_QUERY_TERMS));
  mlt.setMaxNumTokensParsed(params.getInt(MoreLikeThisParams.MAX_NUM_TOKENS_PARSED, MoreLikeThis.DEFAULT_MAX_NUM_TOKENS_PARSED));
  mlt.setBoost(            params.getBool(MoreLikeThisParams.BOOST, false ) );
  
  // There is no default for maxDocFreqPct. Also, it's a bit oddly expressed as an integer value 
  // (percentage of the collection's documents count). We keep Lucene's convention here. 
  if (params.getInt(MoreLikeThisParams.MAX_DOC_FREQ_PCT) != null) {
    mlt.setMaxDocFreqPct(params.getInt(MoreLikeThisParams.MAX_DOC_FREQ_PCT));
  }

  boostFields = SolrPluginUtils.parseFieldBoosts(params.getParams(MoreLikeThisParams.QF));
}
 
Example 30
Source Project: lucene-solr   Source File: SSLConfigurations.java    License: Apache License 2.0 4 votes vote down vote up
private boolean isEmpty(String str) {
  return StringUtils.isEmpty(str);
}