/** * Convert from a JSON file * @param resource input file * @return the parsed JSON * @throws IOException IO problems * @throws JsonMappingException failure to map from the JSON to this class */ @SuppressWarnings({"IOResourceOpenedButNotSafelyClosed"}) public synchronized T fromResource(String resource) throws IOException, JsonParseException, JsonMappingException { InputStream resStream = null; try { resStream = this.getClass().getResourceAsStream(resource); if (resStream == null) { throw new FileNotFoundException(resource); } return mapper.readValue(resStream, classType); } catch (IOException e) { LOG.error("Exception while parsing json resource {}: {}", resource, e); throw e; } finally { IOUtils.closeStream(resStream); } }
public static <T, R> R processHttpRequest(String completeURL, Class<R> responseType, T request, Map<String, String> headers, HttpMethod method) { final Map<String, Object> parameters = getParams(request); try { return mapper.readValue( processHttpRequest(completeURL, parameters, headers, method), responseType); } catch (JsonParseException | JsonMappingException je) { throw new ServiceException(CommonExceptionCodes.HTTP_CLIENT_EXCEPTION.code(), "Could not parse response into specified response type. " + "Error: " + je); } catch (IOException ioe) { throw new InternalServerException(); } }
@Test public void testBucketCache() throws JsonGenerationException, JsonMappingException, IOException { this.conf.set(HConstants.BUCKET_CACHE_IOENGINE_KEY, "offheap"); this.conf.setInt(HConstants.BUCKET_CACHE_SIZE_KEY, 100); CacheConfig cc = new CacheConfig(this.conf); assertTrue(cc.getBlockCache() instanceof CombinedBlockCache); logPerBlock(cc.getBlockCache()); final int count = 3; addDataAndHits(cc.getBlockCache(), count); // The below has no asserts. It is just exercising toString and toJSON code. LOG.info(cc.getBlockCache().getStats()); BlockCacheUtil.CachedBlocksByFile cbsbf = logPerBlock(cc.getBlockCache()); LOG.info(cbsbf); logPerFile(cbsbf); bucketCacheReport(cc.getBlockCache()); LOG.info(BlockCacheUtil.toJSON(cbsbf)); }
@Test public void testLruBlockCache() throws JsonGenerationException, JsonMappingException, IOException { CacheConfig cc = new CacheConfig(this.conf); assertTrue(cc.isBlockCacheEnabled()); assertTrue(CacheConfig.DEFAULT_IN_MEMORY == cc.isInMemory()); assertTrue(cc.getBlockCache() instanceof LruBlockCache); logPerBlock(cc.getBlockCache()); addDataAndHits(cc.getBlockCache(), 3); // The below has no asserts. It is just exercising toString and toJSON code. BlockCache bc = cc.getBlockCache(); LOG.info("count=" + bc.getBlockCount() + ", currentSize=" + bc.getCurrentSize() + ", freeSize=" + bc.getFreeSize() ); LOG.info(cc.getBlockCache().getStats()); BlockCacheUtil.CachedBlocksByFile cbsbf = logPerBlock(cc.getBlockCache()); LOG.info(cbsbf); logPerFile(cbsbf); bucketCacheReport(cc.getBlockCache()); LOG.info(BlockCacheUtil.toJSON(cbsbf)); }
private void logPerFile(final BlockCacheUtil.CachedBlocksByFile cbsbf) throws JsonGenerationException, JsonMappingException, IOException { for (Map.Entry<String, NavigableSet<CachedBlock>> e: cbsbf.getCachedBlockStatsByFile().entrySet()) { int count = 0; long size = 0; int countData = 0; long sizeData = 0; for (CachedBlock cb: e.getValue()) { count++; size += cb.getSize(); BlockType bt = cb.getBlockType(); if (bt != null && bt.isData()) { countData++; sizeData += cb.getSize(); } } LOG.info("filename=" + e.getKey() + ", count=" + count + ", countData=" + countData + ", size=" + size + ", sizeData=" + sizeData); LOG.info(BlockCacheUtil.toJSON(e.getKey(), e.getValue())); } }
public void output(Map<String, Object> parameters, boolean withRequest, boolean prettyPrint) throws JsonGenerationException, JsonMappingException, IOException { Map<String, Object> result = new LinkedHashMap<String, Object>(); // put request to result if (withRequest) { Map<String, Object> request = new LinkedHashMap<String, Object>(); request.put("command", getName()); request.put("parameters", parameters); result.put("request", request); } // put response to result putResponse("status", getStatus()); putResponse("message", getMessage()); result.put("response", getResponse()); ObjectMapper mapper = new ObjectMapper(); if (prettyPrint) { // enable pretty print System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(result)); } else { System.out.println(mapper.writeValueAsString(result)); } }
/** * Reads a collection of FileTrackingStatus.<br/> * If plain text is a list of line separated plain text.<br/> * If json this should be a json array.<br/> * * @param format * @param reader * @return * @throws JsonParseException * @throws JsonMappingException * @throws IOException */ @SuppressWarnings("unchecked") public Collection<FileTrackingStatus> readList(FORMAT format, Reader reader) throws JsonParseException, JsonMappingException, IOException { Collection<FileTrackingStatus> coll = null; if (format.equals(FORMAT.JSON)) { coll = (Collection<FileTrackingStatus>) mapper.readValue(reader, new TypeReference<Collection<FileTrackingStatus>>() { }); } else { BufferedReader buff = new BufferedReader(reader); coll = new ArrayList<FileTrackingStatus>(); String line = null; while ((line = buff.readLine()) != null) { coll.add(read(FORMAT.TXT, line)); } } return coll; }
public CommandImpl(Long id, IPolicy policy, ITask task, List<String> dnList, DNType dnType, List<String> uidList, String commandOwnerUid, Date activationDate, Date expirationDate, Date createDate, List<CommandExecutionImpl> commandExecutions, boolean sentMail) throws JsonGenerationException, JsonMappingException, IOException { this.id = id; this.policy = (PolicyImpl) policy; this.task = (TaskImpl) task; ObjectMapper mapper = new ObjectMapper(); this.dnListJsonString = mapper.writeValueAsString(dnList); setDnType(dnType); this.uidListJsonString = uidList != null ? mapper.writeValueAsString(uidList) : null; this.commandOwnerUid = commandOwnerUid; this.activationDate = activationDate; this.expirationDate = expirationDate; this.createDate = createDate; this.commandExecutions = commandExecutions; this.sentMail = sentMail; }
public CommandImpl(ICommand command) throws JsonGenerationException, JsonMappingException, IOException { this.id = command.getId(); this.policy = (PolicyImpl) command.getPolicy(); this.task = (TaskImpl) command.getTask(); ObjectMapper mapper = new ObjectMapper(); this.dnListJsonString = mapper.writeValueAsString(command.getDnList()); setDnType(command.getDnType()); this.uidListJsonString = command.getUidList() != null ? mapper.writeValueAsString(command.getUidList()) : null; this.commandOwnerUid = command.getCommandOwnerUid(); this.activationDate = command.getActivationDate(); this.expirationDate = command.getExpirationDate(); this.createDate = command.getCreateDate(); this.sentMail = command.isSentMail(); this.mailThreadingActive=command.isMailThreadingActive(); // Convert ICommandExecution to CommandExecutionImpl List<? extends ICommandExecution> tmpCommandExecutions = command.getCommandExecutions(); if (tmpCommandExecutions != null) { for (ICommandExecution commandExecution : tmpCommandExecutions) { addCommandExecution(commandExecution); } } }
public String build(String script) throws JsonParseException, JsonMappingException, IOException{ //if no dependencies just return itself. if(!script.contains(IMPORT_PREFIX)){ return script; } //add the initial script. sb.append(script); //build the set of dependencies. this.resolveDependencies(script); //with all the script files build one big script. for(String path : this.dependencies){ sb.append(this.resolveScript(path)); sb.append("\n\n"); } return sb.toString(); }
public void resolveDependencies(String script) throws JsonParseException, JsonMappingException, IOException{ String[] parsed = script.split("\n"); for(String str : parsed){ if(str.contains(IMPORT_PREFIX)){ str = StrUtils.rightBack(str, IMPORT_PREFIX); String[] references = str.split(StringCache.COMMA); for(String path : references){ path = path.trim(); if(!dependencies.contains(path)){ dependencies.add(path); String newScript = this.resolveScript(path); //recurse to pull in all the script files. this.resolveDependencies(newScript); } } } } }
public static SolrInputDocument createDocument(String dataStr) throws JsonParseException, JsonMappingException, IOException { Map<String, Object> dataMap = new ObjectMapper().readValue(dataStr, new TypeReference<HashMap<String, Object>>() { }); SolrInputDocument document = new SolrInputDocument(); for (Iterator<String> i = dataMap.keySet().iterator(); i.hasNext();) { String fieldName = i.next(); Object fieldValue = dataMap.get(fieldName); document.addField(fieldName, fieldValue); } return document; }
public void testCreateDocument() throws JsonParseException, JsonMappingException, IOException { String data = "{\"id\":\"1\",\"title_txt_en\":\"SolrJ\",\"description_txt_en\":\"SolrJ is an API that makes it easy for Java applications to talk to Solr.\"}"; SolrInputDocument document = SolrJExampleUtil.createDocument(data); String expectedId = "1"; String actualId = document.getFieldValue("id").toString(); assertEquals(expectedId, actualId); String expectedTitle = "SolrJ"; String actualTitle = document.getFieldValue("title_txt_en").toString(); assertEquals(expectedTitle, actualTitle); String expectedDescription = "SolrJ is an API that makes it easy for Java applications to talk to Solr."; String actualDescription = document.getFieldValue("description_txt_en").toString(); assertEquals(expectedDescription, actualDescription); }
/** * {@inheritDoc} */ @Override public Status mapException(JsonMappingException exception) { LOGGER.error(exception.getMessage(), exception); List<Reference> references = exception.getPath(); if (references != null) { String message = new String("Wrong elements: "); for (Reference reference : references) { message += reference.getFieldName() + ", "; } message = message.substring(0, message.length() - 2); message += "."; return new Status("error.rest.badrequest", null, BAD_REQUEST, new Reason( new StaticLocalizedMessage(message), null, null)); } return new Status("error.rest.badrequest", null, BAD_REQUEST); }
/** * Handle messages of {@link JsonMappingException} * * @param exception * {@link JsonMappingException} * @param apiResult * error result * @return {@link ApiResult} with error messages */ @Override public ApiResult<Object> handle(JsonMappingException exception, ApiResult<Object> apiResult) { apiResult.setMessage(getErrorMessage(exception)); List<Reference> references = exception.getPath(); List<ApiResultError> apiResultErrors = new ArrayList<ApiResultError>(); if (references != null) { ApiResultError error = new ApiResultError(); error.setCause("Exception"); String message = new String("wrong elements: "); for (Reference reference : references) { message += reference.getFieldName() + ", "; } message = message.substring(0, message.length() - 2); message += "."; error.setMessage(message); apiResultErrors.add(error); } apiResult.setErrors(apiResultErrors); return apiResult; }
/** * Test convert to cnt message. * * @throws MessageParsingException * in case parsing went wrong * @throws JsonParseException * the json parse exception * @throws JsonMappingException * the json mapping exception * @throws IOException * Signals that an I/O exception has occurred. */ @Test public void testConvertToCNTMessage() throws MessageParsingException, JsonParseException, JsonMappingException, IOException { TransferMessage tm = new TransferMessage(); String testContent = "Test Content"; tm.setContentType(TMContentType.JSON); tm.setContent(testContent); TestMessage testMessage = new TestMessage(); EasyMock.expect(mockObjectMapper.readValue(testContent, TestMessage.class)) .andReturn(testMessage); mockObjectMapper.registerSubtypes(new Class[0]); EasyMock.replay(mockObjectMapper); converter.setMapper(mockObjectMapper); TestMessage res = converter.convertToCommunoteMessage(tm, TestMessage.class); Assert.assertEquals(res, testMessage); }
/** * Method called to locate deserializer for the passed root-level value. */ protected JsonDeserializer<Object> _findRootDeserializer(DeserializationConfig cfg, JavaType valueType) throws JsonMappingException { // Sanity check: must have actual type... if (valueType == null) { throw new JsonMappingException("No value type configured for ObjectReader"); } // First: have we already seen it? JsonDeserializer<Object> deser = _rootDeserializers.get(valueType); if (deser != null) { return deser; } // es-hadoop: findType with 2 args have been removed since 1.9 so this code compiles on 1.8 (which has the fallback method) // es-hadoop: on 1.5 only the 2 args method exists, since 1.9 only the one with 3 args hence the if // Nope: need to ask provider to resolve it deser = _provider.findTypedValueDeserializer(cfg, valueType); if (deser == null) { // can this happen? throw new JsonMappingException("Can not find a deserializer for type " + valueType); } _rootDeserializers.put(valueType, deser); return deser; }
/*********** stdin/namedpipe loop ***********/ void namedpipeLoop() throws JsonGenerationException, JsonMappingException, IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, "UTF-8")); String inputline; BufferedOutputStream out = new BufferedOutputStream( new FileOutputStream(outpipeFilename, true)); // OutputStream out = new FileOutputStream(outpipeFilename, true); log("Waiting for commands on stdin"); while ( (inputline=reader.readLine()) != null) { JsonNode result = parseAndRunCommand(inputline); writeResultToStream(result, out); out.flush(); checkTimings(); } }
public HashMap<String, Object> execute(String operation, String content) throws JsonGenerationException, JsonMappingException, IOException, UserSysException { HashMap<String, Object> res = callOpenAPI(operation, content); //try { //int status = (Integer)res.get("status"); String body = (String)res.get("body"); ObjectMapper mapper = new ObjectMapper(); Object json = mapper.readValue(body, Object.class); res.put("json", json); //} catch (JsonGenerationException ex) { // res.put("exception", ex); //} catch (JsonMappingException ex) { // res.put("exception", ex); //} catch (IOException ex) { // res.put("exception", ex); //} catch (UserSysException ex) { //} return res; }
private static void setOrRemoveOptionalAttributeList(GluuCustomPerson destination, List<?> items, String attributeName) throws JsonGenerationException, JsonMappingException, IOException { if (items == null) { log.trace(" removing " + attributeName); destination.removeAttribute(attributeName); } else { log.trace(" setting " + attributeName); StringWriter listOfItems = new StringWriter(); ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(listOfItems, items); destination.setAttribute(attributeName, listOfItems.toString()); } }
@Override public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { // validate enum class if (enumClass == null) { throw new JsonMappingException("Unable to parse unknown pick-list type"); } final String listValue = jp.getText(); try { // parse the string of the form value1;value2;... final String[] value = listValue.split(";"); final int length = value.length; final Object resultArray = Array.newInstance(enumClass, length); for (int i = 0; i < length; i++) { // use factory method to create object Array.set(resultArray, i, factoryMethod.invoke(null, value[i].trim())); } return resultArray; } catch (Exception e) { throw new JsonParseException("Exception reading multi-select pick list value", jp.getCurrentLocation(), e); } }
public static URI getDocumentURI(URI baseURI, InputStream inStream) throws JsonParseException, JsonMappingException, IOException { final JsonNode rootNode = getRootNode(inStream); URI result = null; if (rootNode.isObject()) { JsonNode okNode = rootNode.findValue("ok"); if (okNode.getBooleanValue()) { JsonNode idNode = rootNode.findValue("id"); //plutte added check if baseURI not already ends with id if (!baseURI.path().endsWith(idNode.getTextValue())) { result = baseURI.appendSegment(idNode.getTextValue()); } else { result = baseURI; } } } return result; }
public static String toJsonString(Object obj, boolean bStripJsReservedWords) throws JsonGenerationException, JsonMappingException, IOException { String strResult = null; if (obj != null) { if (bStripJsReservedWords && obj instanceof JetstreamEvent) obj = ((JetstreamEvent)obj).getFilteredEvent(); try { ObjectMapper mapper = new ObjectMapper(); Writer writer = new StringWriter(); mapper.writeValue(writer, obj); strResult = writer.toString(); } catch (Throwable t) { if (m_nErrorCount++ % 10000 == 0 && LOGGER.isErrorEnabled()) LOGGER.error( "", t); } } return strResult; }
private boolean isDoubleBroadcast(HttpServletRequest request, Object responseEntity) throws JsonGenerationException, JsonMappingException, IOException { String clientId = request.getHeader(HeaderConfig.X_ATMOSPHERE_TRACKING_ID); // return false if the X-Atmosphere-tracking-id is not set if (clientId == null || clientId.isEmpty()) { return false; } CacheEntry entry = ResourceStateChangeListener.getCachedEntries().put( clientId, new CacheEntry(responseEntity)); // there was an existing cached entry, see if its the same if (entry != null) { ObjectMapper mapper = new ObjectMapper(); // cached data final String firedResponse = mapper.writeValueAsString(entry.getData()); // new data final String responseValue = mapper.writeValueAsString(responseEntity); // the same ? return responseValue.equals(firedResponse); } return false; }
@GET @Produces(MediaType.APPLICATION_JSON) public String list() throws JsonGenerationException, JsonMappingException, IOException { this.logger.info("list()"); ObjectWriter viewWriter; if (this.isAdmin()) { viewWriter = this.mapper.writerWithView(JsonViews.Admin.class); } else { viewWriter = this.mapper.writerWithView(JsonViews.User.class); } List<NewsEntry> allEntries = this.newsEntryDao.findAll(); return viewWriter.writeValueAsString(allEntries); }
private void buildCardinalityRangeMaps(String file, HashMap<String, Integer> cardinality, HashMap<String, IntRange> range) throws JsonParseException, JsonMappingException, IOException { List<SchemaAnnotation> saList; if (file == null) { return; // Nothing to do here. } ObjectMapper objectMapper = new ObjectMapper(); saList = objectMapper.readValue(new File(file), new TypeReference<List<SchemaAnnotation>>() { }); for (SchemaAnnotation sa : saList) { String column = sa.getColumn(); if (sa.isRange()) { range.put(column, new IntRange(sa.getRangeStart(), sa.getRangeEnd())); } else { cardinality.put(column, sa.getCardinality()); } } }
/** * Parse file received to generate an instance of quality model. * @param file containing the model in json format * @return QModel imported * @throws IOException * @throws JsonMappingException * @throws JsonParseException */ public static Project parseFile(File file) throws JsonParseException, JsonMappingException, IOException{ Project proj; ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationConfig.Feature.AUTO_DETECT_CREATORS,true); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); mapper.configure(DeserializationConfig.Feature.USE_GETTERS_AS_SETTERS, false); proj = mapper.readValue(file, Project.class); return proj; }
public static AbstractTableConfig init(String jsonString) throws JSONException, JsonParseException, JsonMappingException, JsonProcessingException, IOException { JSONObject o = new JSONObject(jsonString); String tableType = o.getString("tableType").toLowerCase(); String tableName = new TableNameBuilder(TableType.valueOf(tableType.toUpperCase())).forTable(o.getString("tableName")); SegmentsValidationAndRetentionConfig validationConfig = loadSegmentsConfig(new ObjectMapper().readTree(o.getJSONObject("segmentsConfig").toString())); TenantConfig tenantConfig = loadTenantsConfig(new ObjectMapper().readTree(o.getJSONObject("tenants").toString())); TableCustomConfig customConfig = loadCustomConfig(new ObjectMapper().readTree(o.getJSONObject("metadata").toString())); IndexingConfig config = loadIndexingConfig(new ObjectMapper().readTree(o.getJSONObject("tableIndexConfig").toString())); if (tableType.equals("offline")) { return new OfflineTableConfig(tableName, tableType, validationConfig, tenantConfig, customConfig, config); } else if (tableType.equals("realtime")) { return new RealtimeTableConfig(tableName, tableType, validationConfig, tenantConfig, customConfig, config); } throw new UnsupportedOperationException("unknown tableType : " + tableType); }
/** * Parse file received to generate an instance of quality model. * @param file containing the model in json format * @return QModel imported * @throws IOException * @throws JsonMappingException * @throws JsonParseException */ public static QModel parseFile(File file) throws JsonParseException, JsonMappingException, IOException{ QModel qm; ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationConfig.Feature.AUTO_DETECT_CREATORS,true); mapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false); mapper.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true); mapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); mapper.configure(DeserializationConfig.Feature.USE_GETTERS_AS_SETTERS, false); qm = mapper.readValue(file, QModel.class); if (qm!=null && (null == qm.getName() || qm.getName().equals("") )) { qm.setName("Quality Model Imported"+DateTime.now().toDate()); } return qm; }
public static String getSourceNameFromPath(FileSplit fileSplit, Configuration configuration) throws IOException, JsonParseException, JsonMappingException { String content = configuration.get("schema.path.mapping"); Map<String, String> schemaPathMapping = new ObjectMapper().readValue( content, MAP_STRING_STRING_TYPE); LOGGER.info("Schema Path Mapping: {}", schemaPathMapping); String sourceName = null; for (String path : schemaPathMapping.keySet()) { if (fileSplit.getPath().toString().indexOf(path) > -1) { sourceName = schemaPathMapping.get(path); break; } } return sourceName; }
private static void sendMessage() throws IOException, JsonProcessingException, JsonGenerationException, JsonMappingException, UnsupportedEncodingException, HttpException { ObjectMapper mapper = new ObjectMapper(); Map<String, Object> m = new HashMap<String, Object>(); m.put("si", "12345"); m.put("ct", System.currentTimeMillis()); String payload = mapper.writeValueAsString(m); HttpClient client = new HttpClient(); PostMethod method = new PostMethod("http://localhost:8080/tracking/ingest/PulsarRawEvent"); // method.addRequestHeader("Accept-Encoding", "gzip,deflate,sdch"); method.setRequestEntity(new StringRequestEntity(payload, "application/json", "UTF-8")); int status = client.executeMethod(method); System.out.println(Arrays.toString(method.getResponseHeaders())); System.out.println("Status code: " + status + ", Body: " + method.getResponseBodyAsString()); }
@Test public void testFeatureCollection() throws JsonGenerationException, JsonMappingException, IOException { FeatureCollection features = new FeatureCollection(); Map<String, Object> properties = new HashMap<String, Object>(); properties.put("prop1", "value"); properties.put("prop2", 1234); Feature pointFeature = new Feature(new Point(12.34, 56.78, 90.00), properties); features.getFeatures().add(pointFeature); Collection<Collection<Double>> coordinates = new ArrayList<Collection<Double>>(); Collection<Double> point1 = new ArrayList<Double>(); point1.add(1.0); point1.add(2.0); point1.add(3.0); coordinates.add(point1); Collection<Double> point2 = new ArrayList<Double>(); point2.add(1.0); point2.add(2.0); point2.add(3.0); coordinates.add(point2); Feature lineFeature = new Feature(new LineString(coordinates), properties); features.getFeatures().add(lineFeature); String geojson = mapper.writeValueAsString(features); System.out.println(geojson); }