Ejemplo n.º 1
0
  /**
   * Searches for documents and concepts by using partial matches on the label(s) fields.
   *
   * @param parameters The parameters to be used in the service call, account_id, corpus and query
   *     are required.
   *     <ul>
   *       <li>String account_id - The account identifier.<br>
   *       <li>String corpus - The corpus name.<br>
   *       <li>RequestedFields concept_fields - Additional fields to be included in the concept
   *           objects.<br>
   *       <li>RequestedFields document_fields - Additional fields to be included in the document
   *           objects.<br>
   *       <li>Boolean concepts - Whether to return concepts that have a label match.<br>
   *       <li>String query - The query string.<br>
   *       <li>Boolean prefix - Whether the query string should be treated as a prefix.<br>
   *       <li>Integer limit - The maximum number of concepts to be returned.<br>
   *     </ul>
   *
   * @return {@link Matches}
   */
  public Matches searchCorpusByLabel(Map<String, Object> parameters) {
    Validate.notNull(parameters.get(ACCOUNT_ID), "account_id can't be null");
    Validate.notNull(parameters.get(CORPUS), "corpus can't be null");
    Validate.notNull(parameters.get(QUERY), "query can't be null");

    String corpusId =
        createCorpusIdPath((String) parameters.get(ACCOUNT_ID), (String) parameters.get(CORPUS));

    Map<String, Object> queryParameters = new HashMap<String, Object>();
    String[] queryParams = new String[] {QUERY, PREFIX, LIMIT, CONCEPTS};
    for (String param : queryParams) {
      if (parameters.containsKey(param)) queryParameters.put(param, parameters.get(param));
    }

    if (parameters.get(CONCEPT_FIELDS) != null) {
      RequestedFields fields = (RequestedFields) parameters.get(CONCEPT_FIELDS);
      if (fields != null && fields.getFields() != null && !fields.getFields().isEmpty())
        queryParameters.put(CONCEPT_FIELDS, fields.toString());
    }

    if (parameters.get(DOCUMENT_FIELDS) != null) {
      RequestedFields fields = (RequestedFields) parameters.get(DOCUMENT_FIELDS);
      if (fields != null && fields.getFields() != null && !fields.getFields().isEmpty())
        queryParameters.put(DOCUMENT_FIELDS, fields.toString());
    }
    return executeRequest(corpusId + LABEL_SEARCH_PATH, queryParameters, Matches.class);
  }
Ejemplo n.º 2
0
  private static String formatDate(final Object target, final String pattern, final Locale locale) {

    Validate.notNull(target, "Cannot apply format on null");
    Validate.notNull(locale, "Locale cannot be null");

    final DateFormatKey key = new DateFormatKey(pattern, locale);

    DateFormat dateFormat = dateFormats.get(key);
    if (dateFormat == null) {
      if (pattern == null || pattern.trim().equals("")) {
        dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
      } else {
        dateFormat = new SimpleDateFormat(pattern, locale);
      }
      dateFormats.put(key, dateFormat);
    }

    if (target instanceof Calendar) {
      return dateFormat.format(((Calendar) target).getTime());
    } else if (target instanceof java.util.Date) {
      return dateFormat.format((java.util.Date) target);
    } else {
      throw new IllegalArgumentException(
          "Cannot format object of class \"" + target.getClass().getName() + "\" as a date");
    }
  }
Ejemplo n.º 3
0
  /**
   * Returns a list of scores that denotes how related a source concept is to a list of individual
   * concepts.
   *
   * @param parameters The parameters to be used in the service call, account_id, graph and concept
   *     or concepts are required.
   *     <ul>
   *       <li>String account_id - The account identifier.<br>
   *       <li>String graph - The graph name.<br>
   *       <li>String concept - The concept name.<br>
   *       <li>List&lt;String&gt; concepts - Array of concept IDs, each identifying a concept.<br>
   *     </ul>
   *
   * @return {@link Scores}
   */
  public Scores getGraphsRelationScores(Map<String, Object> parameters) {
    Validate.notNull(parameters.get(ACCOUNT_ID), "account_id can't be null");
    Validate.notNull(parameters.get(GRAPH), "graph can't be null");
    Validate.notNull(parameters.get(CONCEPT), "concept can't be null");
    Validate.notNull(parameters.get(CONCEPTS), "concepts can't be null");

    String conceptId =
        createConceptIdPath(
            (String) parameters.get(ACCOUNT_ID),
            (String) parameters.get(GRAPH),
            (String) parameters.get(CONCEPT));

    Map<String, Object> queryParameters = new HashMap<String, Object>();
    JsonObject contentJson = new JsonObject();
    JsonArray conceptsJson = new JsonArray();
    @SuppressWarnings("unchecked")
    List<String> concepts = (List<String>) parameters.get(CONCEPTS);
    for (String value : concepts) {
      conceptsJson.add(new JsonPrimitive(value));
    }
    contentJson.add(CONCEPTS, conceptsJson);
    queryParameters.put(CONCEPTS, conceptsJson.toString());

    return executeRequest(conceptId + RELATION_SCORES_PATH, queryParameters, Scores.class);
  }
  /* (non-Javadoc)
   * @see org.jboss.arquillian.spi.ServiceLoader#all(java.lang.ClassLoader, java.lang.Class)
   */
  @Override
  public <T> Collection<T> all(ClassLoader classLoader, Class<T> serviceClass) {
    Validate.notNull(classLoader, "ClassLoader must be provided");
    Validate.notNull(serviceClass, "ServiceClass must be provided");

    return createInstances(serviceClass, load(serviceClass, classLoader));
  }
  /**
   * {@inheritDoc}
   *
   * @see org.jboss.shrinkwrap.api.Archive#add(org.jboss.shrinkwrap.api.asset.Asset,
   *     org.jboss.shrinkwrap.api.ArchivePath)
   */
  @Override
  public T add(Asset asset, ArchivePath path) {
    Validate.notNull(asset, "No asset was specified");
    Validate.notNull(path, "No path was specified");

    return addAsset(path, asset);
  }
 /**
  * {@inheritDoc}
  *
  * @see org.jboss.shrinkwrap.api.Archive#add(org.jboss.shrinkwrap.api.Archive, java.lang.String,
  *     java.lang.Class)
  */
 @Override
 public T add(
     final Archive<?> archive, final String path, final Class<? extends StreamExporter> exporter) {
   Validate.notNull(archive, "Archive must be specified");
   Validate.notNullOrEmpty(path, "Archive Path must be specified");
   Validate.notNull(exporter, "exporter must be specified");
   return this.add(archive, ArchivePaths.create(path), exporter);
 }
Ejemplo n.º 7
0
  /**
   * Retrieves processing state of document.
   *
   * @param accountId String the account identifier,
   * @param corpusName String the corpus name,
   * @param documentName String the document name.
   * @return {@link DocumentProcessingStatus}
   */
  public DocumentProcessingStatus getDocumentProcessingState(
      final String accountId, final String corpusName, final String documentName) {
    Validate.notNull(accountId, "accountId can't be null");
    Validate.notNull(corpusName, "corpusName can't be null");
    Validate.notNull(documentName, "documentName can't be null");

    String documentId = createDocumentIdPath(accountId, corpusName, documentName);
    return executeRequest(documentId + PROCESSING_STATE_PATH, null, DocumentProcessingStatus.class);
  }
Ejemplo n.º 8
0
  /**
   * Retrieves conceptual view of document (including annotations).
   *
   * @param accountId String the account identifier,
   * @param corpusName String the corpus name,
   * @param documentName String the document name.
   * @return {@link DocumentAnnotations}
   */
  public DocumentAnnotations getDocumentAnnotations(
      final String accountId, final String corpusName, final String documentName) {
    Validate.notNull(accountId, "accountId can't be null");
    Validate.notNull(corpusName, "corpusName can't be null");
    Validate.notNull(documentName, "documentName can't be null");

    String documentId = createDocumentIdPath(accountId, corpusName, documentName);
    return executeRequest(documentId + ANNOTATIONS_PATH, null, DocumentAnnotations.class);
  }
Ejemplo n.º 9
0
  /**
   * Retrieves a document from a corpus.
   *
   * @param accountId String the account identifier,
   * @param corpusName String the corpus name.
   * @param documentName String the document name.
   * @return {@link Document}
   */
  public Document getDocument(
      final String accountId, final String corpusName, final String documentName) {
    Validate.notNull(accountId, "accountId can't be null");
    Validate.notNull(corpusName, "corpusName can't be null");
    Validate.notNull(documentName, "documentName can't be null");

    return executeRequest(
        createDocumentIdPath(accountId, corpusName, documentName), null, Document.class);
  }
Ejemplo n.º 10
0
 /**
  * Gets processing state of a Corpus.
  *
  * @param accountId String the account identifier,
  * @param corpusName String the corpus name.
  * @return {@link CorpusProcessingState} The processing state of a given corpus.
  */
 public CorpusProcessingState getCorpusProcessingState(
     final String accountId, final String corpusName) {
   Validate.notNull(accountId, "accountId can't be null");
   Validate.notNull(corpusName, "corpusName can't be null");
   return executeRequest(
       createCorpusIdPath(accountId, corpusName) + PROCESSING_STATE_PATH,
       null,
       CorpusProcessingState.class);
 }
Ejemplo n.º 11
0
 /**
  * Deletes a document in a given corpus.
  *
  * @param accountId String the account identifier,
  * @param corpusName String the corpus name.
  * @param documentName String the document name.
  */
 public void deleteDocument(
     final String accountId, final String corpusName, final String documentName) {
   Validate.notNull(accountId, "accountId can't be null");
   Validate.notNull(corpusName, "corpusName can't be null");
   Validate.notNull(documentName, "documentName can't be null");
   HttpRequestBase request =
       Request.Delete(createDocumentIdPath(accountId, corpusName, documentName)).build();
   executeWithoutResponse(request);
 }
Ejemplo n.º 12
0
  /**
   * Gets the value of the field with the specified name in the specified object. Supports
   * inheritance.
   *
   * @param object The object to get the value from.
   * @param fieldClass The class of the field.
   * @param fieldName The name of the field.
   * @param <T> The class of the field.
   * @return The value in the field of the object.
   */
  public static <T> T getFieldValue(
      final Object object, final Class<T> fieldClass, final String fieldName) {
    Validate.notNull(object, "The object to get the value from can not be null");
    Validate.notNull(fieldClass, "The class of the field can not be null");
    Validate.notEmpty(fieldName, "The name of the field can not be empty");

    final Field field = getField(object, fieldName);
    return getValue(object, fieldClass, field);
  }
  /* (non-Javadoc)
   * @see org.jboss.arquillian.spi.ServiceLoader#onlyOne(java.lang.ClassLoader, java.lang.Class)
   */
  @Override
  public <T> T onlyOne(ClassLoader classLoader, Class<T> serviceClass) {
    Validate.notNull(classLoader, "ClassLoader must be provided");
    Validate.notNull(serviceClass, "ServiceClass must be provided");

    Set<Class<? extends T>> serviceImpls = load(serviceClass, classLoader);
    verifyOnlyOneOrSameImplementation(serviceClass, serviceImpls);

    return createInstance(serviceImpls.iterator().next());
  }
Ejemplo n.º 14
0
  /**
   * Updates existing corpus meta-data (access and permissions).
   *
   * @param accountId String the Account identifier.
   * @param corpus {@link Corpus} the corpus to update.
   */
  public void updateCorpus(final String accountId, final Corpus corpus) {
    Validate.notNull(accountId, "account_id can't be null");
    Validate.notNull(corpus, "corpus can't be null");
    Validate.notNull(corpus.getId(), "corpus.id can't be null");

    HttpRequestBase request =
        Request.Post(createCorpusIdPath(accountId, corpus.getId()))
            .withContent(GsonSingleton.getGson().toJson(corpus), MediaType.APPLICATION_JSON)
            .build();
    executeWithoutResponse(request);
  }
Ejemplo n.º 15
0
 /**
  * Returns information for a specific concept node in a graph.
  *
  * @param parameters The parameters to be used in the service call, account_id, graph and concept
  *     are required.
  *     <ul>
  *       <li>String account_id - The account identifier.<br>
  *       <li>String graph - The graph name.<br>
  *       <li>String concept - The concept name.<br>
  *     </ul>
  *
  * @return {@link ConceptMetadata}
  */
 public ConceptMetadata getConcept(Map<String, Object> parameters) {
   Validate.notNull(parameters.get(ACCOUNT_ID), "account_id can't be null");
   Validate.notNull(parameters.get(GRAPH), "graph can't be null");
   Validate.notNull(parameters.get(CONCEPT), "concept can't be null");
   String conceptId =
       createConceptIdPath(
           (String) parameters.get(ACCOUNT_ID),
           (String) parameters.get(GRAPH),
           (String) parameters.get(CONCEPT));
   return executeRequest(conceptId, null, ConceptMetadata.class);
 }
Ejemplo n.º 16
0
 public Connection data(String... keyvals) {
   Validate.notNull(keyvals, "Data key value pairs must not be null");
   Validate.isTrue(keyvals.length % 2 == 0, "Must supply an even number of key value pairs");
   for (int i = 0; i < keyvals.length; i += 2) {
     String key = keyvals[i];
     String value = keyvals[i + 1];
     Validate.notEmpty(key, "Data key must not be empty");
     Validate.notNull(value, "Data value must not be null");
     req.data(KeyVal.create(key, value));
   }
   return this;
 }
Ejemplo n.º 17
0
  /**
   * Puts the list of permissions into a Bundle.
   *
   * @param bundle A Bundle in which the list of permissions should be stored.
   * @param value The List&lt;String&gt; representing the list of permissions, or null.
   * @throws NullPointerException if the passed in Bundle or permissions list are null
   */
  public static void putPermissions(Bundle bundle, List<String> value) {
    Validate.notNull(bundle, "bundle");
    Validate.notNull(value, "value");

    ArrayList<String> arrayList;
    if (value instanceof ArrayList<?>) {
      arrayList = (ArrayList<String>) value;
    } else {
      arrayList = new ArrayList<String>(value);
    }
    bundle.putStringArrayList(PERMISSIONS_KEY, arrayList);
  }
Ejemplo n.º 18
0
  /**
   * Updates a document in a given corpus.
   *
   * @param accountId String the account identifier,
   * @param corpusName String the corpus name.
   * @param document {@link Document} The document to update.
   */
  public void updateDocument(
      final String accountId, final String corpusName, final Document document) {
    Validate.notNull(accountId, "accountId can't be null");
    Validate.notNull(corpusName, "corpusName can't be null");
    Validate.notNull(document, "document can't be null");
    Validate.notNull(document.getId(), "document.id can't be null");

    HttpRequestBase request =
        Request.Post(createDocumentIdPath(accountId, corpusName, document.getId()))
            .withContent(GsonSingleton.getGson().toJson(document), MediaType.APPLICATION_JSON)
            .build();
    executeWithoutResponse(request);
  }
Ejemplo n.º 19
0
  /* (non-Javadoc)
   * @see org.jboss.arquillian.spi.ServiceLoader#onlyOne(java.lang.Class, java.lang.Class)
   */
  public <T> T onlyOne(Class<T> serviceClass, Class<? extends T> defaultServiceClass) {
    Validate.notNull(serviceClass, "ServiceClass must be provided");
    Validate.notNull(defaultServiceClass, "DefaultServiceClass must be provided");

    Class<? extends T> serviceImplToCreate = defaultServiceClass;

    Set<Class<? extends T>> serviceImpls =
        load(serviceClass, SecurityActions.getThreadContextClassLoader());
    if (serviceImpls.size() > 0) {
      verifySameImplementation(serviceClass, serviceImpls);
      serviceImplToCreate = serviceImpls.iterator().next();
    }
    return createInstance(serviceImplToCreate);
  }
Ejemplo n.º 20
0
 public T header(String name, String value) {
   Validate.notEmpty(name, "Header name must not be empty");
   Validate.notNull(value, "Header value must not be null");
   removeHeader(name); // ensures we don't get an "accept-encoding" and a "Accept-Encoding"
   headers.put(name, value);
   return (T) this;
 }
Ejemplo n.º 21
0
 public Connection data(Map<String, String> data) {
   Validate.notNull(data, "Data map must not be null");
   for (Map.Entry<String, String> entry : data.entrySet()) {
     req.data(KeyVal.create(entry.getKey(), entry.getValue()));
   }
   return this;
 }
Ejemplo n.º 22
0
  /**
   * Set the value in the field with the specified name in the specified object. Supports
   * inheritance.
   *
   * @param object The object to set the value in.
   * @param fieldName The name of the field.
   * @param value The value to set in the field.
   */
  public static void setFieldValue(
      final Object object, final String fieldName, final Object value) {
    Validate.notNull(object, "The object to set the value in can not be null");
    Validate.notEmpty(fieldName, "The name of the field can not be empty");

    final Field field = getField(object, fieldName);
    setValue(object, value, field);
  }
Ejemplo n.º 23
0
  /* (non-Javadoc)
   * @see org.jboss.arquillian.spi.ServiceLoader#onlyOne(java.lang.Class)
   */
  public <T> T onlyOne(Class<T> serviceClass) {
    Validate.notNull(serviceClass, "ServiceClass must be provided");

    Set<Class<? extends T>> serviceImpls =
        load(serviceClass, SecurityActions.getThreadContextClassLoader());
    verifyOnlyOneOrSameImplementation(serviceClass, serviceImpls);

    return createInstance(serviceImpls.iterator().next());
  }
Ejemplo n.º 24
0
  /**
   * Creates a mock from the specified class and sets it as the value in the field with the
   * specified name in the specified object. Supports inheritance.
   *
   * @param object The object to set the value in.
   * @param fieldName The name of the field.
   * @param classToMock The class to create a mock from, as the value to set in the field.
   */
  public static <T> T setFieldValueWithMock(
      final Object object, final String fieldName, final Class<T> classToMock) {
    Validate.notNull(classToMock, "The class to mock can not be null");

    final T mock = mock(classToMock);
    setFieldValue(object, fieldName, mock);

    return mock;
  }
 /**
  * {@inheritDoc}
  *
  * @see org.jboss.shrinkwrap.api.Archive#get(org.jboss.shrinkwrap.api.ArchivePath)
  */
 @Override
 public Node get(ArchivePath path) {
   Validate.notNull(path, "No path was specified");
   Node node = content.get(path);
   if (node == null && contains(path)) {
     node = getNestedNode(path);
   }
   return node;
 }
  /**
   * {@inheritDoc}
   *
   * @see org.jboss.shrinkwrap.api.Archive#addAsDirectory(org.jboss.shrinkwrap.api.ArchivePath)
   */
  @Override
  public T addAsDirectory(final ArchivePath path) throws IllegalArgumentException {
    // Precondition check
    Validate.notNull(path, "path must be specified");

    // Adjust the path to remove any trailing slash
    ArchivePath adjustedPath = new BasicPath(PathUtil.optionallyRemoveFollowingSlash(path.get()));
    return addAsset(adjustedPath, null);
  }
  /**
   * {@inheritDoc}
   *
   * @see org.jboss.shrinkwrap.api.Archive#contains(org.jboss.shrinkwrap.api.ArchivePath)
   */
  @Override
  public boolean contains(ArchivePath path) {
    Validate.notNull(path, "No path was specified");

    boolean found = content.containsKey(path);
    if (!found) {
      found = nestedContains(path);
    }
    return found;
  }
Ejemplo n.º 28
0
  /**
   * Retrieves concepts that are related to an entire corpus.
   *
   * @param parameters The parameters to be used in the service call, account_id, corpus and
   *     concepts are required.
   *     <ul>
   *       <li>String account_id - The account identifier.<br>
   *       <li>String corpus - The corpus name.<br>
   *       <li>RequestedFields concept_fields - Additional fields to be included in the concept
   *           objects.<br>
   *       <li>Integer level - A number in the range 0 - 3 that represents the level of popularity
   *           of related concepts.<br>
   *       <li>Integer limit - The maximum number of concepts to be returned.<br>
   *     </ul>
   *
   * @return {@link Concepts}
   */
  public Concepts getCorpusRelatedConcepts(Map<String, Object> parameters) {
    Validate.notNull(parameters.get(ACCOUNT_ID), "account_id can't be null");
    Validate.notNull(parameters.get(CORPUS), "corpus can't be null");

    String corpusId =
        createCorpusIdPath((String) parameters.get(ACCOUNT_ID), (String) parameters.get(CORPUS));

    Map<String, Object> queryParameters = new HashMap<String, Object>();
    String[] params = new String[] {LEVEL, LIMIT};
    for (String param : params) {
      if (parameters.containsKey(param)) queryParameters.put(param, parameters.get(param));
    }
    if (parameters.get(CONCEPT_FIELDS) != null) {
      RequestedFields fields = (RequestedFields) parameters.get(CONCEPT_FIELDS);
      if (fields != null && fields.getFields() != null && !fields.getFields().isEmpty())
        queryParameters.put(CONCEPT_FIELDS, fields.toString());
    }
    return executeRequest(corpusId + RELATED_CONCEPTS_PATH, queryParameters, Concepts.class);
  }
 public static boolean equals(Annotation paramAnnotation1, Annotation paramAnnotation2) {
   if (paramAnnotation1 == paramAnnotation2) {}
   for (; ; ) {
     return true;
     if ((paramAnnotation1 == null) || (paramAnnotation2 == null)) {
       return false;
     }
     Object localObject1 = paramAnnotation1.annotationType();
     Class localClass = paramAnnotation2.annotationType();
     Validate.notNull(
         localObject1,
         "Annotation %s with null annotationType()",
         new Object[] {paramAnnotation1});
     Validate.notNull(
         localClass, "Annotation %s with null annotationType()", new Object[] {paramAnnotation2});
     if (!localObject1.equals(localClass)) {
       return false;
     }
     try {
       localObject1 = ((Class) localObject1).getDeclaredMethods();
       int j = localObject1.length;
       int i = 0;
       while (i < j) {
         localClass = localObject1[i];
         if ((localClass.getParameterTypes().length == 0)
             && (isValidAnnotationMemberType(localClass.getReturnType()))) {
           Object localObject2 = localClass.invoke(paramAnnotation1, new Object[0]);
           Object localObject3 = localClass.invoke(paramAnnotation2, new Object[0]);
           boolean bool = memberEquals(localClass.getReturnType(), localObject2, localObject3);
           if (!bool) {
             return false;
           }
         }
         i += 1;
       }
       return false;
     } catch (IllegalAccessException paramAnnotation1) {
       return false;
     } catch (InvocationTargetException paramAnnotation1) {
     }
   }
 }
Ejemplo n.º 30
0
  protected void createRunMethod() throws Exception {
    if (!initialized) initialize();

    if (lazy) this.initialized = true;

    Class<?> classToLoad = Class.forName(mainClass, true, classLoader);
    Validate.notNull(classToLoad, "Invalid main class.");

    method = classToLoad.getDeclaredMethod("run");
    instance = classToLoad.newInstance();
  }