securitysail.java

来自「这是外国一个开源推理机」· Java 代码 · 共 1,997 行 · 第 1/5 页

JAVA
1,997
字号
					case 2 : repos.addStatement(resCR,proRdfType,resSchemaRestr);break;					case 3 : repos.addStatement(resCR,proRdfType,resClassesRestr);break;					case 4 : repos.addStatement(resCR,proRdfType,resInstancesRestr);break;					case 5 : repos.addStatement(resCR,proRdfType,resPropertiesRestr);break;					case 6 : repos.addStatement(resCR,proRdfType,resPaternRestr);break;					case 7 : repos.addStatement(resCR,proRdfType,resQueryRestr);break;					case 8 : repos.addStatement(resCR,proRdfType,resClassesOverSchemaRestr);break;				}				switch (type) {					case 5 : //properties restriction						ArrayList props = new ArrayList(((PropertiesRestriction)r).getProperties());						for ( int i = 0 ; i < props.size() ; i++ ) {							repos.addStatement(resCR,proIncludeProperty,							new URIImpl(((URI)props.get(i)).getURI())) ;						}						break;					case 6 : // PATTERn restriction						PatternRestriction pr = (PatternRestriction) r;						ArrayList su = new ArrayList(pr.getSubjectRestrictions());						for ( int i = 0 ; i < su.size(); i++ ) {							repos.addStatement(resCR,proSubjectRestr,							new URIImpl(							((Restriction)su.get(i)).getUri())							);						}						ArrayList pu = new ArrayList(pr.getPredicateRestrictions());						for ( int i = 0 ; i < pu.size(); i++ ) {							repos.addStatement(resCR, proPredicateRestr,							new URIImpl(							((Restriction)pu.get(i)).getUri()));						}						ArrayList ou = new ArrayList(pr.getObjectRestrictions());						for ( int i = 0 ; i < ou.size(); i++ ) {							repos.addStatement(resCR,proObjectRestr,							new URIImpl(							((Restriction)ou.get(i)).getUri()));						}						break;					case 7 : //QUERY Restriction						ArrayList qList = new ArrayList(((QueryRestriction)r).getQueries());						for ( int i = 0 ; i < qList.size(); i++) {							repos.addStatement(resCR,proRestrOnQuery,new LiteralImpl((String)qList.get(i)));						}						break;				}				if ( r instanceof ResourceRestriction ) {					ArrayList rlist = new ArrayList(((ResourceRestriction)r).getResources());					for ( int i = 0 ; i < rlist.size() ; i++ ) {						repos.addStatement(resCR,proIncludeResource,						new URIImpl(((URI)rlist.get(i)).getURI()));					}				}			} // for restrictions		}		catch(Exception x){			throw new SailInternalException(x);		}	} // exportPolicy()/* * using Jena public void exportSecurityPolicy2RDF(Writer out) { *  * if (!isRepositoryAccessible(Right.ADMIN)) { throw new SailInternalException( * "The current user has no permissions to export the Security Setup."); } * com.hp.hpl.mesa.rdf.jena.model.Model model = new ModelMem(); try { * model.read(KCS_URI); *  * com.hp.hpl.mesa.rdf.jena.model.Property proId = * model.createProperty(BASE_KCS_URI+"id"); * com.hp.hpl.mesa.rdf.jena.model.Property proName = * model.createProperty(BASE_KCS_URI+"name"); * com.hp.hpl.mesa.rdf.jena.model.Property proDescr = * model.createProperty(BASE_KCS_URI+"descritpion"); * com.hp.hpl.mesa.rdf.jena.model.Property proSuperRole = * model.createProperty(BASE_KCS_URI+"superRole"); * com.hp.hpl.mesa.rdf.jena.model.Property proIncludeRule = * model.createProperty(BASE_KCS_URI+"includeRule"); * com.hp.hpl.mesa.rdf.jena.model.Property proRuleRestriction = * model.createProperty(BASE_KCS_URI+"ruleRestriction"); * com.hp.hpl.mesa.rdf.jena.model.Property proRightsGranted = * model.createProperty(BASE_KCS_URI+"rightsGranted"); * com.hp.hpl.mesa.rdf.jena.model.Property proRestrictionType = * model.createProperty(BASE_KCS_URI+"restrictionType"); * com.hp.hpl.mesa.rdf.jena.model.Property proIncludeProperty = * model.createProperty(BASE_KCS_URI+"includeProperty"); *  * com.hp.hpl.mesa.rdf.jena.model.Property proSubjectRestr = * model.createProperty(BASE_KCS_URI+"subjectRestr"); * com.hp.hpl.mesa.rdf.jena.model.Property proPredicateRestr = * model.createProperty(BASE_KCS_URI+"predicateRestr"); * com.hp.hpl.mesa.rdf.jena.model.Property proObjectRestr = * model.createProperty(BASE_KCS_URI+"objectRestr"); *  * com.hp.hpl.mesa.rdf.jena.model.Property proRestrOnQuery = * model.createProperty(BASE_KCS_URI+"restrOnQuery"); *  * com.hp.hpl.mesa.rdf.jena.model.Property proIncludeResource = * model.createProperty(BASE_KCS_URI+"includeResource"); *  * com.hp.hpl.mesa.rdf.jena.model.Property proRdfType = * model.createProperty(RDF.TYPE); *  * com.hp.hpl.mesa.rdf.jena.model.Property proHasRole = * model.createProperty(BASE_KCS_URI+"hasRole"); *  * com.hp.hpl.mesa.rdf.jena.model.Property proHasRule = * model.createProperty(BASE_KCS_URI+"hasRule"); *  *  * com.hp.hpl.mesa.rdf.jena.model.Resource resRole = * model.getResource(BASE_KCS_URI+"Role"); *  * com.hp.hpl.mesa.rdf.jena.model.Resource resRule = * model.getResource(BASE_KCS_URI+"SecurityRule"); *  * com.hp.hpl.mesa.rdf.jena.model.Resource resRepositRestr = * model.getResource(BASE_KCS_URI+"RepositoryRestriction"); *  * com.hp.hpl.mesa.rdf.jena.model.Resource resSchemaRestr = * model.getResource(BASE_KCS_URI+"SchemaRestriction"); *  * com.hp.hpl.mesa.rdf.jena.model.Resource resClassesRestr = * model.getResource(BASE_KCS_URI+"ClassesRestriction"); *  * com.hp.hpl.mesa.rdf.jena.model.Resource resInstancesRestr = * model.getResource(BASE_KCS_URI+"InstancesRestriction"); *  * com.hp.hpl.mesa.rdf.jena.model.Resource resClassesOverSchemaRestr = * model.getResource(BASE_KCS_URI+"ClassesOverSchemaRestriction"); *  * com.hp.hpl.mesa.rdf.jena.model.Resource resPaternRestr = * model.getResource(BASE_KCS_URI+"PatternRestriction"); *  * com.hp.hpl.mesa.rdf.jena.model.Resource resPropertiesRestr = * model.getResource(BASE_KCS_URI+"PropertiesRestriction"); *  * com.hp.hpl.mesa.rdf.jena.model.Resource resQueryRestr = * model.getResource(BASE_KCS_URI+"QueryRestriction"); *  *  * com.hp.hpl.mesa.rdf.jena.model.Resource resUser = * model.getResource(BASE_KCS_URI+"User"); *  * com.hp.hpl.mesa.rdf.jena.model.Resource resRestriction = * model.getResource(BASE_KCS_URI+"Restriction"); *  *  *  * ArrayList userz = new ArrayList(users); ArrayList queList = new ArrayList(); * ArrayList rolz = new ArrayList(roles); Set rulez = new * HashSet(RoleImpl.getRules(roles,false)); *  * for ( int ui = 0; ui <userz.size() ; ui++) { User usr = (User) userz.get(ui); * rulez.addAll(usr.getRules()); com.hp.hpl.mesa.rdf.jena.model.Resource res = * model.createResource(BASE_KCS_URI+"User_"+usr.getId()); * res.addProperty(proRdfType,resUser); res.addProperty(proId,usr.getId()); * ArrayList userRulz = new ArrayList(usr.getRules()); for ( int i = 0 ; i < * userRulz.size() ; i++ ) { *  * res.addProperty(proHasRule, * model.createResource(BASE_KCS_URI+"Rule_"+((Rule)userRulz.get(i)).getName()) ); } * ArrayList userRolz = new ArrayList(usr.getRoles()); for ( int i = 0 ; i < * userRolz.size() ; i++ ) { res.addProperty(proHasRole, * model.createResource(BASE_KCS_URI+"Role_"+((Role)userRolz.get(i)).getName())); } *  } // for userz *  *  * for ( int roi = 0 ; roi < rolz.size(); roi++) { Role role = * (Role)rolz.get(roi); rulez.addAll(role.getRules(true)); *  * com.hp.hpl.mesa.rdf.jena.model.Resource res = * model.createResource(BASE_KCS_URI+"Role_"+role.getName()) * .addProperty(proRdfType,resRole) .addProperty(proId,role.getId()) * .addProperty(proName,role.getName()) * .addProperty(proDescr,role.getDescription()); *  * ArrayList parents = new ArrayList(role.getParentRoles()); *  * for (int i = 0 ; i < parents.size() ; i++ ) { Role pRole = * (Role)parents.get(i); res.addProperty(proSuperRole, * model.createResource(BASE_KCS_URI+"Role_"+pRole.getName())); } // for *  * ArrayList rulzList = new ArrayList(role.getRules(true)); *  * for (int i = 0 ; i < rulzList.size() ; i++ ) { Rule rule = * (Rule)rulzList.get(i); res.addProperty(proIncludeRule, * model.createResource(BASE_KCS_URI+"Rule_"+rule.getName())); } // for } // for * rolz *  *  * ArrayList rulz = new ArrayList(rulez); for (int rui=0; rui <rulz.size(); * rui++) { Rule rule = (Rule)rulz.get(rui); * com.hp.hpl.mesa.rdf.jena.model.Resource res = * model.createResource(BASE_KCS_URI+"Rule_"+rule.getName()); * res.addProperty(proRdfType,resRule) .addProperty(proId,rule.getId()) * .addProperty(proName,rule.getName()) .addProperty(proRuleRestriction, * model.createResource(BASE_KCS_URI+"Restriction_"+rule.getRestriction().getName())); * res.addProperty(proDescr,rule.getDescription()); *  * if (rule.getReadRight()) * res.addProperty(proRightsGranted,Right.READ.toString()); if * (rule.getRemoveRight()) * res.addProperty(proRightsGranted,Right.REMOVE.toString()); if * (rule.getAddRight()) res.addProperty(proRightsGranted,Right.ADD.toString()); * if (rule.getAdminRight()) * res.addProperty(proRightsGranted,Right.ADMIN.toString()); if * (rule.getHistoryRight()) * res.addProperty(proRightsGranted,Right.HISTORY.toString()); } // for rulz *  * ArrayList restrz = new ArrayList(Restriction.getRestrictions()); for (int ri = * 0; ri < restrz.size(); ri++) { Restriction r = (Restriction)restrz.get(ri); * com.hp.hpl.mesa.rdf.jena.model.Resource res = * model.createResource(BASE_KCS_URI+"Restriction_"+r.getName()); *  * res.addProperty(proId,r.getId()); res.addProperty(proName,r.getName()); * res.addProperty(proDescr,r.getDescription()); * res.addProperty(proRestrictionType,Restriction.type2String(r.getType())); int * type = r.getType(); *  * switch (type) { case 1 : res.addProperty(proRdfType,resRepositRestr);break; * case 2 : res.addProperty(proRdfType,resSchemaRestr);break; case 3 : * res.addProperty(proRdfType,resClassesRestr);break; case 4 : * res.addProperty(proRdfType,resInstancesRestr);break; case 5 : * res.addProperty(proRdfType,resPropertiesRestr);break; case 6 : * res.addProperty(proRdfType,resPaternRestr);break; case 7 : * res.addProperty(proRdfType,resQueryRestr);break; case 8 : * res.addProperty(proRdfType,resClassesOverSchemaRestr);break; } *  * switch (type) { case 5 : //properties restriction ArrayList props = new * ArrayList(((PropertiesRestriction)r).getProperties()); for ( int i = 0 ; i < * props.size() ; i++ ) { * res.addProperty(proIncludeProperty,model.createResource( * ((Resource)props.get(i)).getURI())) ; } break; case 6 : // PATTERn * restriction PatternRestriction pr = (PatternRestriction) r; ArrayList su = * new ArrayList(pr.getSubjectRestrictions()); for ( int i = 0 ; i < su.size(); * i++ ) { res.addProperty(proSubjectRestr, model.createResource( * BASE_KCS_URI+"Restriction_"+((Restriction)su.get(i)).getName()) ); } * ArrayList pu = new ArrayList(pr.getPredicateRestrictions()); for ( int i = 0 ; * i < pu.size(); i++ ) { res.addProperty(proPredicateRestr, * model.createResource( * BASE_KCS_URI+"Restriction_"+((Restriction)pu.get(i)).getName())); } ArrayList * ou = new ArrayList(pr.getObjectRestrictions()); for ( int i = 0 ; i < * ou.size(); i++ ) { res.addProperty(proObjectRestr, model.createResource( * BASE_KCS_URI+"Restriction_"+((Restriction)ou.get(i)).getName())); } break; * case 7 : //QUERY Restriction ArrayList qList = new * ArrayList(((QueryRestriction)r).getQueries()); for ( int i = 0 ; i < * qList.size(); i++) { res.addProperty(proRestrOnQuery,(String)qList.get(i)); } * break; } *  * if ( r instanceof ResourceRestriction ) { ArrayList rlist = new * ArrayList(((ResourceRestriction)r).getResources()); for ( int i = 0 ; i < * rlist.size() ; i++ ) { * res.addProperty(proIncludeResource,model.createResource(((Resource)rlist.get(i)).getURI())); } } *  } // for restrictions *  * model.write(new PrintWriter(out), "RDF/XML-ABBREV"); *  *  } catch (com.hp.hpl.mesa.rdf.jena.model.RDFException ex) { throw new * SailInternalException(ex); } *  // add the KCS rdf to the model // export all the setup to triples // export * this part of the repository to a file. } */	/**	 * Imports the security policy given a Graph. 	 * 	 * @param graph the Graph to read the security policy from	 */	public void importPolicy(Graph graph) {				ValueFactory factory = graph.getValueFactory();				URI proId = factory.createURI(BASE_KCS_URI, "id");		URI proName = factory.createURI(BASE_KCS_URI, "name");		URI proDescr = factory.createURI(BASE_KCS_URI, "descritpion");		URI proSuperRole = factory.createURI(BASE_KCS_URI, "superRole");		URI proIncludeRule = factory.createURI(BASE_KCS_URI, "includeRule");		URI proRuleRestriction = factory.createURI(BASE_KCS_URI, "ruleRestriction");		URI proRightsGranted = factory.createURI(BASE_KCS_URI, "rightsGranted");		URI proRestrictionType = factory.createURI(BASE_KCS_URI, "restrictionType");		URI proIncludeProperty = factory.createURI(BASE_KCS_URI, "includeProperty");		URI proSubjectRestr = factory.createURI(BASE_KCS_URI, "subjectRestr");		URI proPredicateRestr = factory.createURI(BASE_KCS_URI, "predicateRestr");		URI proObjectRestr = factory.createURI(BASE_KCS_URI, "objectRestr");		URI proRestrOnQuery = factory.createURI(BASE_KCS_URI, "restrOnQuery");		URI proIncludeResource = factory.createURI(BASE_KCS_URI, "includeResource");		URI resUser = factory.createURI(BASE_KCS_URI, "User");		URI resRole = factory.createURI(BASE_KCS_URI, "Role");		URI resRule = factory.createURI(BASE_KCS_URI, "SecurityRule");		URI resRepositoryRestriction = factory.createURI(BASE_KCS_URI, "RepositoryRestriction");		URI resSchemaRestrction = factory.createURI(BASE_KCS_URI, "SchemaRestrction");		URI resClassesRestriction = factory.createURI(BASE_KCS_URI, "ClassesRestriction");		URI resClassesOverSchemaRestriction = factory.createURI(BASE_KCS_URI, "ClassesOverSchemaRestriction");		URI resInstanceRestriction = factory.createURI(BASE_KCS_URI, "InstancesRestriction");		URI resPropertiesRestriction = factory.createURI(BASE_KCS_URI, "PropertiesRestriction");		URI resPatternRestriction = factory.createURI(BASE_KCS_URI, "PatternRestriction");		URI resQueryRestriction = factory.createURI(BASE_KCS_URI, "QueryRestriction");		URI resRdfType = factory.createURI(RDF.TYPE);		URI resRdfResource = factory.createURI(RDFS.RESOURCE);		URI proHasRole = factory.createURI(BASE_KCS_URI, "hasRole");		URI proHasRule = factory.createURI(BASE_KCS_URI, "hasRule");		//contains uris vs role mapping		Map uriVsRole = new HashMap();		//uris vs rule mapping		Map uriVsRule = new HashMap();		//uris vs restriction mapping		Map uriVsRestr = new HashMap();		ArrayList usersList = new ArrayList();		// iterate users		try {			StatementIterator sti = graph.getStatements(null,resRdfType,resUser);			while ( sti.hasNext() ) {				Resource resCUser = sti.next().getSubject();				int id = Integer.parseInt(						graph.getStatements(resCUser,proId,null).next().getObject().toString());				User user = new UserImpl(id,"","","");				// FIXME cast to URI appropriate?				user.setUri(((URI)resCUser).getURI());				//get rules				StatementIterator rui = graph.getStatements(resCUser,proHasRule,null);				Set rulzSet = new HashSet();				while (rui.hasNext()){					// FIXME cast to URI appropriate?					URI rul = (URI)rui.next().getObject();					Rule rule = (Rule) uriVsRule.get(rul.getURI());					if ( null == rule ) {						rule = new RuleImpl(								Integer.parseInt(graph.getStatements(rul,proId,null)								.next().getObject().toString())								,"","");						uriVsRule.put(rul.getURI(),rule);						rule.setUri(rul.getURI());					}					rulzSet.add(rule);				}				user.setRules(rulzSet );				StatementIterator roi				= graph.getStatements(resCUser,proHasRole,null);				Set rolzSet = new HashSet();				while (roi.hasNext()){					// FIXME cast to URI appropriate?					URI rol = (URI)roi.next().getObject();					Role role = (Role) uriVsRule.get(rol.getURI());					if ( null == role ) {						role = new RoleImpl(								Integer.parseInt(graph.getStatements(rol,proId,null).next().								getObject().toString())								,"","");						role.setUri(rol.getURI());						uriVsRole.put(rol.getURI(),role);					}					rolzSet.add(role);				}				user.setRoles(rolzSet );				usersList.add(user);				usersById.put(new Integer(user.getId()),user);				users.add(user);			} // user		} catch (Exception x) {			throw new SailInternalException(x);		}		// iterate roles		try {			StatementIterator sti = graph.getStatements(null,resRdfType,resRole);			while ( sti.hasNext() ) {				Resource res

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?