Search in sources :

Example 6 with JSONArray

use of net.minidev.json.JSONArray in project scheduler by btrplace.

the class SeqConverter method fromJSON.

@Override
public Seq fromJSON(Model mo, JSONObject o) throws JSONConverterException {
    checkId(o);
    List<VM> s = new ArrayList<>();
    for (Object ob : (JSONArray) o.get("vms")) {
        s.add(getVM(mo, (Integer) ob));
    }
    return new Seq(s);
}
Also used : VM(org.btrplace.model.VM) JSONs.getVM(org.btrplace.json.JSONs.getVM) ArrayList(java.util.ArrayList) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) Seq(org.btrplace.model.constraint.Seq)

Example 7 with JSONArray

use of net.minidev.json.JSONArray in project ddf by codice.

the class GeoNamesWebService method getLocation.

@Override
public GeoResult getLocation(String location) {
    location = getUrlEncodedLocation(location);
    String urlStr = String.format("%s://%s/searchJSON?q=%s&username=%s", GEONAMES_PROTOCOL, GEONAMES_API_ADDRESS, location, USERNAME);
    Object result = query(urlStr);
    if (result != null) {
        if (result instanceof JSONObject) {
            JSONObject jsonResult = (JSONObject) result;
            JSONArray geonames = (JSONArray) jsonResult.get(GEONAMES_KEY);
            if (geonames != null && geonames.size() > 0) {
                JSONObject firstResult = (JSONObject) geonames.get(0);
                if (firstResult != null) {
                    double lat = Double.valueOf((String) firstResult.get(LAT_KEY));
                    double lon = Double.valueOf((String) firstResult.get(LON_KEY));
                    Long population = (Long) firstResult.get(POPULATION_KEY);
                    String adminCode = (String) firstResult.get(ADMIN_CODE_KEY);
                    return GeoResultCreator.createGeoResult((String) firstResult.get(PLACENAME_KEY), lat, lon, adminCode, population);
                }
            }
        }
    }
    return null;
}
Also used : JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject)

Example 8 with JSONArray

use of net.minidev.json.JSONArray in project ddf by codice.

the class GeoNamesWebService method getNearbyCity.

@Override
public NearbyLocation getNearbyCity(String locationWkt) {
    notNull(locationWkt, "argument locationWkt may not be null");
    Point wktCenterPoint = createPointFromWkt(locationWkt);
    String urlStr = String.format("%s://%s/findNearbyPlaceNameJSON?lat=%f&lng=%f&maxRows=1&username=%s&cities=cities5000", GEONAMES_PROTOCOL, GEONAMES_API_ADDRESS, wktCenterPoint.getY(), wktCenterPoint.getX(), USERNAME);
    Object result = query(urlStr);
    if (result instanceof JSONObject) {
        JSONObject jsonResult = (JSONObject) result;
        JSONArray geonames = (JSONArray) jsonResult.get(GEONAMES_KEY);
        if (geonames != null && geonames.size() > 0) {
            JSONObject firstResult = (JSONObject) geonames.get(0);
            if (firstResult != null) {
                double lat = Double.valueOf((String) firstResult.get(LAT_KEY));
                double lon = Double.valueOf((String) firstResult.get(LON_KEY));
                String cityName = (String) firstResult.get(PLACENAME_KEY);
                Point cityPoint = new PointImpl(lon, lat, SpatialContext.GEO);
                return new NearbyLocationImpl(wktCenterPoint, cityPoint, cityName);
            }
        }
    }
    return null;
}
Also used : NearbyLocationImpl(org.codice.ddf.spatial.geocoding.context.impl.NearbyLocationImpl) JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray) JSONObject(net.minidev.json.JSONObject) Point(org.locationtech.spatial4j.shape.Point) PointImpl(org.locationtech.spatial4j.shape.impl.PointImpl)

Example 9 with JSONArray

use of net.minidev.json.JSONArray in project ddf by codice.

the class TestGeoCoderEndpoint method testQuery.

@Test
public void testQuery() {
    JSONObject jsonObject = this.geoCoderEndpoint.doQuery("Phoenix");
    JSONArray resourceSets = (JSONArray) jsonObject.get("resourceSets");
    assertThat(resourceSets.size(), is(1));
    JSONObject resources = (JSONObject) resourceSets.get(0);
    JSONArray resourceElements = (JSONArray) resources.get("resources");
    assertThat(resourceElements.size(), is(1));
    JSONObject resource = (JSONObject) resourceElements.get(0);
    JSONObject point = (JSONObject) resource.get("point");
    JSONArray bbox = (JSONArray) resource.get("bbox");
    String name = (String) resource.get("name");
    assertThat(name, is("Phoenix"));
    assertThat(bbox.size(), is(4));
    String type = (String) point.get("type");
    assertThat(type, is("Point"));
    JSONArray coordinates = (JSONArray) point.get("coordinates");
    assertThat(coordinates.size(), is(2));
}
Also used : JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray) Mockito.anyString(org.mockito.Mockito.anyString) Test(org.junit.Test)

Example 10 with JSONArray

use of net.minidev.json.JSONArray in project ddf by codice.

the class TestLogoutService method testLogout.

@Test
public void testLogout() throws IOException, ParseException, SecurityServiceException {
    KarafLogoutAction karafLogoutActionProvider = new KarafLogoutAction();
    LdapLogoutAction ldapLogoutActionProvider = new LdapLogoutAction();
    Action karafLogoutAction = karafLogoutActionProvider.getAction(null);
    Action ldapLogoutAction = ldapLogoutActionProvider.getAction(null);
    LogoutService logoutService = new LogoutService();
    logoutService.setHttpSessionFactory(sessionFactory);
    logoutService.setSecurityManager(sm);
    logoutService.setLogoutActionProviders(Arrays.asList(karafLogoutActionProvider, ldapLogoutActionProvider));
    String responseMessage = IOUtils.toString((ByteArrayInputStream) logoutService.getActionProviders(null).getEntity());
    JSONArray actionProperties = (JSONArray) new JSONParser().parse(responseMessage);
    assertEquals(2, actionProperties.size());
    JSONObject karafActionProperty = ((JSONObject) actionProperties.get(0));
    assertEquals(karafActionProperty.get("description"), karafLogoutAction.getDescription());
    assertEquals(karafActionProperty.get("realm"), karafLogoutAction.getId().substring(karafLogoutAction.getId().lastIndexOf(".") + 1));
    assertEquals(karafActionProperty.get("title"), karafLogoutAction.getTitle());
    assertEquals(karafActionProperty.get("url"), karafLogoutAction.getUrl().toString());
    JSONObject ldapActionProperty = ((JSONObject) actionProperties.get(1));
    assertEquals(ldapActionProperty.get("description"), ldapLogoutAction.getDescription());
    assertEquals(ldapActionProperty.get("realm"), ldapLogoutAction.getId().substring(ldapLogoutAction.getId().lastIndexOf(".") + 1));
    assertEquals(ldapActionProperty.get("title"), ldapLogoutAction.getTitle());
    assertEquals(ldapActionProperty.get("url"), ldapLogoutAction.getUrl().toString());
}
Also used : Action(ddf.action.Action) JSONObject(net.minidev.json.JSONObject) JSONArray(net.minidev.json.JSONArray) JSONParser(net.minidev.json.parser.JSONParser) Test(org.junit.Test)

Aggregations

JSONArray (net.minidev.json.JSONArray)55 JSONObject (net.minidev.json.JSONObject)41 Test (org.junit.Test)9 HashMap (java.util.HashMap)8 Map (java.util.Map)6 ArrayList (java.util.ArrayList)5 JSONParser (net.minidev.json.parser.JSONParser)5 Test (org.testng.annotations.Test)5 DocumentContext (com.jayway.jsonpath.DocumentContext)3 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 JSONConverterException (org.btrplace.json.JSONConverterException)3 VM (org.btrplace.model.VM)3 PathNotFoundException (com.jayway.jsonpath.PathNotFoundException)2 ContentType (ddf.catalog.data.ContentType)2 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)2 SourceInfoResponse (ddf.catalog.operation.SourceInfoResponse)2 SourceInfoRequestEnterprise (ddf.catalog.operation.impl.SourceInfoRequestEnterprise)2 SourceDescriptor (ddf.catalog.source.SourceDescriptor)2 TIntObjectHashMap (gnu.trove.map.hash.TIntObjectHashMap)2