Examples of PrimalException


Examples of com.insightng.thirdparty.primal.exception.PrimalException

      logger.debug("http response code:" + httpCode);
      if (HTTP_CREATED != httpCode) {
        if (HTTP_UNAUTHORIZED == httpCode) {
          throw new PrimalAuthenticationException();
        } else {
          throw new PrimalException("error executing content source creation request", httpCode);
        }
      }
    } catch (final HttpException e) {
      logger.warn("error executing content source creation request", e);
      throw new PrimalException("error executing content source creation request", 0, e);
    } catch (final IOException e) {
      logger.warn("error executing content source creation request", e);
      throw new PrimalException("error executing content source creation request", 0, e);
    } finally {
      method.releaseConnection();
    }
  }
View Full Code Here

Examples of com.insightng.thirdparty.primal.exception.PrimalException

        return sources;
      }
      else if (HTTP_UNAUTHORIZED == httpCode) {
        throw new PrimalAuthenticationException();
      } else {
        throw new PrimalException("error retrieving content sources", httpCode);
      }

    } catch (final HttpException e) {
      logger.warn("error retrieving content sources", e);
      throw new PrimalException("error retrieving content sources", 0, e);
    } catch (final IOException e) {
      logger.warn("error retrieving content sources", e);
      throw new PrimalException("error retrieving content sources", 0, e);
    } finally {
      method.releaseConnection();
    }
  }
View Full Code Here

Examples of com.insightng.thirdparty.primal.exception.PrimalException

   */
  public Model getResult(String topic, String contentSource, float minScore, int maxContentCount, int wait)
      throws PrimalException {

    if (wait > MAX_WAIT_TIME) {
      throw new PrimalException("wait time is not allowed to exceed " + MAX_WAIT_TIME + " seconds", 0);
    }

    final Model model = new LinkedHashModel();

    final String requestURL = createRequestURLString(topic, contentSource, minScore, maxContentCount,
        wait);

    final GetMethod method = new GetMethod(requestURL);
    try {
      configureRequest(method);
      logRequestDetails(method, requestURL);

      final int httpCode = httpClient.executeMethod(method);
      logger.debug("response code: " + httpCode);
      if (httpCode == HTTP_OK) {
        final JsonReader reader = new JsonReader(new InputStreamReader(
            method.getResponseBodyAsStream()));

        final JsonParser jsonParser = new JsonParser();
        final JsonObject responseObject = jsonParser.parse(reader).getAsJsonObject();

        logger.trace("json response string: " + responseObject.toString());

        final JsonObject responseInfo = responseObject
            .getAsJsonObject(ResponseKey.PRIMAL_RESPONSE_INFO);

        if (responseInfo != null) {
          logger.debug("processing primal response info in response");

          final Resource responseInfoId = vf.createURI(requestURL);

          ResponseValues.processIntValue(responseInfoId, PRIMAL.CONTENT_COUNT, model,
              PRIMAL.RESPONSE_INFO, responseInfo, ResponseKey.PRIMAL_CONTENT_COUNT);

          ResponseValues.processIntValue(responseInfoId, PRIMAL.TOTAL_CONCEPTS_COUNT, model,
              PRIMAL.RESPONSE_INFO, responseInfo, ResponseKey.PRIMAL_TOTAL_CONCEPTS_COUNT);

          ResponseValues.processFloatValue(responseInfoId, PRIMAL.MIN_SEMANTIC_COVERAGE, model,
              PRIMAL.RESPONSE_INFO, responseInfo, ResponseKey.PRIMAL_MIN_SEMANTIC_COVERAGE);

          ResponseValues.processStringValue(responseInfoId, PRIMAL.STATUS, model,
              PRIMAL.RESPONSE_INFO, responseInfo,
              Version.latest == getPrimalVersion() ? ResponseKey.PRIMAL_STATUS
                  : ResponseKey.PRIMAL_STATUS);

          ResponseValues.processIntValue(responseInfoId, PRIMAL.CONTENT_FILTERED_OUT, model,
              PRIMAL.RESPONSE_INFO, responseInfo, ResponseKey.PRIMAL_CONTENT_FILTERED_OUT);

          ResponseValues.processIntValue(responseInfoId, PRIMAL.CONCEPT_COUNT, model,
              PRIMAL.RESPONSE_INFO, responseInfo, ResponseKey.PRIMAL_CONCEPT_COUNT);

          ResponseValues.processFloatValue(responseInfoId, PRIMAL.HIGH_CONTENT_SCORE, model,
              PRIMAL.RESPONSE_INFO, responseInfo, ResponseKey.PRIMAL_HIGH_CONTENT_SCORE);

          ResponseValues.processFloatValue(responseInfoId, PRIMAL.TERM_COVERAGE, model,
              PRIMAL.RESPONSE_INFO, responseInfo, ResponseKey.PRIMAL_TERM_COVERAGE);

          if (responseInfo.has(ResponseKey.PRIMAL_RECOGNIZED_TERMS)) {
            final JsonArray recTerms = responseInfo.get(ResponseKey.PRIMAL_RECOGNIZED_TERMS)
                .getAsJsonArray();

            for (int i = 0; i < recTerms.size(); i++) {
              final String term = recTerms.get(i).getAsString();
              model.add(responseInfoId, PRIMAL.RECOGNIZED_TERM, vf.createLiteral(term),
                  PRIMAL.RESPONSE_INFO);
            }
          }

          ResponseValues.processStringValue(responseInfoId, PRIMAL.VIABILITY_MESSAGE, model,
              PRIMAL.RESPONSE_INFO, responseInfo, ResponseKey.PRIMAL_VIABILITY_MESSAGE);

          ResponseValues.processFloatValue(responseInfoId, PRIMAL.SEMANTIC_RATIO, model,
              PRIMAL.RESPONSE_INFO, responseInfo, ResponseKey.PRIMAL_SEMANTIC_RATIO);

          ResponseValues.processBooleanValue(responseInfoId, PRIMAL.HAS_EXPANSION, model,
              PRIMAL.RESPONSE_INFO, responseInfo, ResponseKey.PRIMAL_HAS_EXPANSION);

        }

        logger.debug("processing SKOS concepts in response");

        final JsonObject conceptScheme = responseObject
            .getAsJsonObject(ResponseKey.SKOS_CONCEPT_SCHEME);

        final JsonElement topConcepts = conceptScheme.get(ResponseKey.SKOS_HAS_TOP_CONCEPT);

        final JsonObject collection = conceptScheme.getAsJsonObject(ResponseKey.SKOS_COLLECTION);

        final Set<Entry<String, JsonElement>> members = collection.entrySet();

        final Resource conceptSchemeId = vf.createBNode();
        model.add(conceptSchemeId, RDF.TYPE, SKOS.CONCEPT_SCHEME, PRIMAL.CONCEPTS);

        final Resource collectionId = vf.createBNode();
        model.add(collectionId, RDF.TYPE, SKOS.COLLECTION, PRIMAL.CONCEPTS);

        final List<URI> rootConcepts = new ArrayList<URI>();

        if (topConcepts.isJsonArray()) {
          URI rcURI;
          final JsonArray rcArray = topConcepts.getAsJsonArray();
          for (int i = 0; i < rcArray.size(); i++) {
            rcURI = vf.createURI(rcArray.get(i).getAsString());
            rootConcepts.add(rcURI);
          }
        } else {
          final URI rcURI = vf.createURI(topConcepts.getAsString());
          rootConcepts.add(rcURI);
        }

        for (final URI rootConceptId : rootConcepts) {
          model.add(collectionId, SKOS.MEMBER, rootConceptId, PRIMAL.CONCEPTS);
          model.add(conceptSchemeId, SKOS.HAS_TOP_CONCEPT, rootConceptId, PRIMAL.CONCEPTS);

          for (final Entry<String, JsonElement> member : members) {
            final String conceptId = member.getKey();

            final JsonObject conceptAsJson = member.getValue().getAsJsonObject();

            final JsonElement prefLabelElement = conceptAsJson.get(ResponseKey.SKOS_PREF_LABEL);
            String prefLabel = null;
            if (prefLabelElement != null && !prefLabelElement.isJsonNull()) {
              prefLabel = prefLabelElement.getAsString();
            } else {
              logger.warn("no prefLabel found for {}. Skipping concept creation", conceptId);
              continue;
            }

            final URI concept = vf.createURI(conceptId);
            model.add(concept, RDF.TYPE, SKOS.CONCEPT, PRIMAL.CONCEPTS);
            model.add(concept, SKOS.PREF_LABEL, vf.createLiteral(prefLabel), PRIMAL.CONCEPTS);

            model.add(collectionId, SKOS.MEMBER, concept, PRIMAL.CONCEPTS);

            ResponseValues.processFloatValue(concept, PRIMAL.CONCEPT_SCORE, model,
                PRIMAL.CONCEPTS, conceptAsJson, ResponseKey.PRIMAL_CONCEPT_SCORE);

            ResponseValues.processStringValue(concept, SKOS.ALT_LABEL, model, PRIMAL.CONCEPTS,
                conceptAsJson, ResponseKey.SKOS_ALT_LABEL);

            ResponseValues.processStringValue(concept, PRIMAL.SOURCE, model, PRIMAL.CONCEPTS,
                conceptAsJson, ResponseKey.PRIMAL_SOURCE);

            final JsonElement narrowerElem = conceptAsJson.get(ResponseKey.SKOS_NARROWER);

            if (narrowerElem != null) {
              final JsonArray narrower = narrowerElem.getAsJsonArray();

              for (int i = 0; i < narrower.size(); i++) {
                final String narrowerId = narrower.get(i).getAsString();
                model.add(concept, SKOS.NARROWER, vf.createURI(narrowerId), PRIMAL.CONCEPTS);
              }
            }
          }
        }

        logger.debug("processing DC part of response...");
        final JsonElement dcCollection = responseObject.get(ResponseKey.DC_COLLECTION);

        if (dcCollection.isJsonArray()) {
          final JsonArray array = dcCollection.getAsJsonArray();

          logger.debug("response contains {} content items", array.size());

          for (int i = 0; i < array.size(); i++) {
            final JsonObject contentItem = array.get(i).getAsJsonObject();

            final String dcIdentifier = contentItem.get(ResponseKey.DC_IDENTIFIER).getAsString();

            final URI contentItemId = vf.createURI(dcIdentifier);
            model.add(contentItemId, RDF.TYPE, PRIMAL.CONTENT_ITEM, PRIMAL.CONTENT);

            model.add(contentItemId, DC.IDENTIFIER, vf.createLiteral(dcIdentifier),
                PRIMAL.CONTENT);

            ResponseValues.processStringValue(contentItemId, DC.TITLE, model, PRIMAL.CONTENT,
                contentItem, ResponseKey.DC_TITLE);

            ResponseValues.processStringValue(contentItemId, DC.DESCRIPTION, model,
                PRIMAL.CONTENT, contentItem, ResponseKey.DC_DESCRIPTION);

            ResponseValues.processStringValue(contentItemId, DC.PUBLISHER, model, PRIMAL.CONTENT,
                contentItem, ResponseKey.DC_PUBLISHER);

            ResponseValues.processStringValue(contentItemId, DC.SOURCE, model, PRIMAL.CONTENT,
                contentItem, ResponseKey.DC_SOURCE);

            ResponseValues.processStringValue(contentItemId, DC.RELATION, model, PRIMAL.CONTENT,
                contentItem, ResponseKey.DC_RELATION);

            ResponseValues.processDateValue(contentItemId, DC.DATE, model, PRIMAL.CONTENT,
                contentItem, ResponseKey.DC_DATE);

            ResponseValues.processFloatValue(contentItemId, PRIMAL.CONTENT_SCORE, model,
                PRIMAL.CONTENT, contentItem, ResponseKey.PRIMAL_CONTENT_SCORE);

            final JsonArray subjects = contentItem.get(ResponseKey.DC_SUBJECT).getAsJsonArray();
            for (int j = 0; j < subjects.size(); j++) {
              final String subject = subjects.get(j).getAsString();
              model.add(contentItemId, DC.SUBJECT, vf.createURI(subject), PRIMAL.CONTENT);
            }
          }

        } else {
          // TODO can this happen?
          logger.warn("json element " + ResponseKey.DC_COLLECTION
              + " of unexpected type. Expected array.");
        }
        logger.debug("response processing complete");
      } else {
        logger.warn("response not ok " + httpCode);
        if (HTTP_UNAUTHORIZED == httpCode) {
          throw new PrimalAuthenticationException();
        } else {
          throw new PrimalException("error retrieving results.", httpCode);
        }
      }
    } catch (final HttpException e) {
      logger.warn("protocol error retrieving result", e);
      throw new PrimalException("protocol error retrieving result", 0, e);
    } catch (final IOException e) {
      logger.warn("I/O error retrieving result", e);
      throw new PrimalException("I/O error retrieving result", 0, e);
    } finally {
      method.releaseConnection();
    }

    return model;
View Full Code Here

Examples of com.insightng.thirdparty.primal.exception.PrimalException

      logger.debug("http response code:" + httpCode);
      if (HTTP_CREATED != httpCode) {
        if (HTTP_UNAUTHORIZED == httpCode) {
          throw new PrimalAuthenticationException();
        } else {
          throw new PrimalException("error executing add request", httpCode);
        }
      }
    } catch (final HttpException e) {
      logger.warn("error executing add request", e);
      throw new PrimalException("error executing add request", 0, e);
    } catch (final IOException e) {
      logger.warn("error executing add request", e);
      throw new PrimalException("error executing add request", 0, e);
    } finally {
      method.releaseConnection();
    }
  }
View Full Code Here

Examples of com.insightng.thirdparty.primal.exception.PrimalException

      logger.debug("http response code:" + httpCode);
      if (HTTP_OK != httpCode) {
        if (HTTP_UNAUTHORIZED == httpCode) {
          throw new PrimalAuthenticationException();
        } else {
          throw new PrimalException("error executing delete request.", httpCode);
        }
      }
    } catch (final HttpException e) {
      logger.warn("error executing delete request", e);
      throw new PrimalException("error executing delete request", 0, e);
    } catch (final IOException e) {
      logger.warn("error executing delete request", e);
      throw new PrimalException("error executing delete request", 0, e);
    } finally {
      method.releaseConnection();
    }
  }
View Full Code Here

Examples of com.insightng.thirdparty.primal.exception.PrimalException

   * @throws PrimalException
   *             if the user could not be deleted.
   */
  public void deleteUser(String username) throws PrimalException {
    if (username == null) {
      throw new PrimalException("username can not be null", 0);
    }

    String requestURL = null;
    try {
      requestURL = PRIMAL_USER_API_URL + "/" + URIUtil.encodePath(username, "UTF-8");
    } catch (URIException e1) {
      throw new RuntimeException(e1);
    }
    DeleteMethod method = new DeleteMethod(requestURL);
    try {
      configureRequest(method);
      logRequestDetails(method, requestURL);

      final int httpCode = httpClient.executeMethod(method);
      logger.debug("http response code:" + httpCode);
      if (HTTP_OK != httpCode) {
        if (HTTP_UNAUTHORIZED == httpCode) {
          throw new PrimalAuthenticationException();
        } else {
          logger.debug(method.getResponseBodyAsString());
          throw new PrimalException("error executing deleteUSer request", httpCode);
        }
      }
    } catch (final HttpException e) {
      logger.warn("error executing delete request", e);
      throw new PrimalException("error executing delete request", 0, e);
    } catch (final IOException e) {
      logger.warn("error executing delete request", e);
      throw new PrimalException("error executing delete request", 0, e);
    } finally {
      method.releaseConnection();
    }

  }
View Full Code Here

Examples of com.insightng.thirdparty.primal.exception.PrimalException

      if (HTTP_CREATED != httpCode) {
        if (HTTP_UNAUTHORIZED == httpCode) {
          throw new PrimalAuthenticationException();
        } else {
          logger.debug(method.getResponseBodyAsString());
          throw new PrimalException("error executing createUser request", httpCode);
        }
      } else {
        final JsonReader reader = new JsonReader(new InputStreamReader(
            method.getResponseBodyAsStream()));

        final JsonParser jsonParser = new JsonParser();
        final JsonObject responseObject = jsonParser.parse(reader).getAsJsonObject();

        createdUser = responseObject.get("name").getAsString();

      }
    } catch (final HttpException e) {
      logger.warn("error executing add request", e);
      throw new PrimalException("error executing add request", 0, e);
    } catch (final IOException e) {
      logger.warn("error executing add request", e);
      throw new PrimalException("error executing add request", 0, e);
    } finally {
      method.releaseConnection();
    }

    return createdUser;
View Full Code Here

Examples of com.insightng.thirdparty.primal.exception.PrimalException

   * @throws PrimalException
   *             if the user could not be created.
   */
  public void changePassword(String username, String password) throws PrimalException {
    if (username == null) {
      throw new PrimalException("username can not be null", 0);
    }
    if (password == null) {
      throw new PrimalException("password can not be null", 0);
    }

    String requestURL = null;
    try {
      requestURL = PRIMAL_USER_API_URL + "/" + URIUtil.encodePath(username, "UTF-8");
    } catch (URIException e2) {
      throw new RuntimeException(e2);
    }

    PostMethod method = new PostMethod(requestURL);

    final JsonObject payload = new JsonObject();
    payload.add("password", new JsonPrimitive(password));

    try {
      ByteArrayRequestEntity requestEntity = new ByteArrayRequestEntity(payload.toString().getBytes(
          "UTF-8"), "application/json");
      method.setRequestEntity(requestEntity);
    } catch (UnsupportedEncodingException e1) {
      throw new RuntimeException(e1);
    }

    try {
      configureRequest(method);
      logRequestDetails(method, requestURL);

      final int httpCode = httpClient.executeMethod(method);
      logger.debug("http response code:" + httpCode);
      if (HTTP_OK != httpCode) {
        if (HTTP_UNAUTHORIZED == httpCode) {
          throw new PrimalAuthenticationException();
        } else {
          logger.debug(method.getResponseBodyAsString());
          throw new PrimalException("error executing change password request", httpCode);
        }
      }
    } catch (final HttpException e) {
      logger.warn("error executing change request", e);
      throw new PrimalException("error executing change request", 0, e);
    } catch (final IOException e) {
      logger.warn("error executing change request", e);
      throw new PrimalException("error executing change request", 0, e);
    } finally {
      method.releaseConnection();
    }

  }
View Full Code Here

Examples of com.insightng.thirdparty.primal.exception.PrimalException

      if (HTTP_OK != httpCode) {
        if (HTTP_UNAUTHORIZED == httpCode) {
          throw new PrimalAuthenticationException();
        } else {
          logger.error("error retrieving users: " +  method.getResponseBodyAsString());
          throw new PrimalException("error retrieving users", httpCode);
        }
      } else {
        final JsonReader reader = new JsonReader(new InputStreamReader(
            method.getResponseBodyAsStream()));

        final JsonParser jsonParser = new JsonParser();
        final JsonObject response = jsonParser.parse(reader).getAsJsonObject();

        final JsonArray users = response.getAsJsonArray("users");
       
        List<String> result = new ArrayList<String>();
        for (int i = 0; i < users.size(); i++) {
          String user = users.get(i).getAsJsonObject().get("name").getAsString();
          result.add(user);
        }
        return result;
      }

    } catch (IOException e) {
      throw new PrimalException("error retrieving list of users", 0, e);
    } finally {
      method.releaseConnection();
    }
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.