Пример #1
0
 /**
  * Queries a single link and populates the object with its settings
  *
  * @param The query parameters
  * @throws SynchronizationException if getting and initializing the link details fail
  */
 private void getLink(JsonValue query) throws SynchronizationException {
   JsonValue results =
       linkQuery(
           mapping.getService().getServerContext(),
           mapping.getService().getConnectionFactory(),
           query);
   if (results.size() == 1) {
     fromJsonValue(results.get(0));
   } else if (results.size() > 1) { // shouldn't happen if index is unique
     throw new SynchronizationException("More than one link found");
   }
 }
 /**
  * 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;
 }
  @Test
  public void shouldGetWithSSL() throws ResourceException {

    // Given
    setSslConfiguration();
    final String uri = "URI";
    final Map<String, String> queryParameters = new HashMap<String, String>();
    final Map<String, String> headers = new HashMap<String, String>();
    final Context context = mock(Context.class);
    final ConcurrentHashMap<String, Object> requestAttributes =
        new ConcurrentHashMap<String, Object>();
    final JSONObject restResponse = mock(JSONObject.class);

    given(resource.getContext()).willReturn(context);
    given(context.getAttributes()).willReturn(requestAttributes);
    given(resource.get(JSONObject.class)).willReturn(restResponse);
    given(restResponse.toString()).willReturn("{}");

    // When
    final JsonValue response = restClient.get(uri, queryParameters, headers);

    // Then
    verify(resource, never()).addQueryParameter(anyString(), anyString());
    verify(requestHeaders, never()).set(anyString(), anyString());
    verify(resource).getContext();
    assertTrue(requestAttributes.containsKey("sslContextFactory"));
    assertEquals(response.size(), 0);
  }
  @Test
  public void shouldGetWithEmptyQueryParameters() throws ResourceException {

    // Given
    final String uri = "URI";
    final Map<String, String> queryParameters = new HashMap<String, String>();
    final Map<String, String> headers = new HashMap<String, String>();
    final JSONObject restResponse = mock(JSONObject.class);

    given(resource.get(JSONObject.class)).willReturn(restResponse);
    given(restResponse.toString()).willReturn("{}");

    // When
    final JsonValue response = restClient.get(uri, queryParameters, headers);

    // Then
    verify(resource, never()).addQueryParameter(anyString(), anyString());
    verify(requestHeaders, never()).set(anyString(), anyString());
    verify(resource, never()).getContext();
    assertEquals(response.size(), 0);
  }
Пример #5
0
 /**
  * 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);
   }
 }