/**
  * Loads the configuration properties in the configuration property file associated with the
  * framework installation; these properties are accessible to the framework and to bundles and are
  * intended for configuration purposes. By default, the configuration property file is located in
  * the <tt>conf/</tt> directory and is called " <tt>config.properties</tt>".
  *
  * @return A <tt>Map<String, Object></tt> instance or <tt>null</tt> if there was an error.
  */
 protected Map<String, String> loadConfigProperties(
     JsonValue configuration, URI projectDirectory) {
   JsonValue systemProperties = configuration.get(CONFIG_PROPERTIES_PROP);
   if (systemProperties.isMap()) {
     // Substitute all variables
     systemProperties = systemProperties.copy();
   } else {
     Properties props =
         loadPropertyFile(
             projectDirectory,
             systemProperties
                 .expect(String.class)
                 .defaultTo(CONFIG_PROPERTIES_FILE_VALUE)
                 .asString());
     if (props == null) return new HashMap<String, String>(0);
     // Perform variable substitution on specified properties.
     systemProperties = (new JsonValue(props, null, Arrays.asList(transformer))).copy();
   }
   Map<String, String> config = new HashMap<String, String>(systemProperties.size());
   for (Map.Entry<String, Object> entry : systemProperties.asMap().entrySet()) {
     if (entry.getValue() instanceof String) {
       // Excluce the null and non String values
       config.put(entry.getKey(), (String) entry.getValue());
     }
   }
   return config;
 }
 /**
  * TODO: Description.
  *
  * @param service
  * @param config TODO.
  * @throws JsonValueException TODO.
  */
 public Policy(SynchronizationService service, JsonValue config) throws JsonValueException {
   this.service = service;
   situation = config.get("situation").required().asEnum(Situation.class);
   JsonValue action = config.get("action").required();
   if (action.isString()) {
     this.action = action.asEnum(Action.class);
     this.script = null;
     this.scriptScope = null;
   } else {
     this.action = null;
     this.script = Scripts.newInstance("Policy", action);
     if (action.isMap() && action.asMap().size() > 2) {
       // If there is additional attributes then copy them
       scriptScope = action.copy().asMap();
       scriptScope.remove("type");
       scriptScope.remove("source");
       scriptScope.remove("file");
     } else {
       scriptScope = null;
     }
   }
   JsonValue pAction = config.get("postAction");
   if (pAction.isNull()) {
     this.postAction = null;
   } else {
     this.postAction = Scripts.newInstance("PostAction", pAction);
   }
 }
  private Map<String, Object> toMap(JsonRepresentation entity) throws BadRequestException {
    if (entity == null) {
      return Collections.emptyMap();
    }

    try {
      final String jsonString = entity.getJsonObject().toString();
      if (StringUtils.isNotEmpty(jsonString)) {
        JsonValue jsonContent = JsonValueBuilder.toJsonValue(jsonString);
        return jsonContent.asMap(Object.class);
      }

      return Collections.emptyMap();
    } catch (JSONException e) {
      throw new BadRequestException(e.getMessage());
    }
  }
Beispiel #4
0
  /**
   * Issues a query on link(s)
   *
   * @param router the ServerContext
   * @param connectionFactory the connection factory
   * @param query the query parameters
   * @return The query results
   * @throws SynchronizationException if getting and initializing the link details fail
   */
  private static JsonValue linkQuery(
      ServerContext router, ConnectionFactory connectionFactory, JsonValue query)
      throws SynchronizationException {
    JsonValue results = null;
    try {
      final Collection<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

      QueryRequest request =
          RequestUtil.buildQueryRequestFromParameterMap(linkId(null), query.asMap());
      connectionFactory
          .getConnection()
          .query(
              router,
              request,
              new QueryResultHandler() {
                @Override
                public void handleError(ResourceException error) {
                  // ignore
                }

                @Override
                public boolean handleResource(Resource resource) {
                  result.add(resource.getContent().asMap());
                  return true;
                }

                @Override
                public void handleResult(QueryResult result) {
                  // ignore
                }
              });

      results = new JsonValue(result).required().expect(List.class);
    } catch (JsonValueException jve) {
      throw new SynchronizationException("Malformed link query response", jve);
    } catch (ResourceException ose) {
      throw new SynchronizationException("Link query failed", ose);
    }
    return results;
  }
 /**
  * Returns a JSON object containing only the specified fields from the provided JSON value. If the
  * list of fields is empty then the value is returned unchanged.
  *
  * <p><b>NOTE:</b> this method only performs a shallow copy of extracted fields, so changes to the
  * filtered JSON value may impact the original JSON value, and vice-versa.
  *
  * @param resource The JSON value whose fields are to be filtered.
  * @param fields The list of fields to be extracted.
  * @return The filtered JSON value.
  */
 public static JsonValue filterResource(
     final JsonValue resource, final Collection<JsonPointer> fields) {
   if (fields.isEmpty() || resource.isNull() || resource.size() == 0) {
     return resource;
   } else {
     final Map<String, Object> filtered = new LinkedHashMap<String, Object>(fields.size());
     for (JsonPointer field : fields) {
       if (field.isEmpty()) {
         // Special case - copy resource fields (assumes Map).
         filtered.putAll(resource.asMap());
       } else {
         // FIXME: what should we do if the field refers to an array element?
         final JsonValue value = resource.get(field);
         if (value != null) {
           final String key = field.leaf();
           filtered.put(key, value.getObject());
         }
       }
     }
     return new JsonValue(filtered);
   }
 }