| author | Steve Pitschke | 2012-07-13 15:32:59 (EDT) |
|---|---|---|
| committer | Steve Pitschke | 2012-07-13 15:32:59 (EDT) |
| commit | ef4430342e4755eb6df6f0cd8251654699f3fc32 (patch) (side-by-side diff) | |
| tree | 75b3b6a1d2aeeecfb09fa99ab06dac387a3d18da | |
| parent | bddbacd335f6eff6c8ee7f191488123476999700 (diff) | |
| download | org.eclipse.lyo.docs-ef4430342e4755eb6df6f0cd8251654699f3fc32.zip org.eclipse.lyo.docs-ef4430342e4755eb6df6f0cd8251654699f3fc32.tar.gz org.eclipse.lyo.docs-ef4430342e4755eb6df6f0cd8251654699f3fc32.tar.bz2 | |
Bug 384760: Add support for oslc.select, oslc.properties and oslc.prefixrefs/changes/03/6803/1
Change-Id: I106ea4f021a0b5bfe9f46420faa1579dd5a81d2a
Signed-off-by: Steve Pitschke <pitschke@us.ibm.com>
3 files changed, 292 insertions, 36 deletions
diff --git a/OSLC4JBugzilla/pom.xml b/OSLC4JBugzilla/pom.xml index 015f18d..b64e962 100644 --- a/OSLC4JBugzilla/pom.xml +++ b/OSLC4JBugzilla/pom.xml @@ -81,6 +81,11 @@ <version>2.2.1</version> <scope>provided</scope> </dependency> + <dependency> + <groupId>org.eclipse.lyo.core.query</groupId> + <artifactId>org.eclipse.lyo.core.query</artifactId> + <version>0.0.1-SNAPSHOT</version> + </dependency> </dependencies> <build> diff --git a/OSLC4JBugzilla/src/main/java/org/eclipse/lyo/oslc4j/bugzilla/BugzillaManager.java b/OSLC4JBugzilla/src/main/java/org/eclipse/lyo/oslc4j/bugzilla/BugzillaManager.java index f393ac7..f464be5 100644 --- a/OSLC4JBugzilla/src/main/java/org/eclipse/lyo/oslc4j/bugzilla/BugzillaManager.java +++ b/OSLC4JBugzilla/src/main/java/org/eclipse/lyo/oslc4j/bugzilla/BugzillaManager.java @@ -20,10 +20,12 @@ package org.eclipse.lyo.oslc4j.bugzilla; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Properties; - import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; @@ -32,11 +34,19 @@ import javax.servlet.http.HttpServletRequest; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.Response.Status; +import org.eclipse.lyo.core.query.ComparisonTerm; +import org.eclipse.lyo.core.query.CompoundTerm; +import org.eclipse.lyo.core.query.PName; +import org.eclipse.lyo.core.query.ParseException; +import org.eclipse.lyo.core.query.QueryUtils; +import org.eclipse.lyo.core.query.SimpleTerm; +import org.eclipse.lyo.core.query.Value; +import org.eclipse.lyo.core.query.WhereClause; import org.eclipse.lyo.oslc4j.bugzilla.resources.BugzillaChangeRequest; import org.eclipse.lyo.oslc4j.bugzilla.servlet.CredentialsFilter; import org.eclipse.lyo.oslc4j.client.ServiceProviderRegistryURIs; -import org.eclipse.lyo.oslc4j.core.OSLC4JUtils; import org.eclipse.lyo.oslc4j.core.model.Link; +import org.eclipse.lyo.oslc4j.core.model.OslcConstants; import com.j2bugzilla.base.Bug; import com.j2bugzilla.base.BugFactory; @@ -177,23 +187,18 @@ public class BugzillaManager implements ServletContextListener { * @param productIdString * @param page * @param limit + * @param oslcWhere + * @param prefixMap * @return The list of bugs, paged if necessary * @throws IOException * @throws ServletException */ - public static List<Bug> getBugsByProduct(final HttpServletRequest httpServletRequest, final String productIdString, int page, int limit) throws IOException, ServletException + public static List<Bug> getBugsByProduct(final HttpServletRequest httpServletRequest, final String productIdString, int page, int limit, String oslcWhere, Map<String, String> prefixMap) throws IOException, ServletException { - List<Bug> results=null; - - + List<Bug> results=null; try { final BugzillaConnector bc = BugzillaManager.getBugzillaConnector(httpServletRequest); - final String pageString = httpServletRequest.getParameter("page"); - - if (null != pageString) { - page = Integer.parseInt(pageString); - } int productId = Integer.parseInt(productIdString); final GetProduct getProducts = new GetProduct(productId); @@ -203,7 +208,7 @@ public class BugzillaManager implements ServletContextListener { bc.executeMethod(getProducts); final Product product = getProducts.getProduct(); - final BugSearch bugSearch = createBugSearch(page, limit, product); + final BugSearch bugSearch = createBugSearch(page, limit, product, oslcWhere, prefixMap); bc.executeMethod(bugSearch); results = bugSearch.getSearchResults(); } else { @@ -218,15 +223,110 @@ public class BugzillaManager implements ServletContextListener { } - protected static BugSearch createBugSearch(int page, int limit, Product product) { + protected static BugSearch createBugSearch(int page, int limit, Product product, String oslcWhere, + Map<String, String> prefixMap) throws ParseException + { + List<BugSearch.SearchQuery> queries = new ArrayList<BugSearch.SearchQuery>(); BugSearch.SearchQuery productQuery = new BugSearch.SearchQuery( BugSearch.SearchLimiter.PRODUCT, product.getName()); + + queries.add(productQuery); + BugSearch.SearchQuery limitQuery = new BugSearch.SearchQuery( BugSearch.SearchLimiter.LIMIT, (limit + 1) + ""); + + queries.add(limitQuery); + BugSearch.SearchQuery offsetQuery = new BugSearch.SearchQuery( BugSearch.SearchLimiter.OFFSET, (page * limit) + ""); - return new BugSearch(productQuery, limitQuery, offsetQuery); + queries.add(offsetQuery); + + if (oslcWhere != null) { + + WhereClause whereClause = QueryUtils.parseWhere(oslcWhere, prefixMap); + + createSearchQueries(queries, whereClause, toplevelQueryProperties); + } + + return new BugSearch(queries.toArray(new BugSearch.SearchQuery[queries.size()])); + } + + protected static void createSearchQueries(List<BugSearch.SearchQuery> queries, + CompoundTerm compoundTerm, + Map<String, Object> queryProperties) + { + for (SimpleTerm term : compoundTerm.children()) { + + switch (term.type()) { + case COMPARISON: + break; + case NESTED: + PName property = term.property(); + Object limiter = queryProperties.get(property.namespace + property.local); + + if (limiter == null || limiter instanceof BugSearch.SearchLimiter) { + throw new WebApplicationException( + new UnsupportedOperationException("Unsupported oslc.where nested term property term: " + term), + Status.BAD_REQUEST); + } + + @SuppressWarnings("unchecked") + Map<String, Object> nestedQueryProperties = (Map<String, Object>)limiter; + + createSearchQueries(queries, (CompoundTerm)term, nestedQueryProperties); + + continue; + + default: + throw new WebApplicationException( + new UnsupportedOperationException("Unsupported oslc.where term type: " + term), + Status.BAD_REQUEST); + } + + ComparisonTerm comparison = (ComparisonTerm)term; + + switch (comparison.operator()) { + case EQUALS: + break; + default: + throw new WebApplicationException( + new UnsupportedOperationException("Unsupported oslc.where comparison operator: " + term), + Status.BAD_REQUEST); + } + + PName property = comparison.property(); + Object limiter = + queryProperties.get(property.namespace + property.local); + + if (limiter == null || ! (limiter instanceof BugSearch.SearchLimiter)) { + throw new WebApplicationException( + new UnsupportedOperationException("Unsupported oslc.where comparison property: " + term), + Status.BAD_REQUEST); + } + + Value operand = comparison.operand(); + String value = operand.toString(); + + switch (operand.type()) { + case STRING: + case URI_REF: + value = value.substring(1, value.length() - 1); + break; + case BOOLEAN: + case DECIMAL: + break; + default: + throw new WebApplicationException( + new UnsupportedOperationException("Unsupported oslc.where comparison operand: " + value), + Status.BAD_REQUEST); + } + + BugSearch.SearchQuery query = new BugSearch.SearchQuery( + (BugSearch.SearchLimiter)limiter, value); + + queries.add(query); + } } /** @@ -407,6 +507,34 @@ public class BugzillaManager implements ServletContextListener { return b.toString(); } + static final Map<String, Object> toplevelQueryProperties = + new HashMap<String, Object>(); + static { + + Map<String, Object> nestedQueryProperties = + new HashMap<String, Object>(1); + + nestedQueryProperties.put(Constants.FOAF_NAMESPACE + "mbox", + BugSearch.SearchLimiter.OWNER); + + toplevelQueryProperties.put(OslcConstants.DCTERMS_NAMESPACE + "contributor", + nestedQueryProperties); + + toplevelQueryProperties.put(Constants.BUGZILLA_NAMESPACE + "component", + BugSearch.SearchLimiter.COMPONENT); + toplevelQueryProperties.put(Constants.BUGZILLA_NAMESPACE + "version", + BugSearch.SearchLimiter.VERSION); + toplevelQueryProperties.put(Constants.BUGZILLA_NAMESPACE + "priority", + BugSearch.SearchLimiter.PRIORITY); + toplevelQueryProperties.put(Constants.BUGZILLA_NAMESPACE + "platform", + BugSearch.SearchLimiter.PLATFORM); + toplevelQueryProperties.put(Constants.BUGZILLA_NAMESPACE + "operatingSystem", + BugSearch.SearchLimiter.OPERATING_SYSTEM); + toplevelQueryProperties.put(OslcConstants.DCTERMS_NAMESPACE + "title", + BugSearch.SearchLimiter.SUMMARY); + toplevelQueryProperties.put(Constants.CHANGE_MANAGEMENT_NAMESPACE + "status", + BugSearch.SearchLimiter.STATUS); + } } diff --git a/OSLC4JBugzilla/src/main/java/org/eclipse/lyo/oslc4j/bugzilla/services/BugzillaChangeRequestService.java b/OSLC4JBugzilla/src/main/java/org/eclipse/lyo/oslc4j/bugzilla/services/BugzillaChangeRequestService.java index 8cb08db..57d17fd 100644 --- a/OSLC4JBugzilla/src/main/java/org/eclipse/lyo/oslc4j/bugzilla/services/BugzillaChangeRequestService.java +++ b/OSLC4JBugzilla/src/main/java/org/eclipse/lyo/oslc4j/bugzilla/services/BugzillaChangeRequestService.java @@ -22,9 +22,8 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Arrays; - import java.util.List; - +import java.util.Map; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; @@ -47,24 +46,28 @@ import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.ws.rs.core.UriInfo; +import org.eclipse.lyo.core.query.ParseException; +import org.eclipse.lyo.core.query.Properties; +import org.eclipse.lyo.core.query.QueryUtils; +import org.eclipse.lyo.oslc4j.bugzilla.BugzillaManager; import org.eclipse.lyo.oslc4j.bugzilla.Constants; import org.eclipse.lyo.oslc4j.bugzilla.jbugzx.rpc.GetLegalValues; import org.eclipse.lyo.oslc4j.bugzilla.resources.BugzillaChangeRequest; import org.eclipse.lyo.oslc4j.bugzilla.resources.ChangeRequest; import org.eclipse.lyo.oslc4j.bugzilla.servlet.ServiceProviderCatalogSingleton; +import org.eclipse.lyo.oslc4j.core.OSLC4JConstants; import org.eclipse.lyo.oslc4j.core.annotation.OslcCreationFactory; import org.eclipse.lyo.oslc4j.core.annotation.OslcDialog; import org.eclipse.lyo.oslc4j.core.annotation.OslcDialogs; +import org.eclipse.lyo.oslc4j.core.annotation.OslcNamespaceDefinition; import org.eclipse.lyo.oslc4j.core.annotation.OslcQueryCapability; +import org.eclipse.lyo.oslc4j.core.annotation.OslcSchema; import org.eclipse.lyo.oslc4j.core.annotation.OslcService; import org.eclipse.lyo.oslc4j.core.model.Compact; import org.eclipse.lyo.oslc4j.core.model.OslcConstants; import org.eclipse.lyo.oslc4j.core.model.OslcMediaType; import org.eclipse.lyo.oslc4j.core.model.Preview; import org.eclipse.lyo.oslc4j.core.model.ServiceProvider; -import org.eclipse.lyo.oslc4j.bugzilla.BugzillaManager; - - import com.j2bugzilla.base.Bug; import com.j2bugzilla.base.BugzillaConnector; @@ -117,6 +120,8 @@ public class BugzillaChangeRequestService * * @param productId * @param where + * @param select + * @param prefix * @param pageString * @return * @throws IOException @@ -124,18 +129,93 @@ public class BugzillaChangeRequestService */ @GET @Produces({OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.APPLICATION_XML, OslcMediaType.APPLICATION_JSON}) - public BugzillaChangeRequest[] getChangeRequests(@PathParam("productId") final String productId, - @QueryParam("oslc.where") final String where, - @QueryParam("page") final String pageString) throws IOException, ServletException + public List<BugzillaChangeRequest> getChangeRequests(@PathParam("productId") final String productId, + @QueryParam("oslc.where") final String where, + @QueryParam("oslc.select") final String select, + @QueryParam("oslc.prefix") final String prefix, + @QueryParam("page") final String pageString) throws IOException, ServletException { - int page=0; //paging not supported yet for rdf, xml or JSON + int page=0; + + if (null != pageString) { + page = Integer.parseInt(pageString); + } + int limit=999; + Map<String, String> prefixMap; + + try { + prefixMap = QueryUtils.parsePrefixes(prefix); + } catch (ParseException e) { + throw new IOException(e); + } + addDefaultPrefixes(prefixMap); + + final List<Bug> bugList = BugzillaManager.getBugsByProduct(httpServletRequest, productId, page, limit, + where, prefixMap); + + Properties properties; + + if (select == null) { + properties = QueryUtils.WILDCARD_PROPERTY_LIST; + } else { + try { + properties = QueryUtils.parseSelect(select, prefixMap); + } catch (ParseException e) { + throw new IOException(e); + } + } - final List<Bug> bugList = BugzillaManager.getBugsByProduct(httpServletRequest, productId, page, limit); final List<BugzillaChangeRequest> results = changeRequestsFromBugList(httpServletRequest, bugList, productId); + + httpServletRequest.setAttribute(OSLC4JConstants.OSLC4J_SELECTED_PROPERTIES, + QueryUtils.invertSelectedProperties(properties)); - return results.toArray(new BugzillaChangeRequest[results.size()]); + return results; + } + + private static void addDefaultPrefixes(final Map<String, String> prefixMap) + { + recursivelyCollectNamespaceMappings(prefixMap, BugzillaChangeRequest.class); + } + + private static void recursivelyCollectNamespaceMappings(final Map<String, String> prefixMap, + final Class<? extends Object> resourceClass) + { + final OslcSchema oslcSchemaAnnotation = resourceClass.getPackage().getAnnotation(OslcSchema.class); + + if (oslcSchemaAnnotation != null) + { + final OslcNamespaceDefinition[] oslcNamespaceDefinitionAnnotations = oslcSchemaAnnotation.value(); + + for (final OslcNamespaceDefinition oslcNamespaceDefinitionAnnotation : oslcNamespaceDefinitionAnnotations) + { + final String prefix = oslcNamespaceDefinitionAnnotation.prefix(); + final String namespaceURI = oslcNamespaceDefinitionAnnotation.namespaceURI(); + + prefixMap.put(prefix, namespaceURI); + } + } + + final Class<?> superClass = resourceClass.getSuperclass(); + + if (superClass != null) + { + recursivelyCollectNamespaceMappings(prefixMap, + superClass); + } + + final Class<?>[] interfaces = resourceClass.getInterfaces(); + + if (interfaces != null) + { + for (final Class<?> interfac : interfaces) + { + recursivelyCollectNamespaceMappings(prefixMap, + interfac); + } + } } /** @@ -144,7 +224,8 @@ public class BugzillaChangeRequestService * Forwards to changerequest_collection_html.jsp to build the html page * * @param productId - * @param changeRequestId + * @param where + * @param prefix * @param pageString * @return * @throws ServletException @@ -152,18 +233,31 @@ public class BugzillaChangeRequestService */ @GET @Produces({ MediaType.TEXT_HTML }) - public Response getHtmlCollection(@PathParam("productId") final String productId, - @PathParam("changeRequestId") final String changeRequestId, - @QueryParam("page") final String pageString) throws ServletException, IOException + public Response getHtmlCollection(@PathParam("productId") final String productId, + @QueryParam("oslc.where") final String where, + @QueryParam("oslc.prefix") final String prefix, + @QueryParam("page") final String pageString) throws ServletException, IOException { int page=0; + + if (null != pageString) { + page = Integer.parseInt(pageString); + } + int limit=20; - if (null != pageString) { - page = Integer.parseInt(pageString); - } - - List<Bug> bugList = BugzillaManager.getBugsByProduct(httpServletRequest, productId, page, limit); + Map<String, String> prefixMap; + + try { + prefixMap = QueryUtils.parsePrefixes(prefix); + } catch (ParseException e) { + throw new IOException(e); + } + + addDefaultPrefixes(prefixMap); + + List<Bug> bugList = BugzillaManager.getBugsByProduct(httpServletRequest, productId, page, limit, + where, prefixMap); final List<BugzillaChangeRequest> results = changeRequestsFromBugList(httpServletRequest, bugList, productId); if (bugList != null) { @@ -194,6 +288,8 @@ public class BugzillaChangeRequestService * * @param productId * @param changeRequestId + * @param propertiesString + * @param prefix * @return * @throws IOException * @throws ServletException @@ -202,9 +298,33 @@ public class BugzillaChangeRequestService @GET @Path("{changeRequestId}") @Produces({OslcMediaType.APPLICATION_RDF_XML, OslcMediaType.APPLICATION_XML, OslcMediaType.APPLICATION_JSON}) - public BugzillaChangeRequest getChangeRequest(@PathParam("productId") final String productId, - @PathParam("changeRequestId") final String changeRequestId) throws IOException, ServletException, URISyntaxException + public BugzillaChangeRequest getChangeRequest(@PathParam("productId") final String productId, + @PathParam("changeRequestId") final String changeRequestId, + @QueryParam("oslc.properties") final String propertiesString, + @QueryParam("oslc.prefix") final String prefix) throws IOException, ServletException, URISyntaxException { + Map<String, String> prefixMap; + + try { + prefixMap = QueryUtils.parsePrefixes(prefix); + } catch (ParseException e) { + throw new IOException(e); + } + + addDefaultPrefixes(prefixMap); + + Properties properties; + + if (propertiesString == null) { + properties = QueryUtils.WILDCARD_PROPERTY_LIST; + } else { + try { + properties = QueryUtils.parseSelect(propertiesString, prefixMap); + } catch (ParseException e) { + throw new IOException(e); + } + } + final Bug bug = BugzillaManager.getBugById(httpServletRequest, changeRequestId); if (bug != null) { @@ -216,6 +336,9 @@ public class BugzillaChangeRequestService changeRequest.setAbout(getAboutURI(productId + "/changeRequests/" + changeRequest.getIdentifier())); setETagHeader(getETagFromChangeRequest(changeRequest), httpServletResponse); + httpServletRequest.setAttribute(OSLC4JConstants.OSLC4J_SELECTED_PROPERTIES, + QueryUtils.invertSelectedProperties(properties)); + return changeRequest; } |

