Java tutorial
/******************************************************************************* * Copyright (c) 2011 - 2012 TXT e-solutions SpA * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * * This work was performed within the IoT_at_Work Project * and partially funded by the European Commission's * 7th Framework Programme under the research area ICT-2009.1.3 * Internet of Things and enterprise environments. * * * Authors: * Gaetano Scigliuto (TXT e-solutions SpA) * * Contributors: * Domenico Rotondi (TXT e-solutions SpA) *******************************************************************************/ package IoTatWork; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.net.URL; import java.util.List; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import org.restlet.ext.json.JsonRepresentation; import org.restlet.representation.Representation; import org.restlet.resource.Get; import org.restlet.resource.ResourceException; import org.restlet.resource.ServerResource; import org.w3c.dom.Document; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import com.google.gson.JsonObject; import com.orientechnologies.orient.core.db.graph.OGraphDatabase; import com.orientechnologies.orient.core.id.ORID; import com.orientechnologies.orient.core.record.ORecordInternal; import com.orientechnologies.orient.core.record.impl.ODocument; import com.orientechnologies.orient.core.sql.query.OSQLSynchQuery; import com.tinkerpop.blueprints.Direction; import com.tinkerpop.blueprints.Edge; import com.tinkerpop.blueprints.Vertex; import com.tinkerpop.blueprints.impls.orient.OrientGraph; public class AuCapTreeResource extends ServerResource { private DataMapper dataMapper; private String dbUrl; private String usr; private String pwd; private int depth; private OrientGraph graph; private static final String ROOT_NAME = "auCapDBRoot"; @Override protected void doInit() throws ResourceException { depth = 2; dbUrl = System.getProperty("capabilitiesDatabaseUrl"); usr = System.getProperty("capabilitiesDatabaseUsername"); pwd = System.getProperty("capabilitiesDatabasePassword"); /* try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); String path = System.getProperty("filePropertiesPath"); Document doc = docBuilder.parse (new File(path)); Node dbNode = doc.getElementsByTagName("DBConnection").item(0); //Node dbConnNode = dbNode.getChildNodes().item(0); Node dbConnNode = doc.getElementsByTagName("db").item(0); dbUrl = dbConnNode.getTextContent(); //Node rootNameNode = dbNode.getChildNodes().item(1); Node rootNameNode = doc.getElementsByTagName("root").item(0); rootName = rootNameNode.getTextContent(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } */ } @Get public Representation get() { System.out.println(this.dbUrl); graph = new OrientGraph(this.dbUrl); JsonRepresentation jsonReturn = null; Map<String, String> attributes = getQuery().getValuesMap(); System.out.println(attributes); if (attributes.containsKey("depth")) { depth = Integer.parseInt(attributes.get("depth")); } String auCapRid = null; if (attributes.containsKey("id")) { System.out.println("#" + attributes.get("id")); if (checkExistNode("#" + attributes.get("id"))) { auCapRid = "#" + attributes.get("id"); } } else { auCapRid = getRootRid(); } if (auCapRid != null) { JSONObject jsonNodeTree = new JSONObject(); Vertex auCapNode = graph.getVertex(auCapRid); Object revokedSince = checkParentsRevocation(auCapNode); if (revokedSince != null) { getJsonNodeTree(auCapNode, jsonNodeTree, 0, true, revokedSince); } else { getJsonNodeTree(auCapNode, jsonNodeTree, 0, false, null); } System.out.println(jsonNodeTree.toString()); jsonReturn = new JsonRepresentation(jsonNodeTree); } graph.shutdown(); return jsonReturn; } /** * * @param auCapNode * @return */ private Object checkParentsRevocation(Vertex auCapNode) { Object revokedSince = null; boolean revoked = false; while (auCapNode.getEdges(Direction.IN, DataMapper.DEPENDENT_CAPABILIES_LABEL).iterator().hasNext() && !revoked) { Vertex parentNode = auCapNode.getEdges(Direction.IN, DataMapper.DEPENDENT_CAPABILIES_LABEL).iterator() .next().getVertex(Direction.OUT); if (parentNode.getProperty(dataMapper.STATUS).equals(Status.REVOKED)) { String revocationScope = parentNode.getProperty(dataMapper.REVOCATION_SCOPE).toString(); if (revocationScope.equals(RevocationScope.ALL) || revocationScope.equals(RevocationScope.DESCENDANT)) { revoked = true; revokedSince = parentNode.getProperty(DataMapper.DESCENDANT_REVOKED_SINCE); } } auCapNode = parentNode; } return revokedSince; } /** * * @param auCapRid * @param depth * @param jsonNodeTree * @return */ private void getJsonNodeTree(Vertex auCapNode, JSONObject jsonNodeTree, int depth, boolean toRevoke, Object revokedSince) { //graph.getRawGraph(); try { OGraphDatabase rawGraph = graph.getRawGraph(); ODocument doc = rawGraph.load((ORID) auCapNode.getId()); //System.out.println(doc.getClassName()); if (doc.getClassName().equals(DataMapper.AUCAP_VERTEX)) { getJsonAuCapVertexData(auCapNode, jsonNodeTree); JSONObject jsonData = (JSONObject) jsonNodeTree.get("data"); if (!toRevoke) { //controllo se lui o i figli sono sa revocare String status = (String) auCapNode.getProperty(DataMapper.STATUS); if (status.equals(Status.REVOKED)) { String revocationScope = (String) auCapNode.getProperty(DataMapper.REVOCATION_SCOPE); if (revocationScope.equals(RevocationScope.ALL)) { jsonData.put(DataMapper.STATUS, Status.REVOKED); jsonData.put(DataMapper.REVOKED_SINCE, auCapNode.getProperty(DataMapper.REVOKED_SINCE)); toRevoke = true; revokedSince = auCapNode.getProperty(DataMapper.DESCENDANT_REVOKED_SINCE); } else if (revocationScope.equals(RevocationScope.THIS)) { jsonData.put(DataMapper.STATUS, Status.REVOKED); jsonData.put(DataMapper.REVOKED_SINCE, auCapNode.getProperty(DataMapper.REVOKED_SINCE)); } else if (revocationScope.equals(RevocationScope.DESCENDANT)) { jsonData.put(DataMapper.STATUS, Status.VALID); toRevoke = true; revokedSince = auCapNode.getProperty(DataMapper.DESCENDANT_REVOKED_SINCE); } } } else { //metto lo status a Revoked e metto il campo revokedSince jsonData.put(DataMapper.REVOKED_SINCE, revokedSince); jsonData.put(DataMapper.STATUS, Status.REVOKED); } Iterable<Edge> dependentEdges = auCapNode.getEdges(Direction.OUT, DataMapper.DEPENDENT_CAPABILIES_LABEL); JSONArray jsonArrayDependant = new JSONArray(); depth++; for (Edge depend : dependentEdges) { if (depth <= this.depth) { JSONObject jsonDependantNode = new JSONObject(); jsonArrayDependant.put(jsonDependantNode); Vertex dependantVertex = depend.getVertex(Direction.IN); getJsonNodeTree(dependantVertex, jsonDependantNode, depth, toRevoke, revokedSince); } } jsonNodeTree.put("children", jsonArrayDependant); } else { jsonNodeTree.put("id", auCapNode.getId().toString().substring(1)); jsonNodeTree.put("name", auCapNode.getProperty("name").toString()); Iterable<Edge> dependentEdges = auCapNode.getEdges(Direction.OUT); JSONObject jsonData = new JSONObject(); String edgeLabel = dependentEdges.iterator().next().getLabel(); jsonData.put("edgeLabel", edgeLabel); jsonNodeTree.put("data", jsonData); JSONArray jsonArrayDependant = new JSONArray(); depth++; for (Edge depend : dependentEdges) { if (depth <= this.depth) { JSONObject jsonDependantNode = new JSONObject(); jsonArrayDependant.put(jsonDependantNode); Vertex dependantVertex = depend.getVertex(Direction.IN); getJsonNodeTree(dependantVertex, jsonDependantNode, depth, false, null); } } jsonNodeTree.put("children", jsonArrayDependant); } } catch (JSONException e) { e.printStackTrace(); } } /** * * @param auCapNode * @param jsonNodeTree * @return */ private void getJsonAuCapVertexData(Vertex auCapNode, JSONObject jsonNodeTree) { try { Vertex detailsVertex = auCapNode.getEdges(Direction.OUT, DataMapper.DETAILS_LABEL).iterator().next() .getVertex(Direction.IN); jsonNodeTree.put("id", auCapNode.getId().toString().substring(1)); jsonNodeTree.put("name", detailsVertex.getProperty(DataMapper.SUBJECT_ID).toString()); JSONObject jsonVertexData = new JSONObject(); jsonVertexData.put("edgeLabel", DataMapper.DEPENDENT_CAPABILIES_LABEL); //AuCapVertex jsonVertexData.put(DataMapper.CAPABILITY_ID, auCapNode.getProperty(DataMapper.CAPABILITY_ID)); jsonVertexData.put(DataMapper.CAPABILITY_ISSUER, auCapNode.getProperty(DataMapper.CAPABILITY_ISSUER)); jsonVertexData.put(DataMapper.CAPABILITY_ISSUE_TIME, auCapNode.getProperty(DataMapper.CAPABILITY_ISSUE_TIME)); String status = (String) auCapNode.getProperty(DataMapper.STATUS); jsonVertexData.put(DataMapper.STATUS, status); /* if(status.equals(Status.REVOKED)){ String revocationScope = (String) auCapNode.getProperty(DataMapper.REVOCATION_SCOPE); jsonVertexData.put(DataMapper.REVOCATION_SCOPE, revocationScope); if(revocationScope.equals(RevocationScope.ALL)){ jsonVertexData.put(DataMapper.REVOKED_SINCE, auCapNode.getProperty(DataMapper.REVOKED_SINCE)); jsonVertexData.put(DataMapper.DESCENDANT_REVOKED_SINCE, auCapNode.getProperty(DataMapper.DESCENDANT_REVOKED_SINCE)); }else if(revocationScope.equals(RevocationScope.THIS)){ jsonVertexData.put(DataMapper.REVOKED_SINCE, auCapNode.getProperty(DataMapper.REVOKED_SINCE)); }else if(revocationScope.equals(RevocationScope.DESCENDANT)){ jsonVertexData.put(DataMapper.DESCENDANT_REVOKED_SINCE, auCapNode.getProperty(DataMapper.DESCENDANT_REVOKED_SINCE)); } } */ jsonVertexData.put(DataMapper.VALIDITY_START_TIME, auCapNode.getProperty(DataMapper.VALIDITY_START_TIME)); jsonVertexData.put(DataMapper.VALIDITY_END_TIME, auCapNode.getProperty(DataMapper.VALIDITY_END_TIME)); //AuCapDetails jsonVertexData.put(DataMapper.SUBJECT_ID, detailsVertex.getProperty(DataMapper.SUBJECT_ID)); jsonVertexData.put(DataMapper.RESOURCE_ID, detailsVertex.getProperty(DataMapper.RESOURCE_ID)); jsonVertexData.put(DataMapper.REVOCATION_SERVICE_URLS, detailsVertex.getProperty(DataMapper.REVOCATION_SERVICE_URLS)); if (!auCapNode.getEdges(Direction.OUT, DataMapper.DEPENDENT_CAPABILIES_LABEL).iterator().hasNext()) { jsonVertexData.put("leaf", "leaf"); } // for(String prop : auCapNode.getPropertyKeys()){ // jsonVertexData.put(prop, auCapNode.getProperty(prop)); // } // for(String prop : detailsVertex.getPropertyKeys()){ // if(!prop.equals(DataMapper.CAPABILITY_TOKEN)) // jsonVertexData.put(prop, detailsVertex.getProperty(prop)); // } JSONArray jsonArrayAccessRights = new JSONArray(); for (Edge accessRightEdge : detailsVertex.getEdges(Direction.OUT, DataMapper.ACCESS_RIGHTS_LABEL)) { Vertex accesRightVertex = accessRightEdge.getVertex(Direction.IN); JSONObject jsonAccessRight = new JSONObject(); for (String prop : accesRightVertex.getPropertyKeys()) { jsonAccessRight.put(prop, accesRightVertex.getProperty(prop)); } jsonArrayAccessRights.put(jsonAccessRight); } jsonVertexData.put(DataMapper.ACCESS_RIGHTS_LABEL, jsonArrayAccessRights); jsonNodeTree.put("data", jsonVertexData); } catch (JSONException e) { e.printStackTrace(); } } /** * * @param string * @return */ private boolean checkExistNode(String rid) { OrientGraph graph = new OrientGraph(this.dbUrl); String query = "select * from OGraphVertex where @rid = ?"; List<ODocument> queryResult = graph.getRawGraph().query(new OSQLSynchQuery<ODocument>(query), rid); boolean exist = false; if (queryResult.size() > 0) { exist = true; } graph.shutdown(); return exist; } /** * * @return */ private String getRootRid() { OrientGraph graph = new OrientGraph(this.dbUrl); String query = "select * from OGraphVertex where name = ?"; List<ODocument> queryResult = graph.getRawGraph().query(new OSQLSynchQuery<ODocument>(query), ROOT_NAME); String rootRID = null; if (queryResult.size() > 0) { ODocument rootNode = queryResult.get(0); rootRID = rootNode.getIdentity().toString(); } graph.shutdown(); return rootRID; } }