/** * Converts a string based request to a RequestCtx obejct. * * @param request * the string request * @return the RequestCtx object * @throws MelcoeXacmlException */ public RequestCtx makeRequestCtx(String request) throws MelcoeXacmlException { RequestCtx reqCtx = null; try { ByteArrayInputStream is = new ByteArrayInputStream(request.getBytes()); reqCtx = BasicRequestCtx.getInstance(is); } catch (ParsingException pe) { throw new MelcoeXacmlException("Error parsing response.", pe); } return reqCtx; }
@Override public void encode(OutputStream output, Indenter indenter) { encode(output, indenter, null); }
new BasicRequestCtx(subjects, resources, actions,
@Test public void testRoundtrip() throws ParsingException { ByteArrayOutputStream buf = new ByteArrayOutputStream(); test.encode(buf); ByteArrayInputStream encoded = new ByteArrayInputStream(buf.toByteArray()); RequestCtx actual = BasicRequestCtx.getInstance(encoded); assertEquals(toString(test), toString(actual)); assertTrue(test.equals(actual)); }
List attributes = parseAttributes(node); newResource = parseAttributes(node); } else if (tag.equals("Action")) { newAction = parseAttributes(node); } else if (tag.equals("Environment")) { newEnvironment = parseAttributes(node); return new BasicRequestCtx(newSubjects, newResource, newAction, newEnvironment, root);
@Test public void testEquals() throws ParsingException { List<Subject> subjects = Collections.singletonList(fixtureSubject); List<Attribute> action = Collections.emptyList(); List<Attribute> environment = Collections.emptyList(); List<Attribute> resource = Arrays.asList(new Attribute[]{ AbstractPolicyEnforcementPoint.RESOURCE_ATTRIBUTE, new SingletonAttribute(Constants.OBJECT.PID.attributeId, null, null, new StringAttribute("foo:bar")) }); RequestCtx actual = new BasicRequestCtx(subjects, resource, action, environment); assertFalse(test.equals(actual)); resource = Arrays.asList(new Attribute[]{ AbstractPolicyEnforcementPoint.RESOURCE_ATTRIBUTE, new SingletonAttribute(Constants.OBJECT.PID.attributeId, null, null, new StringAttribute("lol:wut")) }); actual = new BasicRequestCtx(subjects, resource, action, environment); assertTrue(test.equals(actual)); }
while (it.hasNext()) { Subject subject = (Subject)(it.next()); encodeSubject(subject, out, indenter); out.append(indenter.makeString()).append("<ResourceContent>") .append(resourceContent).println("</ResourceContent>"); encodeAttributes(resource, out, indenter); out.print(indent); out.println("</Resource>"); out.print(indent); out.println("<Action>"); encodeAttributes(action, out, indenter); out.print(indent); out.println("</Action>"); encodeAttributes(environment, out, indenter);
/** * Private helper function to encode the subjects */ private void encodeSubject(Subject subject, PrintStream out, Indenter indenter) { char [] indent = indenter.makeString().toCharArray(); out.print(indent); out.append("<Subject SubjectCategory=\"") .append(subject.getCategory().toString()).append('"'); List subjectAttrs = subject.getAttributesAsList(); if (subjectAttrs.size() == 0) { // there's nothing in this Subject, so just close the tag out.println("/>"); } else { // there's content, so fill it in out.println('>'); encodeAttributes(subjectAttrs, out, indenter); out.print(indent); out.println("</Subject>"); } }
@Before public void setUp() throws ParsingException{ fixtureSubject = new Subject(Collections.emptyList()); List<Subject> subjects = Collections.singletonList(fixtureSubject); List<Attribute> action = Collections.emptyList(); List<Attribute> environment = Collections.emptyList(); List<Attribute> resource = Arrays.asList(new Attribute[]{ AbstractPolicyEnforcementPoint.RESOURCE_ATTRIBUTE, new SingletonAttribute(Constants.OBJECT.PID.attributeId, null, null, new StringAttribute("lol:wut")) }); test = new BasicRequestCtx(subjects, resource, action, environment); }
List attributes = parseAttributes(node); newResource = parseAttributes(node); } else if (tag.equals("Action")) { newAction = parseAttributes(node); } else if (tag.equals("Environment")) { newEnvironment = parseAttributes(node); return new BasicRequestCtx(newSubjects, newResource, newAction, newEnvironment, root);
while (it.hasNext()) { Subject subject = (Subject)(it.next()); encodeSubject(subject, out, indenter); out.append(indenter.makeString()).append("<ResourceContent>") .append(resourceContent).println("</ResourceContent>"); encodeAttributes(resource, out, indenter); out.print(indent); out.println("</Resource>"); out.print(indent); out.println("<Action>"); encodeAttributes(action, out, indenter); out.print(indent); out.println("</Action>"); encodeAttributes(environment, out, indenter);
/** * Private helper function to encode the subjects */ private void encodeSubject(Subject subject, PrintStream out, Indenter indenter) { char [] indent = indenter.makeString().toCharArray(); out.print(indent); out.append("<Subject SubjectCategory=\"") .append(subject.getCategory().toString()).append('"'); List subjectAttrs = subject.getAttributesAsList(); if (subjectAttrs.size() == 0) { // there's nothing in this Subject, so just close the tag out.println("/>"); } else { // there's content, so fill it in out.println('>'); encodeAttributes(subjectAttrs, out, indenter); out.print(indent); out.println("</Subject>"); } }
new BasicRequestCtx(subjects, resources, actions,
/** * Creates a new <code>RequestCtx</code> by parsing XML from an * input stream. Note that this a convenience method, and it will * not do schema validation by default. You should be parsing the data * yourself, and then providing the root node to the other * <code>getInstance</code> method. If you use this convenience * method, you probably want to turn on validation by setting the * context schema file (see the programmer guide for more information * on this). * * @param input a stream providing the XML data * * @return a new <code>RequestCtx</code> * * @throws ParsingException if there is an error parsing the input */ public static RequestCtx getInstance(InputStream input) throws ParsingException { return getInstance(InputParser.parseInput(input, "Request")); }
@Override public void encode(OutputStream output, Indenter indenter) { encode(output, indenter, null); }
@Before public void setUp() throws ParsingException{ List<Subject> subjects = Collections.singletonList(mockSubject); List<Attribute> action = Collections.emptyList(); List<Attribute> environment = Collections.emptyList(); List<Attribute> resource = Arrays.asList(new Attribute[]{ AbstractPolicyEnforcementPoint.RESOURCE_ATTRIBUTE, new SingletonAttribute(Constants.OBJECT.PID.attributeId, null, null, new StringAttribute("lol:wut")) }); RequestCtx request = new BasicRequestCtx(subjects, resource, action, environment); test = new BasicEvaluationCtx(request, mockFinder, mockContext); }
/** * Creates a new <code>RequestCtx</code> by parsing XML from an * input stream. Note that this a convenience method, and it will * not do schema validation by default. You should be parsing the data * yourself, and then providing the root node to the other * <code>getInstance</code> method. If you use this convenience * method, you probably want to turn on validation by setting the * context schema file (see the programmer guide for more information * on this). * * @param input a stream providing the XML data * * @return a new <code>RequestCtx</code> * * @throws ParsingException if there is an error parsing the input */ public static RequestCtx getInstance(InputStream input) throws ParsingException { return getInstance(InputParser.parseInput(input, "Request")); }
@Override public void encode(OutputStream output, String nsURI) { encode(output, new Indenter(0), nsURI); }
new BasicRequestCtx(setupSubjects(subjects), setupResources(resources, relationshipResolver), setupAction(actions),
@Override public String evaluate(String request) throws EvaluationException { logger.debug("evaluating request: {}", request); RequestCtx req = null; ByteArrayInputStream is = new ByteArrayInputStream(request.getBytes()); try { req = BasicRequestCtx.getInstance(is); } catch (ParsingException pe) { logger.error("Error parsing request:\n" + request, pe); throw new EvaluationException("Error parsing request:\n" + request); } ResponseCtx res = evaluate(req); ByteArrayOutputStream os = new ByteArrayOutputStream(); res.encode(os, new Indenter()); if (logger.isDebugEnabled()) { logger.debug("response is: {}", os.toString()); } return os.toString(); }