Created
May 10, 2011 07:19
-
-
Save nacx/964033 to your computer and use it in GitHub Desktop.
Wink Resource Parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.abiquo; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.URL; | |
import java.net.URLConnection; | |
import java.util.HashSet; | |
import java.util.Set; | |
import javax.xml.parsers.DocumentBuilder; | |
import javax.xml.parsers.DocumentBuilderFactory; | |
import javax.xml.parsers.ParserConfigurationException; | |
import org.w3c.dom.Document; | |
import org.w3c.dom.Node; | |
import org.w3c.dom.NodeList; | |
import org.xml.sax.SAXException; | |
public class WinkResourceParser | |
{ | |
private static final String INTERCEPT_TEMPLATE = | |
"<security:intercept-url pattern=\"%s\" method=\"%s\" access=\"\" />"; | |
public static void main(final String[] args) throws Exception | |
{ | |
String uri = "http://10.60.21.206/api/console?doc=resources"; | |
String credentials = "YWRtaW46eGFiaXF1bw=="; // Base64(user:pass) | |
WinkResourceParser parser = new WinkResourceParser(uri, credentials); | |
URLConnection conn = parser.connect(); | |
parser.parse(conn.getInputStream()); | |
} | |
private String uri; | |
private String credentials; | |
public WinkResourceParser(final String uri, final String credentials) | |
{ | |
super(); | |
this.uri = uri; | |
this.credentials = credentials; | |
} | |
public URLConnection connect() throws IOException | |
{ | |
URL url = new URL(uri); | |
URLConnection conn = url.openConnection(); | |
conn.setRequestProperty("Authorization", "Basic " + credentials); | |
return conn; | |
} | |
public void parse(final InputStream in) throws ParserConfigurationException, SAXException, | |
IOException | |
{ | |
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | |
DocumentBuilder db = dbf.newDocumentBuilder(); | |
Document doc = db.parse(in); | |
NodeList resources = doc.getElementsByTagName("resource"); | |
for (int i = 0; i < resources.getLength(); i++) | |
{ | |
Node node = resources.item(i); | |
Set<String> resourceMethods = new HashSet<String>(); | |
String resourceUri = getUriAndMethods(node, resourceMethods); | |
printResource(resourceUri, resourceMethods); | |
} | |
} | |
private String getUriAndMethods(final Node node, final Set<String> methods) | |
{ | |
String resourceUri = null; | |
NodeList children = node.getChildNodes(); | |
for (int j = 0; j < children.getLength(); j++) | |
{ | |
Node child = children.item(j); | |
if (child.getNodeName().equals("uri")) | |
{ | |
resourceUri = child.getTextContent().trim(); | |
} | |
else if (child.getNodeName().equals("methods")) | |
{ | |
addMethods(child, methods); | |
} | |
} | |
return resourceUri; | |
} | |
private void addMethods(final Node node, final Set<String> methods) | |
{ | |
NodeList children = node.getChildNodes(); | |
for (int i = 0; i < children.getLength(); i++) | |
{ | |
Node method = children.item(i); | |
NodeList methodElems = method.getChildNodes(); | |
for (int j = 0; j < methodElems.getLength(); j++) | |
{ | |
Node elem = methodElems.item(j); | |
if (elem.getNodeName().equals("name")) | |
{ | |
methods.add(elem.getTextContent().trim()); | |
} | |
} | |
} | |
} | |
private void printResource(final String uri, final Set<String> methods) | |
{ | |
for (String method : methods) | |
{ | |
System.out.println(String.format(INTERCEPT_TEMPLATE, uri, method)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment