1
|
/*
|
2
|
* Styled Layer Descriptor Factory
|
3
|
* Author: Matt Perry
|
4
|
* Status: testing
|
5
|
* MPTODO: Use a spatial access constraints class to generate the appropriate SLD filter
|
6
|
*/
|
7
|
package edu.ucsb.nceas.metacat.spatial;
|
8
|
|
9
|
import edu.ucsb.nceas.utilities.XMLUtilities;
|
10
|
|
11
|
import javax.servlet.ServletConfig;
|
12
|
import javax.servlet.ServletContext;
|
13
|
import javax.servlet.ServletException;
|
14
|
import javax.servlet.ServletInputStream;
|
15
|
import javax.servlet.http.HttpServlet;
|
16
|
import javax.servlet.http.HttpServletRequest;
|
17
|
import javax.servlet.http.HttpServletResponse;
|
18
|
import javax.servlet.http.HttpSession;
|
19
|
import javax.servlet.http.HttpUtils;
|
20
|
import javax.servlet.ServletOutputStream;
|
21
|
|
22
|
import javax.xml.parsers.DocumentBuilder;
|
23
|
import javax.xml.parsers.DocumentBuilderFactory;
|
24
|
import javax.xml.parsers.FactoryConfigurationError;
|
25
|
import javax.xml.parsers.ParserConfigurationException;
|
26
|
|
27
|
import org.xml.sax.SAXException;
|
28
|
import org.xml.sax.SAXParseException;
|
29
|
|
30
|
import java.io.File;
|
31
|
import java.io.IOException;
|
32
|
|
33
|
import org.w3c.dom.*;
|
34
|
|
35
|
/**
|
36
|
* Class to generate Styled Layer Descriptors allowing geoserver to
|
37
|
* filter the feature rendering based on various contraints
|
38
|
* such as permissions, non-spatial queries and skin configuration.
|
39
|
*/
|
40
|
public class SldFactory extends HttpServlet {
|
41
|
|
42
|
static Document document;
|
43
|
static String sld;
|
44
|
|
45
|
/**
|
46
|
* Handle "GET" method requests from HTTP clients
|
47
|
*
|
48
|
* @param request Incoming servlet request.
|
49
|
* @param response Servlet response.
|
50
|
*/
|
51
|
public void doGet(HttpServletRequest request, HttpServletResponse response)
|
52
|
throws ServletException, IOException
|
53
|
{
|
54
|
handleGetOrPost(request, response);
|
55
|
}
|
56
|
|
57
|
/**
|
58
|
* Handle "POST" method requests from HTTP clients
|
59
|
*
|
60
|
* @param request Incoming servlet request.
|
61
|
* @param response Servlet response.
|
62
|
*/
|
63
|
public void doPost(HttpServletRequest request, HttpServletResponse response)
|
64
|
throws ServletException, IOException
|
65
|
{
|
66
|
handleGetOrPost(request, response);
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* Control servlet response depending on the action parameter specified
|
71
|
* Modifies the original SLD according to various access contraints
|
72
|
* and sends it as the servlet response.
|
73
|
*
|
74
|
* @param request Incoming servlet request.
|
75
|
* @param response Servlet response.
|
76
|
* @todo Determine filename from variables in metacat.properties.
|
77
|
*/
|
78
|
private void handleGetOrPost(HttpServletRequest request, HttpServletResponse response)
|
79
|
throws ServletException, IOException
|
80
|
{
|
81
|
//String dataset = request.getParameter("dataset");
|
82
|
// MPTODO : Eventually use dataset to determine filename
|
83
|
String filename = "/var/lib/tomcat5/webapps/knb/style/skins/ebm/spatial/data_bounds_style.sld";
|
84
|
String sld = getSld(filename);
|
85
|
|
86
|
response.setContentType("text/xml");
|
87
|
response.getWriter().write(sld);
|
88
|
|
89
|
System.out.println("\n*** SldFactory request handled ***\n");
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* Given a filename of an existing SLD document,
|
94
|
* reads the sld and adds an ogc:Filter to exclude/include
|
95
|
* certain docids based on user's permissions.
|
96
|
*
|
97
|
* returns SLD document as a String.
|
98
|
*
|
99
|
* @param filename Filename of the base sld.
|
100
|
* @todo Implement the doc id list queries for contraints.
|
101
|
*/
|
102
|
private String getSld(String filename)
|
103
|
{
|
104
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
105
|
|
106
|
try {
|
107
|
DocumentBuilder builder = factory.newDocumentBuilder();
|
108
|
document = builder.parse( new File(filename) );
|
109
|
} catch (SAXException sxe) {
|
110
|
sxe.printStackTrace();
|
111
|
} catch (ParserConfigurationException pce) {
|
112
|
pce.printStackTrace();
|
113
|
} catch (IOException ioe) {
|
114
|
ioe.printStackTrace();
|
115
|
}
|
116
|
|
117
|
Element root = document.getDocumentElement();
|
118
|
NodeList elemList = document.getElementsByTagName("Rule");
|
119
|
Node ruleNode = elemList.item(0);
|
120
|
|
121
|
// Get the list of docids due to access contrainsts
|
122
|
|
123
|
// Get the list of docids due to skin filtering of org names
|
124
|
|
125
|
// Get the list of docids due to existing query (eg taxonomic)
|
126
|
|
127
|
// Get the Element/node comprising the ogc:Filter for all of the above
|
128
|
Element filterElement = getFilterElement(document);
|
129
|
|
130
|
// Append the Filter to the ruleNode
|
131
|
ruleNode.appendChild(filterElement);
|
132
|
|
133
|
// Write new DOM as a string
|
134
|
String rulesString = XMLUtilities.getDOMTreeAsString((Node) root, true);
|
135
|
|
136
|
return rulesString;
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* Given a DOM document and a vector of docids,
|
141
|
* Creates an OGC Filter node
|
142
|
*
|
143
|
* @param inDoc DOM document.
|
144
|
*
|
145
|
*/
|
146
|
private Element getFilterElement(Document inDoc)
|
147
|
{
|
148
|
Element filterElem = inDoc.createElement("ogc:Filter");
|
149
|
|
150
|
// MPTODO : BEGIN LOOP thru vector of docids
|
151
|
Element opElem = inDoc.createElement("ogc:PropertyIsEqualTo");
|
152
|
|
153
|
Element propertyElem = inDoc.createElement("ogc:PropertyName");
|
154
|
Text propertyText = inDoc.createTextNode("docid");
|
155
|
propertyElem.appendChild(propertyText);
|
156
|
|
157
|
Element literalElem = inDoc.createElement("ogc:Literal");
|
158
|
Text literalText = inDoc.createTextNode("perry.1"); // i
|
159
|
literalElem.appendChild(literalText);
|
160
|
|
161
|
opElem.appendChild(propertyElem);
|
162
|
opElem.appendChild(literalElem);
|
163
|
filterElem.appendChild(opElem);
|
164
|
// END LOOP
|
165
|
|
166
|
return filterElem;
|
167
|
}
|
168
|
|
169
|
}
|