List of usage examples for com.fasterxml.jackson.core JsonLocation getLineNr
public int getLineNr()
From source file:org.apache.openaz.xacml.std.json.JSONRequest.java
/** * Read characters from the given <code>InputStream</code> and parse them into an XACML * {@link org.apache.openaz.xacml.api.Request} object. * * @param is//from w w w . j a va 2 s . co m * @return * @throws JSONStructureException */ public static Request load(InputStream is) throws JSONStructureException { // TODO - ASSUME that order of members within an object does not matter (Different from XML, in JSON // everything is handled as Maps so order does not matter) // ensure shorthand map is set up if (shorthandMap == null) { initShorthandMap(); } // ensure that we have an instance of the DataTypeFactory for generating AttributeValues by DataType if (dataTypeFactory == null) { try { dataTypeFactory = DataTypeFactory.newInstance(); if (dataTypeFactory == null) { throw new NullPointerException("No DataTypeFactory found"); } } catch (FactoryException e) { throw new JSONStructureException("Unable to find DataTypeFactory, e=" + e); } } // create a new Request object to be filled in StdMutableRequest stdMutableRequest = null; String json = null; ObjectMapper mapper = null; try { // read the inputStream into a buffer (trick found online scans entire input looking for // end-of-file) java.util.Scanner scanner = new java.util.Scanner(is); scanner.useDelimiter("\\A"); json = scanner.hasNext() ? scanner.next() : ""; scanner.close(); mapper = new ObjectMapper().setVisibility(PropertyAccessor.FIELD, Visibility.ANY); // TODO - ASSUME that any duplicated component is a bad thing (probably indicating an error in the // incoming JSON) mapper.configure(JsonParser.Feature.STRICT_DUPLICATE_DETECTION, true); Map<?, ?> root = mapper.readValue(json, Map.class); // // Does the request exist? // Map<?, ?> jsonRequestMap = (Map<?, ?>) root.remove("Request"); if (jsonRequestMap == null) { throw new JSONStructureException("No \"Request\" property found."); } checkUnknown("Top-level message", root); stdMutableRequest = new StdMutableRequest(); // // Is there a Category? // Object categoryList = jsonRequestMap.remove("Category"); if (categoryList != null && !(categoryList instanceof List)) { throw new JSONStructureException( "Category must contain list of objects, not '" + categoryList.getClass() + "'"); } if (categoryList != null) { // // Iterate each Category // Iterator<?> iter = ((List<?>) categoryList).iterator(); while (iter.hasNext()) { Object category = iter.next(); if (!(category instanceof Map)) { throw new JSONStructureException( "Category list must contain objects contained within curly braces ({})"); } parseCategory((Map<?, ?>) category, "Category", null, stdMutableRequest); } } // The following may be either a single instance or an array. This allows multiple decisions to // work with the Default Category objects. // Example: // "AccessSubject" : [ {attributes group one}, // {attributes group two} // ] // // Look for default Shorthand AccessSubject // parseDefaultCategory(jsonRequestMap, "AccessSubject", "urn:oasis:names:tc:xacml:1.0:subject-category:access-subject", stdMutableRequest); // // Provide backward compatibility for our PEP's // parseDefaultCategory(jsonRequestMap, "Subject", "urn:oasis:names:tc:xacml:1.0:subject-category:access-subject", stdMutableRequest); // // Look for default Shorthand Action // parseDefaultCategory(jsonRequestMap, "Action", "urn:oasis:names:tc:xacml:3.0:attribute-category:action", stdMutableRequest); // // Look for default Shorthand Resource // parseDefaultCategory(jsonRequestMap, "Resource", "urn:oasis:names:tc:xacml:3.0:attribute-category:resource", stdMutableRequest); // // Look for default Shorthand Environment // parseDefaultCategory(jsonRequestMap, "Environment", "urn:oasis:names:tc:xacml:3.0:attribute-category:environment", stdMutableRequest); // // Look for default Shorthand RecipientSubject // parseDefaultCategory(jsonRequestMap, "RecipientSubject", "urn:oasis:names:tc:xacml:1.0:subject-category:recipient-subject", stdMutableRequest); // // Look for default Shorthand IntermediarySubject // parseDefaultCategory(jsonRequestMap, "IntermediarySubject", "urn:oasis:names:tc:xacml:1.0:subject-category:intermediary-subject", stdMutableRequest); // // Look for default Shorthand Codebase // parseDefaultCategory(jsonRequestMap, "Codebase", "urn:oasis:names:tc:xacml:1.0:subject-category:codebase", stdMutableRequest); // // Look for default Shorthand RequestingMachine // parseDefaultCategory(jsonRequestMap, "RequestingMachine", "urn:oasis:names:tc:xacml:1.0:subject-category:requesting-machine", stdMutableRequest); // // MultiRequest // Map<?, ?> multiRequests = (Map<?, ?>) jsonRequestMap.remove("MultiRequests"); if (multiRequests != null) { if (!(multiRequests instanceof Map)) { throw new JSONStructureException("MultiRequests must be object structure, not single value"); } List<?> requestReferenceList = (List<?>) multiRequests.remove("RequestReference"); if (requestReferenceList == null) { throw new JSONStructureException("MultiRequest must contain a RequestReference element"); } if (requestReferenceList.size() < 1) { throw new JSONStructureException( "MultiRequest must contain at least one element in the RequestReference list"); } checkUnknown("MultiRequest", multiRequests); for (Object requestReferenceMapObject : requestReferenceList) { if (!(requestReferenceMapObject instanceof Map)) { throw new JSONStructureException("MultiRequest RequestReference must be object"); } Map<?, ?> requestReferenceMap = (Map<?, ?>) requestReferenceMapObject; // each object within the list must contain a ReferenceId and only a ReferenceId Object referenceIdListObject = requestReferenceMap.remove("ReferenceId"); if (referenceIdListObject == null) { throw new JSONStructureException( "MultiRequest RequestReference list element must contain ReferenceId"); } List<?> referenceIdList = (List<?>) referenceIdListObject; if (referenceIdList.size() == 0) { // the spec does not disallow empty list RequestReference objects continue; } checkUnknown("RequestReference", requestReferenceMap); // create reference corresponding to RequestReference list element StdMutableRequestReference requestReference = new StdMutableRequestReference(); for (Object referenceId : referenceIdList) { // add attributes to the reference // Since the order of the JSON is not constrained, we could process this section // before the section containing attribute being referenced, // so we cannot do a cross-check here to verify that the attribute reference exists. // That will happen later when the PDP attempts to find the attribute. StdRequestAttributesReference requestAttributesReference = new StdRequestAttributesReference( (String) referenceId); requestReference.add(requestAttributesReference); } stdMutableRequest.add(requestReference); } } // // ReturnPolicyIdList // // If omitted this is set to a default of false by the StdMutableRequest constructor. // Object returnPolicyIdList = jsonRequestMap.remove("ReturnPolicyIdList"); Boolean returnPolicyIdListBoolean = makeBoolean(returnPolicyIdList, "ReturnPolicyIdList"); if (returnPolicyIdList != null) { stdMutableRequest.setReturnPolicyIdList(returnPolicyIdListBoolean); } // // CombinedDecision // // If omitted this is set to a default of false by the StdMutableRequest constructor. // Object combinedDecision = jsonRequestMap.remove("CombinedDecision"); Boolean combinedDecisionBoolean = makeBoolean(combinedDecision, "CombinedDecision"); if (combinedDecision != null) { stdMutableRequest.setCombinedDecision(combinedDecisionBoolean); } // // XPath // // The JSON spec says that this has a default value, implying that if it is missing in the Request // we should fill it in. // However the XML (DOM) version does not do that. If the value is missing it leaves the // requestDefaults object blank. // We are following the XML approach and ignoring the Default value for this field in the spec. // TODO - Assume that no value for XPathVersion means "leave as null", not "fill in the default // value from spec. This violates the JSON spec Object xPath = jsonRequestMap.remove("XPathVersion"); if (xPath != null) { // XPath is given in the JSON input if (!(xPath instanceof String)) { throw new JSONStructureException("XPathVersion not a URI passed as a String"); } URI xPathUri = null; try { xPathUri = new URI(xPath.toString()); } catch (Exception e) { throw new JSONStructureException("XPathVersion not a valid URI: '" + xPath + "'", e); } StdRequestDefaults requestDefaults = new StdRequestDefaults(xPathUri); stdMutableRequest.setRequestDefaults(requestDefaults); } checkUnknown("Request", jsonRequestMap); } catch (JsonParseException e) { // try to point to problem area in JSON input, if possible JsonLocation location = e.getLocation(); String locationOfError = "(unavailable)"; if (location != null && location != JsonLocation.NA) { String jsonText = json; if (location.getLineNr() > 1) { String[] jsonArray = jsonText.split("\\r?\\n|\\r"); jsonText = jsonArray[location.getLineNr()]; } if (location.getCharOffset() < jsonText.length()) { if (location.getCharOffset() > 0) { locationOfError = jsonText.substring((int) location.getCharOffset() - 1); } if (locationOfError.length() > 30) { locationOfError = locationOfError.substring(0, 30); } } } throw new JSONStructureException("Unable to parse JSON starting at text'" + locationOfError + "', input was '" + json + "', exception: " + e, e); } catch (JsonMappingException e) { throw new JSONStructureException("Unable to map JSON '" + json + "', exception: " + e, e); } catch (IOException e) { throw new JSONStructureException("Unable to read JSON input, exception: " + e, e); } // all done return new StdRequest(stdMutableRequest); }
From source file:org.eclipse.rdf4j.rio.rdfjson.RDFJSONParser.java
/** * Creates a literal, using the current value, language, and datatype, and additionally using the given * {@link JsonLocation} to provide information about the line and column numbers in the event of a * warning, error or exception being generated by the creation of the literal. * //w w w. ja v a2 s .c om * @param label * the literal's lexical label * @param language * the literal's language tag. Can be null. * @param datatype * the literal's datatype. Can be null. * @param currentLocation * the current JsonLocation. May not be null. * @return the created {@link Literal} object. * @throws RDFParseException */ protected Literal createLiteral(String label, String language, IRI datatype, JsonLocation currentLocation) throws RDFParseException { return createLiteral(label, language, datatype, currentLocation.getLineNr(), currentLocation.getColumnNr()); }
From source file:org.eclipse.rdf4j.rio.rdfjson.RDFJSONParser.java
protected void reportError(String msg, Throwable e, JsonLocation location, RioSetting<Boolean> setting) throws RDFParseException { reportError(msg, location.getLineNr(), location.getColumnNr(), setting); }
From source file:org.eclipse.rdf4j.rio.rdfjson.RDFJSONParser.java
protected void reportError(String msg, JsonLocation location, RioSetting<Boolean> setting) throws RDFParseException { reportError(msg, location.getLineNr(), location.getColumnNr(), setting); }
From source file:org.eclipse.rdf4j.rio.rdfjson.RDFJSONParser.java
protected void reportFatalError(String msg, Throwable e, JsonLocation location) throws RDFParseException { reportFatalError(msg, location.getLineNr(), location.getColumnNr()); }
From source file:org.eclipse.rdf4j.rio.rdfjson.RDFJSONParser.java
protected void reportFatalError(String msg, JsonLocation location) throws RDFParseException { reportFatalError(msg, location.getLineNr(), location.getColumnNr()); }