Search in sources :

Example 36 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class TestBulkSchemaAPI method testAddIllegalDynamicField.

public void testAddIllegalDynamicField() throws Exception {
    RestTestHarness harness = restTestHarness;
    String newFieldName = "illegal";
    String payload = "{\n" + "    'add-dynamic-field' : {\n" + "                 'name':'" + newFieldName + "',\n" + "                 'type':'string',\n" + "                 'stored':true,\n" + "                 'indexed':true\n" + "                 }\n" + "    }";
    String response = harness.post("/schema?wt=json", json(payload));
    Map map = (Map) ObjectBuilder.getVal(new JSONParser(new StringReader(response)));
    assertNotNull(response, map.get("errors"));
    map = getObj(harness, newFieldName, "dynamicFields");
    assertNull(newFieldName + " illegal dynamic field should not have been added to schema", map);
}
Also used : RestTestHarness(org.apache.solr.util.RestTestHarness) StringReader(java.io.StringReader) JSONParser(org.noggit.JSONParser) Map(java.util.Map)

Example 37 with JSONParser

use of org.noggit.JSONParser in project lucene-solr by apache.

the class RequestUtil method getParamsFromJSON.

private static void getParamsFromJSON(Map<String, String[]> params, String json) {
    if (json.indexOf("params") < 0) {
        return;
    }
    JSONParser parser = new JSONParser(json);
    try {
        JSONUtil.expect(parser, JSONParser.OBJECT_START);
        boolean found = JSONUtil.advanceToMapKey(parser, "params", false);
        if (!found) {
            return;
        }
        // advance to the value
        parser.nextEvent();
        Object o = ObjectBuilder.getVal(parser);
        if (!(o instanceof Map))
            return;
        Map<String, Object> map = (Map<String, Object>) o;
        // Solr params are based on String though, so we need to convert
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object val = entry.getValue();
            if (params.get(key) != null) {
                continue;
            }
            if (val == null) {
                params.remove(key);
            } else if (val instanceof List) {
                List lst = (List) val;
                String[] vals = new String[lst.size()];
                for (int i = 0; i < vals.length; i++) {
                    vals[i] = lst.get(i).toString();
                }
                params.put(key, vals);
            } else {
                params.put(key, new String[] { val.toString() });
            }
        }
    } catch (Exception e) {
        // ignore parse exceptions at this stage, they may be caused by incomplete macro expansions
        return;
    }
}
Also used : JSONParser(org.noggit.JSONParser) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException)

Aggregations

JSONParser (org.noggit.JSONParser)37 Map (java.util.Map)30 StringReader (java.io.StringReader)25 RestTestHarness (org.apache.solr.util.RestTestHarness)12 ArrayList (java.util.ArrayList)10 List (java.util.List)10 IOException (java.io.IOException)6 HashSet (java.util.HashSet)6 LinkedHashMap (java.util.LinkedHashMap)6 HttpEntity (org.apache.http.HttpEntity)6 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 SolrException (org.apache.solr.common.SolrException)4 HttpGet (org.apache.http.client.methods.HttpGet)3 HttpPost (org.apache.http.client.methods.HttpPost)3 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)3 NamedList (org.apache.solr.common.util.NamedList)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2