Search in sources :

Example 11 with JsonConfig

use of net.sf.json.JsonConfig in project jmeter-plugins-manager by undera.

the class DependencyResolverTest method testResolveLibBeforeDetete.

@Test
public void testResolveLibBeforeDetete() throws Exception {
    URL url = PluginManagerTest.class.getResource("/lib_for_delete");
    JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(FileUtils.readFileToString(new File(url.getPath())), new JsonConfig());
    Map<Plugin, Boolean> map = new HashMap<>();
    for (Object obj : jsonArray) {
        Plugin plugin = Plugin.fromJSON((JSONObject) obj);
        plugin.detectInstalled(new HashSet<Plugin>());
        plugin.installedPath = "";
        plugin.installedVersion = "0.1";
        map.put(plugin, !plugin.getName().startsWith("delete"));
    }
    DependencyResolver resolver = new DependencyResolver(map);
    Set<String> libs = resolver.getLibDeletions();
    assertEquals(3, libs.size());
    assertTrue(libs.contains("ApacheJMeter_core"));
    assertTrue(libs.contains("commons-lang3"));
    assertTrue(libs.contains("commons-httpclient"));
}
Also used : HashMap(java.util.HashMap) JsonConfig(net.sf.json.JsonConfig) JSONArray(net.sf.json.JSONArray) URL(java.net.URL) JSONObject(net.sf.json.JSONObject) File(java.io.File) Test(org.junit.Test)

Example 12 with JsonConfig

use of net.sf.json.JsonConfig in project jmeter-plugins-manager by undera.

the class DependencyResolverTest method testResolveDowngradeWithNPE.

@Test
public void testResolveDowngradeWithNPE() throws Exception {
    URL url = PluginManagerTest.class.getResource("/self_npe.json");
    JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(FileUtils.readFileToString(new File(url.getPath())), new JsonConfig());
    Map<Plugin, Boolean> map = new HashMap<>();
    for (Object obj : jsonArray) {
        Plugin plugin = Plugin.fromJSON((JSONObject) obj);
        plugin.detectInstalled(new HashSet<Plugin>());
        plugin.installedPath = "";
        plugin.installedVersion = "0.14";
        plugin.candidateVersion = "0.13";
        map.put(plugin, true);
    }
    DependencyResolver resolver = new DependencyResolver(map);
    Map<String, String> libs = resolver.getLibAdditions();
    assertEquals(1, libs.size());
    assertNotNull(libs.get("cmdbeginner"));
    Set<Plugin> pluginsAdd = resolver.getAdditions();
    assertEquals(1, pluginsAdd.size());
    assertEquals("jpgc-plugins-manager", pluginsAdd.toArray(new Plugin[1])[0].getID());
    Set<Plugin> pluginsDelete = resolver.getDeletions();
    assertEquals(1, pluginsDelete.size());
    assertEquals("jpgc-plugins-manager", pluginsDelete.toArray(new Plugin[1])[0].getID());
}
Also used : HashMap(java.util.HashMap) JsonConfig(net.sf.json.JsonConfig) JSONArray(net.sf.json.JSONArray) URL(java.net.URL) JSONObject(net.sf.json.JSONObject) File(java.io.File) Test(org.junit.Test)

Example 13 with JsonConfig

use of net.sf.json.JsonConfig in project jmeter-plugins-manager by undera.

the class DependencyResolverTest method testUpdateWhenInstall.

@Test
public void testUpdateWhenInstall() throws Exception {
    URL url = PluginManagerTest.class.getResource("/installed.json");
    JSONArray jsonArray = (JSONArray) JSONSerializer.toJSON(FileUtils.readFileToString(new File(url.getPath())), new JsonConfig());
    Map<Plugin, Boolean> map = new HashMap<>();
    for (Object obj : jsonArray) {
        Plugin plugin = Plugin.fromJSON((JSONObject) obj);
        plugin.detectInstalled(new HashSet<Plugin>());
        map.put(plugin, true);
    }
    DependencyResolver resolver = new DependencyResolver(map);
    Set<String> libsDeletions = resolver.getLibDeletions();
    assertEquals(1, libsDeletions.size());
    assertTrue(libsDeletions.contains("commons-codec"));
    Map<String, String> libsAdditions = resolver.getLibAdditions();
    assertEquals(1, libsAdditions.size());
    assertEquals("commons-codec-99.99.jar", libsAdditions.get("commons-codec"));
}
Also used : HashMap(java.util.HashMap) JsonConfig(net.sf.json.JsonConfig) JSONArray(net.sf.json.JSONArray) URL(java.net.URL) JSONObject(net.sf.json.JSONObject) File(java.io.File) Test(org.junit.Test)

Example 14 with JsonConfig

use of net.sf.json.JsonConfig in project jmeter-plugins-manager by undera.

the class JARSourceHTTP method getJSON.

protected JSON getJSON(String uri) throws IOException {
    log.debug("Requesting " + uri);
    HttpRequestBase get = new HttpGet(uri);
    HttpParams requestParams = get.getParams();
    get.setHeader("Accept-Encoding", "gzip");
    requestParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
    requestParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
    HttpResponse result = execute(get);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    HttpEntity entity = result.getEntity();
    try {
        entity.writeTo(bos);
        byte[] bytes = bos.toByteArray();
        if (bytes == null) {
            bytes = "null".getBytes();
        }
        String response = isGZIPResponse(result) ? convertGZIPToString(bytes) : new String(bytes);
        int statusCode = result.getStatusLine().getStatusCode();
        if (statusCode >= 300) {
            log.warn("Response with code " + result + ": " + response);
            throw new IOException("Repository responded with wrong status code: " + statusCode);
        } else {
            log.debug("Response with code " + result + ": " + response);
        }
        return JSONSerializer.toJSON(response, new JsonConfig());
    } finally {
        get.abort();
        try {
            entity.getContent().close();
        } catch (IOException | IllegalStateException e) {
            log.warn("Exception in finalizing request", e);
        }
    }
}
Also used : HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpEntity(org.apache.http.HttpEntity) JsonConfig(net.sf.json.JsonConfig) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) HttpParams(org.apache.http.params.HttpParams)

Example 15 with JsonConfig

use of net.sf.json.JsonConfig in project lobcder by skoulouzis.

the class JsonPropFindHandler method sendContent.

public void sendContent(PropFindableResource wrappedResource, String encodedUrl, OutputStream out, Range range, Map<String, String> params, String contentType) throws IOException, NotAuthorizedException, BadRequestException {
    log.debug("sendContent: " + encodedUrl);
    JsonConfig cfg = new JsonConfig();
    cfg.setIgnoreTransientFields(true);
    cfg.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
    JSON json;
    Writer writer = new PrintWriter(out);
    String[] arr;
    if (propertyBuilder == null) {
        if (wrappedResource instanceof CollectionResource) {
            List<? extends Resource> children = ((CollectionResource) wrappedResource).getChildren();
            json = JSONSerializer.toJSON(toSimpleList(children), cfg);
        } else {
            json = JSONSerializer.toJSON(toSimple(wrappedResource), cfg);
        }
    } else {
        // use propfind handler
        String sFields = params.get("fields");
        Set<QName> fields = new HashSet<QName>();
        Map<QName, String> aliases = new HashMap<QName, String>();
        if (sFields != null && sFields.length() > 0) {
            arr = sFields.split(",");
            for (String s : arr) {
                parseField(s, fields, aliases);
            }
        }
        String sDepth = params.get("depth");
        int depth = 1;
        if (sDepth != null && sDepth.trim().length() > 0) {
            depth = Integer.parseInt(sDepth);
        }
        String href = encodedUrl.replace("/_DAV/PROPFIND", "");
        PropertiesRequest parseResult = new PropertiesRequest(toProperties(fields));
        LogUtils.debug(log, "prop builder: ", propertyBuilder.getClass(), "href", href);
        List<PropFindResponse> props;
        try {
            props = propertyBuilder.buildProperties(wrappedResource, depth, parseResult, href);
        } catch (URISyntaxException ex) {
            throw new RuntimeException("Requested url is not properly encoded: " + href, ex);
        }
        String where = params.get("where");
        filterResults(props, where);
        List<Map<String, Object>> list = helper.toMap(props, aliases);
        json = JSONSerializer.toJSON(list, cfg);
    }
    json.write(writer);
    writer.flush();
}
Also used : CollectionResource(io.milton.resource.CollectionResource) HashMap(java.util.HashMap) JsonConfig(net.sf.json.JsonConfig) QName(javax.xml.namespace.QName) JSON(net.sf.json.JSON) URISyntaxException(java.net.URISyntaxException) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) PrintWriter(java.io.PrintWriter) HashSet(java.util.HashSet)

Aggregations

JsonConfig (net.sf.json.JsonConfig)28 JSONObject (net.sf.json.JSONObject)17 File (java.io.File)10 HashMap (java.util.HashMap)10 JSONArray (net.sf.json.JSONArray)10 Test (org.junit.Test)10 URL (java.net.URL)7 PrintWriter (java.io.PrintWriter)5 JSON (net.sf.json.JSON)5 Writer (java.io.Writer)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 Iterator (java.util.Iterator)2 NewBeanInstanceStrategy (net.sf.json.util.NewBeanInstanceStrategy)2 FlexClass (org.jaffa.flexfields.FlexClass)2 FlexCriteriaBean (org.jaffa.flexfields.FlexCriteriaBean)2 Status (io.milton.http.Response.Status)1 NameAndError (io.milton.http.webdav.PropFindResponse.NameAndError)1 CollectionResource (io.milton.resource.CollectionResource)1