aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDmitry Kornilov2014-08-06 08:27:24 (EDT)
committerLukas Jungmann2014-08-07 13:31:53 (EDT)
commitf57a81bb8b1a8653e9ca5f1a766e4406cb05bb3b (patch)
tree72aa18656f7f2f9bfe9e0d3aede8ff6d752287fd
parent5845c9454d5f5b6c6669ca0ebd1319b1c6fd96d1 (diff)
downloadeclipselink.runtime-f57a81bb8b1a8653e9ca5f1a766e4406cb05bb3b.zip
eclipselink.runtime-f57a81bb8b1a8653e9ca5f1a766e4406cb05bb3b.tar.gz
eclipselink.runtime-f57a81bb8b1a8653e9ca5f1a766e4406cb05bb3b.tar.bz2
Bug #441250: JPARS canonical links.
Signed-off-by: Dmitry Kornilov <dmitry.kornilov@oracle.com>
-rw-r--r--dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/ReservedWords.java1
-rw-r--r--dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/features/core/selflinks/SelfLinksResponseBuilder.java23
-rw-r--r--dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/features/paging/PagingResponseBuilder.java11
-rw-r--r--dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/util/HrefHelper.java43
-rw-r--r--jpa/eclipselink.jpars.test/src/org/eclipse/persistence/jpars/test/AllJavaEETests.java3
-rw-r--r--jpa/eclipselink.jpars.test/src/org/eclipse/persistence/jpars/test/server/ServerLinksTest.java102
6 files changed, 164 insertions, 19 deletions
diff --git a/dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/ReservedWords.java b/dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/ReservedWords.java
index e988dba..5cb4e16 100644
--- a/dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/ReservedWords.java
+++ b/dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/ReservedWords.java
@@ -28,6 +28,7 @@ public class ReservedWords {
public static final String JPARS_REL_NEXT = "next";
public static final String JPARS_REL_PREV = "prev";
public static final String JPARS_REL_SELF = "self";
+ public static final String JPARS_REL_CANONICAL = "canonical";
public static final String NO_ROUTE_JAXB_ELEMENT_LABEL = "result";
//
diff --git a/dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/features/core/selflinks/SelfLinksResponseBuilder.java b/dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/features/core/selflinks/SelfLinksResponseBuilder.java
index 9936669..f6a5a2d 100644
--- a/dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/features/core/selflinks/SelfLinksResponseBuilder.java
+++ b/dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/features/core/selflinks/SelfLinksResponseBuilder.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2013 Oracle. All rights reserved.
+ * Copyright (c) 2013, 2014 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
* which accompanies this distribution.
@@ -12,13 +12,6 @@
******************************************************************************/
package org.eclipse.persistence.jpa.rs.features.core.selflinks;
-import java.util.Collection;
-import java.util.List;
-import java.util.Map;
-
-import javax.ws.rs.core.UriInfo;
-import javax.xml.bind.JAXBElement;
-
import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.internal.jpa.rs.metadata.model.ItemLinks;
import org.eclipse.persistence.internal.jpa.rs.metadata.model.LinkV2;
@@ -27,12 +20,19 @@ import org.eclipse.persistence.internal.weaving.PersistenceWeavedRest;
import org.eclipse.persistence.jpa.rs.PersistenceContext;
import org.eclipse.persistence.jpa.rs.ReservedWords;
import org.eclipse.persistence.jpa.rs.features.FeatureResponseBuilderImpl;
+import org.eclipse.persistence.jpa.rs.util.HrefHelper;
import org.eclipse.persistence.jpa.rs.util.IdHelper;
import org.eclipse.persistence.jpa.rs.util.list.PageableCollection;
import org.eclipse.persistence.jpa.rs.util.list.ReadAllQueryResultCollection;
import org.eclipse.persistence.jpa.rs.util.list.ReportQueryResultCollection;
import org.eclipse.persistence.jpa.rs.util.list.ReportQueryResultListItem;
+import javax.ws.rs.core.UriInfo;
+import javax.xml.bind.JAXBElement;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+
@SuppressWarnings({ "unchecked", "rawtypes" })
public class SelfLinksResponseBuilder extends FeatureResponseBuilderImpl {
@@ -72,8 +72,9 @@ public class SelfLinksResponseBuilder extends FeatureResponseBuilderImpl {
ItemLinks itemLinks = new ItemLinks();
ClassDescriptor descriptor = context.getJAXBDescriptorForClass(result.getClass());
PersistenceWeavedRest entity = (PersistenceWeavedRest) result;
- String href = context.getBaseURI() + context.getVersion() + "/" + context.getName() + "/entity/" + descriptor.getAlias() + "/" + IdHelper.stringifyId(result, descriptor.getAlias(), context);
+ String href = HrefHelper.buildEntityHref(context, descriptor.getAlias(), IdHelper.stringifyId(result, descriptor.getAlias(), context));
itemLinks.addItem(new LinkV2(ReservedWords.JPARS_REL_SELF, href));
+ itemLinks.addItem(new LinkV2(ReservedWords.JPARS_REL_CANONICAL, href));
entity._persistence_setLinks(itemLinks);
}
return result;
@@ -119,9 +120,9 @@ public class SelfLinksResponseBuilder extends FeatureResponseBuilderImpl {
ClassDescriptor descriptor = context.getJAXBDescriptorForClass(result.getClass());
if ((result instanceof PersistenceWeavedRest) && (descriptor != null) && (context != null)) {
PersistenceWeavedRest entity = (PersistenceWeavedRest) result;
- String entityId = IdHelper.stringifyId(result, descriptor.getAlias(), context);
- String href = context.getBaseURI() + context.getVersion() + "/" + context.getName() + "/entity/" + descriptor.getAlias() + "/" + entityId;
+ String href = HrefHelper.buildEntityHref(context, descriptor.getAlias(), IdHelper.stringifyId(result, descriptor.getAlias(), context));
itemLinks.addItem(new LinkV2(ReservedWords.JPARS_REL_SELF, href));
+ itemLinks.addItem(new LinkV2(ReservedWords.JPARS_REL_CANONICAL, href));
entity._persistence_setLinks(itemLinks);
return entity;
}
diff --git a/dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/features/paging/PagingResponseBuilder.java b/dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/features/paging/PagingResponseBuilder.java
index 3139c0e..be3a29c 100644
--- a/dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/features/paging/PagingResponseBuilder.java
+++ b/dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/features/paging/PagingResponseBuilder.java
@@ -21,6 +21,7 @@ import org.eclipse.persistence.jpa.rs.PersistenceContext;
import org.eclipse.persistence.jpa.rs.QueryParameters;
import org.eclipse.persistence.jpa.rs.ReservedWords;
import org.eclipse.persistence.jpa.rs.features.FeatureResponseBuilderImpl;
+import org.eclipse.persistence.jpa.rs.util.HrefHelper;
import org.eclipse.persistence.jpa.rs.util.IdHelper;
import org.eclipse.persistence.jpa.rs.util.list.PageableCollection;
import org.eclipse.persistence.jpa.rs.util.list.ReadAllQueryResultCollection;
@@ -97,13 +98,9 @@ public class PagingResponseBuilder extends FeatureResponseBuilderImpl {
if ((result instanceof PersistenceWeavedRest) && (descriptor != null) && (context != null)) {
ItemLinks itemLinks = new ItemLinks();
PersistenceWeavedRest entity = (PersistenceWeavedRest) result;
- String entityId = IdHelper.stringifyId(result, descriptor.getAlias(), context);
-
- StringBuilder href = new StringBuilder(context.getBaseURI().toString());
- href.append(context.getVersion()).append("/").append(context.getName()).append("/entity/")
- .append(descriptor.getAlias()).append("/").append(entityId);
-
- itemLinks.addItem(new LinkV2(ReservedWords.JPARS_REL_SELF, href.toString()));
+ String href = HrefHelper.buildEntityHref(context, descriptor.getAlias(), IdHelper.stringifyId(result, descriptor.getAlias(), context));
+ itemLinks.addItem(new LinkV2(ReservedWords.JPARS_REL_SELF, href));
+ itemLinks.addItem(new LinkV2(ReservedWords.JPARS_REL_CANONICAL, href));
entity._persistence_setLinks(itemLinks);
return entity;
}
diff --git a/dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/util/HrefHelper.java b/dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/util/HrefHelper.java
new file mode 100644
index 0000000..3d8fb5c
--- /dev/null
+++ b/dbws/org.eclipse.persistence.dbws/src/org/eclipse/persistence/jpa/rs/util/HrefHelper.java
@@ -0,0 +1,43 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Oracle. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
+ * which accompanies this distribution.
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ * Dmitry Kornilov - initial implementation
+ ******************************************************************************/
+package org.eclipse.persistence.jpa.rs.util;
+
+import org.eclipse.persistence.jpa.rs.PersistenceContext;
+
+/**
+ * A collection of static methods used to build 'href' attribute values for REST 'link' elements.
+ *
+ * @author Dmitry Kornilov
+ * @since EclipseLink 2.6.0
+ */
+public class HrefHelper {
+
+ /**
+ * Returns a href to single entity resource.
+ * http(s)://root:port/version/context/entity/id
+ *
+ * @param context Persistence context.
+ * @param entityClass Entity class name.
+ * @param entityId Entity ID.
+ * @return href to given entity.
+ */
+ public static String buildEntityHref(PersistenceContext context, String entityClass, String entityId) {
+ StringBuilder href = new StringBuilder(context.getBaseURI().toString());
+ href.append(context.getVersion()).append("/")
+ .append(context.getName()).append("/")
+ .append("entity/")
+ .append(entityClass).append("/")
+ .append(entityId);
+ return href.toString();
+ }
+}
diff --git a/jpa/eclipselink.jpars.test/src/org/eclipse/persistence/jpars/test/AllJavaEETests.java b/jpa/eclipselink.jpars.test/src/org/eclipse/persistence/jpars/test/AllJavaEETests.java
index 48987f9..2f2e010 100644
--- a/jpa/eclipselink.jpars.test/src/org/eclipse/persistence/jpars/test/AllJavaEETests.java
+++ b/jpa/eclipselink.jpars.test/src/org/eclipse/persistence/jpars/test/AllJavaEETests.java
@@ -17,13 +17,14 @@ import org.eclipse.persistence.jpars.test.server.ServerCrudTest;
import org.eclipse.persistence.jpars.test.server.ServerEmployeeTest;
import org.eclipse.persistence.jpars.test.server.ServerEmployeeTestV2;
import org.eclipse.persistence.jpars.test.server.ServerFieldsFilteringTest;
+import org.eclipse.persistence.jpars.test.server.ServerLinksTest;
import org.eclipse.persistence.jpars.test.server.ServerPageableTest;
import org.eclipse.persistence.jpars.test.server.ServerTravelerTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(VersionedTestSuite.class)
-@SuiteClasses({ ServerEmployeeTestV2.class, ServerCrudTest.class, ServerEmployeeTest.class, ServerTravelerTest.class, ServerPageableTest.class, ServerFieldsFilteringTest.class })
+@SuiteClasses({ ServerEmployeeTestV2.class, ServerCrudTest.class, ServerEmployeeTest.class, ServerTravelerTest.class, ServerPageableTest.class, ServerFieldsFilteringTest.class, ServerLinksTest.class })
public class AllJavaEETests {
}
diff --git a/jpa/eclipselink.jpars.test/src/org/eclipse/persistence/jpars/test/server/ServerLinksTest.java b/jpa/eclipselink.jpars.test/src/org/eclipse/persistence/jpars/test/server/ServerLinksTest.java
new file mode 100644
index 0000000..012830f
--- /dev/null
+++ b/jpa/eclipselink.jpars.test/src/org/eclipse/persistence/jpars/test/server/ServerLinksTest.java
@@ -0,0 +1,102 @@
+/*******************************************************************************
+ * Copyright (c) 2014 Oracle. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
+ * which accompanies this distribution.
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ * Dmitry Kornilov - initial implementation
+ ******************************************************************************/
+package org.eclipse.persistence.jpars.test.server;
+
+import org.eclipse.persistence.config.PersistenceUnitProperties;
+import org.eclipse.persistence.dynamic.DynamicClassLoader;
+import org.eclipse.persistence.jpa.rs.PersistenceContext;
+import org.eclipse.persistence.jpa.rs.PersistenceFactoryBase;
+import org.eclipse.persistence.jpars.test.model.basket.Basket;
+import org.eclipse.persistence.jpars.test.util.ExamplePropertiesLoader;
+import org.eclipse.persistence.jpars.test.util.RestUtils;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import javax.persistence.Persistence;
+import javax.ws.rs.core.MediaType;
+import java.net.URISyntaxException;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.logging.Logger;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * This class tests entity links.
+ *
+ * @author Dmitry Kornilov
+ */
+public class ServerLinksTest {
+ private static final Logger logger = Logger.getLogger("org.eclipse.persistence.jpars.test.server");
+
+ private static final String JPA_RS_VERSION_STRING = "jpars.version.string";
+ private static final String DEFAULT_PU = "jpars_basket-static";
+
+ private static PersistenceContext context;
+ private static PersistenceFactoryBase factory;
+
+ @BeforeClass
+ public static void setup() throws Exception {
+ final Map<String, Object> properties = new HashMap<String, Object>();
+ ExamplePropertiesLoader.loadProperties(properties);
+ properties.put(PersistenceUnitProperties.NON_JTA_DATASOURCE, null);
+ properties.put(PersistenceUnitProperties.DDL_GENERATION, PersistenceUnitProperties.DROP_AND_CREATE);
+ properties.put(PersistenceUnitProperties.CLASSLOADER, new DynamicClassLoader(Thread.currentThread().getContextClassLoader()));
+ System.setProperty(JPA_RS_VERSION_STRING, "v2.0");
+ factory = new PersistenceFactoryBase();
+ context = factory.bootstrapPersistenceContext(DEFAULT_PU, Persistence.createEntityManagerFactory(DEFAULT_PU, properties), RestUtils.getServerURI(), null, true);
+ initData();
+ }
+
+ @AfterClass
+ public static void cleanup() throws Exception {
+ RestUtils.restUpdateQuery(context, "Basket.deleteAll", "Basket", null, null, MediaType.APPLICATION_JSON_TYPE);
+ }
+
+ private static void initData() throws Exception {
+ // Create a basket
+ Basket basket = new Basket();
+ basket.setId(1);
+ basket.setName("Basket1");
+ basket = RestUtils.restCreate(context, basket, Basket.class.getSimpleName(), Basket.class, null, MediaType.APPLICATION_XML_TYPE, true);
+ assertNotNull("Basket create failed.", basket);
+ }
+
+ @Test
+ public void testLinksJson() throws URISyntaxException {
+ // Get BasketItem with id = 1
+ String queryResult = RestUtils.restRead(context, 1, Basket.class.getSimpleName(), null, MediaType.APPLICATION_JSON_TYPE);
+ logger.info(queryResult);
+
+ // Check that 'canonical' link is in place
+ assertTrue(queryResult.contains("{\"href\":\"http://localhost:7001/eclipselink.jpars.test/persistence/v2.0/jpars_basket-static/entity/Basket/1\",\"rel\":\"canonical\"}"));
+
+ // Check that 'self' link is in place
+ assertTrue(queryResult.contains("{\"href\":\"http://localhost:7001/eclipselink.jpars.test/persistence/v2.0/jpars_basket-static/entity/Basket/1\",\"rel\":\"self\"}"));
+ }
+
+ @Test
+ public void testLinksXml() throws URISyntaxException {
+ // Get BasketItem with id = 1
+ String queryResult = RestUtils.restRead(context, 1, Basket.class.getSimpleName(), null, MediaType.APPLICATION_XML_TYPE);
+ logger.info(queryResult);
+
+ // Check that 'canonical' link is in place
+ assertTrue(queryResult.contains("<link><href>http://localhost:7001/eclipselink.jpars.test/persistence/v2.0/jpars_basket-static/entity/Basket/1</href><rel>canonical</rel></link>"));
+
+ // Check that 'self' link is in place
+ assertTrue(queryResult.contains("<link><href>http://localhost:7001/eclipselink.jpars.test/persistence/v2.0/jpars_basket-static/entity/Basket/1</href><rel>self</rel></link>"));
+ }
+}