Permalink
Browse files

restSQL 0.7.1 release adds table aliasing

  • Loading branch information...
1 parent a190fb5 commit 35585f62b7713e82e59b34e152db0e68fc915535 @restsql restsql committed Oct 23, 2011
View
18 README.txt
@@ -1,4 +1,4 @@
-README.txt (15-Sep-2011)
+README.txt (23-Oct-2011)
restSQL Deployment Guide
@@ -160,14 +160,24 @@ web.xml: Change the restSQL WEB-INF/web.xml. The LifecycleManager needs to know
The default deployment descriptor (web.xml) contains login config (authentication method) and security constraints (authorization declarations). See the restSQL SDK's /restsql-sdk/default/xml/web.xml for the default deployment descriptor.
-Disabling Security: To disable authentication/authorization, simply remove or comment out the security-constraint and login-config elements in the web.xml.
+Disabling Authentication and Authorization: To disable authentication/authorization, simply remove or comment out the security-constraint and login-config elements in the web.xml.
-Enabling Security: You may use the default security constraints and login config or change it to conform to your specific roles, realm and other requirements. More information on Web Application Security using deployment descriptors is available at http://java.sun.com/javaee/6/docs/tutorial/doc/bncbe.html. Or consult your container's documentation. Authentication mechanisms (credential management, user to role assigment) are typically container-specific/proprietary. You will also need to configure a privileges properties file and reference it in the restsql properties file. See the SDK's Security configuration for instructions.
+Enabling Authentication and Authorization: You may use the default security constraints and login config or change it to conform to your specific roles, realm and other requirements. More information on Web Application Security using deployment descriptors is available at http://java.sun.com/javaee/6/docs/tutorial/doc/bncbe.html. Or consult your container's documentation. Authentication mechanisms (credential management, user to role assigment) are typically container-specific/proprietary. You will also need to configure a privileges properties file and reference it in the restsql properties file. See the SDK's Security configuration for instructions.
Naming: You may deploy this as a single file or exploded war to your JEE container. Rename it to restsql.war or webapps/restsql if you want the path to be http://yourhost:port/restsql. Containers generally use the war file name instead of the web.xml's id to name the web app. Additionally, the SDK's HTTP API Explorer will work without any customization.
Deploy: Copy your exploded war or war to your container's webapps dir and restart the container, or deploy the webapp in your preferred style. All third party dependencies are included in the war distribution in the WEB-INF/lib.
+Java Security Manager: If Java Security is enabled in your container, permissions must be added to your container's policy file. restSQL requires:
+ * read/write access (java.util.PropertyPermission) to the following system properties: org.restsql.properties, org.apache.commons.logging.Log and either log4j.configuration for log4j logging or java.util.logging.config.file for Java Native logging
+ * read access (java.io.FilePermission) to the various properties files, and SQL Resources and triggers directories
+
+However, restsql uses other libraries (jersey, jdbc, logging) which need some unknown combination of access permissions. The only configuration that has been demonstrated to work is to grant all permissions to restsql. For example for Tomcat, add this to the end of the ${TOMCAT_HOME}/conf/catalina.policy file:
+
+ grant codeBase "file:${catalina.base}/webapps/restsql/-" {
+ permission java.security.AllPermission;
+ };
+
-------------------------------------------------------------------------------
Installing restSQL JAR mode
@@ -184,7 +194,7 @@ Additionally one of the following jdbc drivers is necessary for databases with b
* mysql-connector-java-#.jar (tested with MySQL version 5.5)
* postgresql-#.jdbc4.jar (tested with PostgreSQL version 9.0)
-Enabling Security: restSQL will authorize SQL Resource operations. Your app will authenticate users and associate users with roles. You must provide a priviliges properties file and reference it in the restsql.properties. Your app will call restSQL's Authorizer and provide a SecurityContextimplementation. See the SDK's Security configuration for more instructions.
+Enabling Authentication and Authorization: restSQL will authorize SQL Resource operations. Your app will authenticate users and associate users with roles. You must provide a priviliges properties file and reference it in the restsql.properties. Your app will call restSQL's Authorizer and provide a SecurityContextimplementation. See the SDK's Security configuration for more instructions.
-------------------------------------------------------------------------------
View
14 WebContent/WEB-INF/web.xml
@@ -43,7 +43,19 @@
<param-value>/restsql/eclipse-workspace/restsql-test/src/resources/properties/restsql-mysql.properties</param-value>
</context-param>
- <!-- Remove security-constraints and login-config for no authentication/authorization -->
+ <!-- Remove security-roles, security-constraints and login-config for no authentication/authorization -->
+ <security-role>
+ <role-name>all</role-name>
+ </security-role>
+ <security-role>
+ <role-name>limited</role-name>
+ </security-role>
+ <security-role>
+ <role-name>readonly</role-name>
+ </security-role>
+ <security-role>
+ <role-name>admin</role-name>
+ </security-role>
<security-constraint>
<web-resource-collection>
<web-resource-name>SQL Resources</web-resource-name>
View
2 build.properties
@@ -1,5 +1,5 @@
# restsql build.properties
-build.version=0.7
+build.version=0.7.1
compile.classpath=lib/servlet-api.jar;lib/jersey-server-1.5.jar;lib/jsr311-api-1.1.1.jar;lib/commons-logging-1.1.1.jar;../restsql/lib/postgresql-9.0-801.jdbc4.jar
View
2 build.xml
@@ -88,7 +88,7 @@
<mkdir dir="obj/war/WEB-INF/lib" />
<copy todir="obj/war/WEB-INF/lib">
<path>
- <fileset dir="lib" includes="*.jar" excludes="jaxb-*.jar" />
+ <fileset dir="lib" includes="*.jar" excludes="jaxb-*.jar,servlet-api.jar" />
</path>
</copy>
View
12 src/org/restsql/core/Factory.java
@@ -23,9 +23,13 @@
/** Returns connection. */
public static Connection getConnection(final String defaultDatabase) throws SQLException {
- final ConnectionFactory connectionFactory = (ConnectionFactory) getInstance(
- Config.KEY_CONNECTION_FACTORY, Config.DEFAULT_CONNECTION_FACTORY);
- return connectionFactory.getConnection(defaultDatabase);
+ return getConnectionFactory().getConnection(defaultDatabase);
+ }
+
+ /** Return connection factory. Useful for destroying it on app unload. */
+ public static ConnectionFactory getConnectionFactory() {
+ return (ConnectionFactory) getInstance(Config.KEY_CONNECTION_FACTORY,
+ Config.DEFAULT_CONNECTION_FACTORY);
}
/** Returns request object. */
@@ -140,6 +144,8 @@ public static SqlResourceMetaData getSqlResourceMetaData(final SqlResourceDefini
/** Creates JDBC connection objects. */
public interface ConnectionFactory {
public Connection getConnection(String defaultDatabase) throws SQLException;
+
+ public void destroy() throws SQLException;
}
/** Creates request objects. */
View
4 src/org/restsql/core/SqlResource.java
@@ -76,7 +76,7 @@
* @throws SqlResourceException if a database access error occurs
* @return list of rows, where each row is a map of name-value pairs
*/
- public List<Map<String, Object>> readCollection(Request request)
+ public List<Map<String, Object>> readAsCollection(Request request)
throws SqlResourceException;
/**
@@ -86,7 +86,7 @@
* @throws SqlResourceException if a database access error occurs
* @return list of rows, where each row is a map of name-value pairs
*/
- public String readXml(Request request) throws SqlResourceException;
+ public String readAsXml(Request request) throws SqlResourceException;
/**
* Executes insert, update or delete.
View
3 src/org/restsql/core/TableMetaData.java
@@ -29,6 +29,9 @@
*/
public String getQualifiedTableName();
+ /** Returns table alias. */
+ public String getTableAlias();
+
/** Returns table name. */
public String getTableName();
View
6 src/org/restsql/core/impl/AbstractSqlResourceMetaData.java
@@ -361,12 +361,18 @@ private void buildTablesAndColumns(final ResultSet resultSet) throws SQLExceptio
switch (tableRole) {
case Parent:
parentTable = table;
+ if (definition.getParentAlias() != null) {
+ table.setTableAlias(definition.getParentAlias());
+ }
// fall through
case ParentExtension:
parentPlusExtTables.add(table);
break;
case Child:
childTable = table;
+ if (definition.getChildAlias() != null) {
+ table.setTableAlias(definition.getChildAlias());
+ }
// fall through
case ChildExtension:
childPlusExtTables.add(table);
View
32 src/org/restsql/core/impl/ConnectionFactoryImpl.java
@@ -2,8 +2,10 @@
package org.restsql.core.impl;
import java.sql.Connection;
+import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
+import java.util.Properties;
import org.restsql.core.Config;
import org.restsql.core.Factory.ConnectionFactory;
@@ -15,27 +17,41 @@
* @author Mark Sawers
*/
public class ConnectionFactoryImpl implements ConnectionFactory {
- private String driverClassName, url, user, password;
+ private final String driverClassName, url;
+ private final Properties connectProperties;
+ private Driver driver;
public ConnectionFactoryImpl() {
driverClassName = Config.properties.getProperty(Config.KEY_DATABASE_DRIVER_CLASSNAME,
Config.DEFAULT_DATABASE_DRIVER_CLASSNAME);
url = Config.properties.getProperty(Config.KEY_DATABASE_URL, Config.DEFAULT_DATABASE_URL);
- user = Config.properties.getProperty(Config.KEY_DATABASE_USER, Config.DEFAULT_DATABASE_USER);
- password = Config.properties.getProperty(Config.KEY_DATABASE_PASSWORD,
+ String user = Config.properties.getProperty(Config.KEY_DATABASE_USER, Config.DEFAULT_DATABASE_USER);
+ String password = Config.properties.getProperty(Config.KEY_DATABASE_PASSWORD,
Config.DEFAULT_DATABASE_PASSWORD);
+ connectProperties = new Properties();
+ connectProperties.put("user", user);
+ connectProperties.put("password", password);
}
public Connection getConnection(String defaultDatabase) throws SQLException {
- try {
- Class.forName(driverClassName).newInstance();
- } catch (Exception exception) {
- throw new SQLException("Failed to load JDBC driver class " + driverClassName, exception);
+ if (driver == null) {
+ try {
+ Class.forName(driverClassName).newInstance();
+ } catch (Exception exception) {
+ throw new SQLException("Failed to load JDBC driver class " + driverClassName, exception);
+ }
+ driver = DriverManager.getDriver(url);
}
- Connection connection = DriverManager.getConnection(url, user, password);
+ Connection connection = driver.connect(url, connectProperties);
if (defaultDatabase != null) {
connection.setCatalog(defaultDatabase);
}
return connection;
}
+
+ public void destroy() throws SQLException {
+ if (driver != null) {
+ DriverManager.deregisterDriver(driver);
+ }
+ }
}
View
8 src/org/restsql/core/impl/Serializer.java
@@ -85,17 +85,17 @@ static void appendReadDocStart(final StringBuffer string) {
private static void serializeRows(final SqlResource sqlResource, final List<Map<String, Object>> rows,
final StringBuffer string, final int level) {
boolean hierarchical = false;
- String tableName = sqlResource.getDefinition().getParent();
+ String tableAlias = sqlResource.getParentTable().getTableAlias();
if (level == 2) {
- tableName = sqlResource.getDefinition().getChild();
+ tableAlias = sqlResource.getChildTable().getTableAlias();
}
for (final Map<String, Object> row : rows) {
if (level == 1) {
string.append("\n\t<");
} else {
string.append("\n\t\t<");
}
- string.append(tableName);
+ string.append(tableAlias);
// Do parent attribute columns
for (final String columnLabel : row.keySet()) {
@@ -118,7 +118,7 @@ private static void serializeRows(final SqlResource sqlResource, final List<Map<
if (hierarchical) {
string.append("\n\t</");
- string.append(tableName);
+ string.append(tableAlias);
string.append(">");
} else if (level == 2) {
string.append(" />");
View
6 src/org/restsql/core/impl/SqlResourceImpl.java
@@ -88,7 +88,7 @@ public boolean isHierarchical() {
* @throws TriggerException if a trigger error occurs
* @return list of rows, where each row is a map of name-value pairs
*/
- public List<Map<String, Object>> readCollection(final Request request) throws SqlResourceException {
+ public List<Map<String, Object>> readAsCollection(final Request request) throws SqlResourceException {
TriggerManager.executeTriggers(getName(), request, true);
final List<Map<String, Object>> results;
@@ -131,7 +131,7 @@ public boolean isHierarchical() {
* @throws SqlResourceException if the request is invalid or a database access error or trigger exception occurs
* @return xml string
*/
- public String readXml(final Request request) throws SqlResourceException {
+ public String readAsXml(final Request request) throws SqlResourceException {
TriggerManager.executeTriggers(getName(), request, true);
String results;
@@ -280,7 +280,7 @@ private String buildReadResultsFlatXml(final ResultSet resultSet) throws SQLExce
boolean newParent = false;
final int numberParentElementColumns = metaData.getParentReadColumns().size();
final int numberChildElementColumns = metaData.getChildReadColumns().size();
- final String childRowElementName = definition.getChild() + "s";
+ final String childRowElementName = metaData.getChild().getTableAlias() + "s";
Map<String, Object> parentRow = null;
List<Map<String, Object>> childRows = null;
View
10 src/org/restsql/core/impl/TableMetaDataImpl.java
@@ -20,12 +20,14 @@
private final String databaseName;
private final List<ColumnMetaData> primaryKeys;
private final String qualifiedTableName;
+ private String tableAlias;
private final String tableName;
private final TableRole tableRole;
public TableMetaDataImpl(final String tableName, final String qualifedTableName,
final String databaseName, final TableRole tableRole) {
this.tableName = tableName;
+ this.tableAlias = tableName;
qualifiedTableName = qualifedTableName;
this.databaseName = databaseName;
this.tableRole = tableRole;
@@ -57,6 +59,10 @@ public String getQualifiedTableName() {
return qualifiedTableName;
}
+ public String getTableAlias() {
+ return tableAlias;
+ }
+
public String getTableName() {
return tableName;
}
@@ -72,4 +78,8 @@ public boolean isChild() {
public boolean isParent() {
return tableRole == TableRole.Parent;
}
+
+ void setTableAlias(String tableAlias) {
+ this.tableAlias = tableAlias;
+ }
}
View
18 src/org/restsql/core/sqlresource/ObjectFactory.java
@@ -2,7 +2,7 @@
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.2-147
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
-// Generated on: 2011.07.12 at 06:05:14 PM EDT
+// Generated on: 2011.10.23 at 11:14:14 AM EDT
//
@@ -41,6 +41,14 @@ public ObjectFactory() {
}
/**
+ * Create an instance of {@link ValidatedAttribute }
+ *
+ */
+ public ValidatedAttribute createValidatedAttribute() {
+ return new ValidatedAttribute();
+ }
+
+ /**
* Create an instance of {@link Query }
*
*/
@@ -57,14 +65,6 @@ public SqlResourceDefinition createSqlResourceDefinition() {
}
/**
- * Create an instance of {@link ValidatedAttribute }
- *
- */
- public ValidatedAttribute createValidatedAttribute() {
- return new ValidatedAttribute();
- }
-
- /**
* Create an instance of {@link JAXBElement }{@code <}{@link SqlResourceDefinition }{@code >}}
*
*/
View
2 src/org/restsql/core/sqlresource/Query.java
@@ -2,7 +2,7 @@
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.2-147
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
-// Generated on: 2011.07.12 at 06:05:14 PM EDT
+// Generated on: 2011.10.23 at 11:14:14 AM EDT
//
View
56 src/org/restsql/core/sqlresource/SqlResourceDefinition.java
@@ -2,7 +2,7 @@
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.2-147
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
-// Generated on: 2011.07.12 at 06:05:14 PM EDT
+// Generated on: 2011.10.23 at 11:14:14 AM EDT
//
@@ -33,8 +33,10 @@
* &lt;attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* &lt;attribute name="defaultDatabase" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
* &lt;attribute name="parent" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
+ * &lt;attribute name="parentAlias" type="{http://www.w3.org/2001/XMLSchema}string" />
* &lt;attribute name="parentExt" type="{http://www.w3.org/2001/XMLSchema}string" />
* &lt;attribute name="child" type="{http://www.w3.org/2001/XMLSchema}string" />
+ * &lt;attribute name="childAlias" type="{http://www.w3.org/2001/XMLSchema}string" />
* &lt;attribute name="childExt" type="{http://www.w3.org/2001/XMLSchema}string" />
* &lt;attribute name="join" type="{http://www.w3.org/2001/XMLSchema}string" />
* &lt;/restriction>
@@ -60,10 +62,14 @@
protected String defaultDatabase;
@XmlAttribute(name = "parent", required = true)
protected String parent;
+ @XmlAttribute(name = "parentAlias")
+ protected String parentAlias;
@XmlAttribute(name = "parentExt")
protected String parentExt;
@XmlAttribute(name = "child")
protected String child;
+ @XmlAttribute(name = "childAlias")
+ protected String childAlias;
@XmlAttribute(name = "childExt")
protected String childExt;
@XmlAttribute(name = "join")
@@ -195,6 +201,30 @@ public void setParent(String value) {
}
/**
+ * Gets the value of the parentAlias property.
+ *
+ * @return
+ * possible object is
+ * {@link String }
+ *
+ */
+ public String getParentAlias() {
+ return parentAlias;
+ }
+
+ /**
+ * Sets the value of the parentAlias property.
+ *
+ * @param value
+ * allowed object is
+ * {@link String }
+ *
+ */
+ public void setParentAlias(String value) {
+ this.parentAlias = value;
+ }
+
+ /**
* Gets the value of the parentExt property.
*
* @return
@@ -243,6 +273,30 @@ public void setChild(String value) {
}
/**
+ * Gets the value of the childAlias property.
+ *
+ * @return
+ * possible object is
+ * {@link String }
+ *
+ */
+ public String getChildAlias() {
+ return childAlias;
+ }
+
+ /**
+ * Sets the value of the childAlias property.
+ *
+ * @param value
+ * allowed object is
+ * {@link String }
+ *
+ */
+ public void setChildAlias(String value) {
+ this.childAlias = value;
+ }
+
+ /**
* Gets the value of the childExt property.
*
* @return
View
2 src/org/restsql/core/sqlresource/ValidatedAttribute.java
@@ -2,7 +2,7 @@
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.2-147
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
-// Generated on: 2011.07.12 at 06:05:14 PM EDT
+// Generated on: 2011.10.23 at 11:14:14 AM EDT
//
View
2 src/org/restsql/core/sqlresource/package-info.java
@@ -2,7 +2,7 @@
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vhudson-jaxb-ri-2.2-147
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
-// Generated on: 2011.07.12 at 06:05:14 PM EDT
+// Generated on: 2011.10.23 at 11:14:14 AM EDT
//
@javax.xml.bind.annotation.XmlSchema(namespace = "http://restsql.org/schema")
View
10 src/org/restsql/service/LifecycleListener.java
@@ -1,10 +1,13 @@
/* Copyright (c) restSQL Project Contributors. Licensed under MIT. */
package org.restsql.service;
+import java.sql.SQLException;
+
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.restsql.core.Config;
+import org.restsql.core.Factory;
/**
* Loads properties file from servlet context.
@@ -27,7 +30,12 @@ public void contextInitialized(ServletContextEvent event) {
/**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
- public void contextDestroyed(ServletContextEvent arg0) {
+ public void contextDestroyed(ServletContextEvent arg0) {
+ try {
+ Factory.getConnectionFactory().destroy();
+ } catch (SQLException exception) {
+ exception.printStackTrace();
+ }
}
}
View
2 src/org/restsql/service/ResResource.java
@@ -329,7 +329,7 @@ private Response executeRequest(final Request.Type requestType, final String res
if (requestType.equals(Request.Type.SELECT)) {
final Request request = Factory.getRequest(requestType, resName, resIds, params, null,
requestLogger);
- responseBody = sqlResource.readXml(request);
+ responseBody = sqlResource.readAsXml(request);
} else { // INSERT, UPDATE or DELETE
final int rowsAffected;
if (body == null || body.length() == 0) {
View
4 src/org/restsql/service/XmlRequestProcessor.java
@@ -69,7 +69,7 @@ public static int execWrite(final Request.Type requestType, final List<NameValue
@Override
public void endElement(final String uri, final String localName, final String qName)
throws SAXException {
- if (qName.equals(sqlResource.getParentTable().getTableName())) {
+ if (qName.equals(sqlResource.getParentTable().getTableAlias())) {
if (childrenParams != null) {
// Apply operation to the children
// Parent is only for giving context to child inserts and updates
@@ -107,7 +107,7 @@ public int getRowsAffected() {
public void startElement(final String uri, final String localName, final String qName,
final Attributes attributes) throws SAXException {
if (!qName.equals(TAG_REQUEST)) {
- if (qName.equals(sqlResource.getParentTable().getTableName())) {
+ if (qName.equals(sqlResource.getParentTable().getTableAlias())) {
parentAttributes = parseAttributes(attributes);
} else { // child element
if (childrenParams == null) {
View
2 src/resources/xml/SqlResource.xsd
@@ -20,8 +20,10 @@
<attribute name="name" type="string" use="required"></attribute>
<attribute name="defaultDatabase" type="string" use="required"></attribute>
<attribute name="parent" type="string" use="required"></attribute>
+ <attribute name="parentAlias" type="string" use="optional"></attribute>
<attribute name="parentExt" type="string" use="optional"></attribute>
<attribute name="child" type="string" use="optional"></attribute>
+ <attribute name="childAlias" type="string" use="optional"></attribute>
<attribute name="childExt" type="string" use="optional"></attribute>
<attribute name="join" type="string" use="optional"></attribute>
</complexType>

0 comments on commit 35585f6

Please sign in to comment.