Permalink
Browse files

Working out issues with transactions and controllers and sessions (oh…

… my\!)
  • Loading branch information...
1 parent 5477d55 commit 6a6da1336107b546abcfe2f40645031277bfe44e @jbrisbin jbrisbin committed Jan 27, 2012
View
@@ -2,3 +2,4 @@
exporter-2012-*
.gradle
*.iws
+build/
@@ -1,23 +1,12 @@
package org.springframework.data.services.web.exporter;
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.List;
import javax.servlet.http.HttpServletRequest;
-import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
-import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
-import org.springframework.data.services.DelegatingResolver;
-import org.springframework.data.services.Resolver;
-import org.springframework.data.services.util.UriUtils;
-import org.springframework.http.HttpEntity;
-import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
-import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@@ -26,114 +15,10 @@
*/
@Controller
@RequestMapping("/**")
-public class ResourceExporterController implements ApplicationContextAware, InitializingBean {
-
- private ApplicationContext applicationContext;
- private String host;
- private URI baseUri;
- private DelegatingResolver readResolvers = new DelegatingResolver();
-
- public String getHost() {
- return host;
- }
-
- public ResourceExporterController setHost(String host) {
- this.host = host;
- return this;
- }
-
- public URI getBaseUri() {
- return baseUri;
- }
-
- public ResourceExporterController setBaseUri(URI baseUri) {
- this.baseUri = baseUri;
- return this;
- }
-
- public List<Resolver> getReadResolvers() {
- return readResolvers.resolvers();
- }
-
- public ResourceExporterController setReadResolvers(List<Resolver> readResolvers) {
- Assert.notNull(readResolvers, "Read resolvers cannot be null.");
- this.readResolvers.resolvers().addAll(readResolvers);
- return this;
- }
-
-
- @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
- this.applicationContext = applicationContext;
- }
-
- @Override public void afterPropertiesSet() throws Exception {
-
- }
-
- @RequestMapping(method = RequestMethod.HEAD)
- public void head(HttpServletRequest request, Model model) {
- try {
- URI requestUri = new URI(request.getRequestURL().toString());
- System.out.println("URI: " + requestUri);
-
- Object o = null;
- for (URI uri : UriUtils.explode(baseUri, requestUri)) {
- o = readResolvers.resolve(uri, o);
- System.out.println("o: " + o);
- }
-
- if (null != o) {
- model.addAttribute("status", HttpStatus.OK);
- model.addAttribute("resource", o);
- } else {
- model.addAttribute("status", HttpStatus.NOT_FOUND);
- }
- } catch (URISyntaxException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
-
- }
+public interface ResourceExporterController extends ApplicationContextAware, InitializingBean {
@Transactional(readOnly = true)
@RequestMapping(method = RequestMethod.GET)
- public void get(HttpServletRequest request, Model model) {
- try {
- URI requestUri = new URI(request.getRequestURL().toString());
- System.out.println("URI: " + requestUri);
-
- Object o = null;
- for (URI uri : UriUtils.explode(baseUri, requestUri)) {
- o = readResolvers.resolve(uri, o);
- System.out.println("o: " + o);
- }
- if (readResolvers.supports(requestUri, o)) {
- o = readResolvers.resolve(requestUri, o);
- }
- if (null != o) {
- model.addAttribute("status", HttpStatus.OK);
- model.addAttribute("resource", o);
- } else {
- model.addAttribute("status", HttpStatus.NOT_FOUND);
- }
- } catch (URISyntaxException e) {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- }
-
- @RequestMapping(method = RequestMethod.POST)
- public void post(HttpServletRequest request, HttpEntity<byte[]> input, Model model) {
-
- }
-
- @RequestMapping(method = RequestMethod.PUT)
- public void put(HttpServletRequest request, HttpEntity<byte[]> input, Model model) {
-
- }
-
- @RequestMapping(method = RequestMethod.DELETE)
- public void delete(HttpServletRequest request, Model model) {
-
- }
-
+ public void get(HttpServletRequest request, Model model);
}
@@ -0,0 +1,88 @@
+package org.springframework.data.services.web.exporter;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.List;
+import javax.servlet.http.HttpServletRequest;
+
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.data.services.DelegatingResolver;
+import org.springframework.data.services.Resolver;
+import org.springframework.data.services.util.UriUtils;
+import org.springframework.http.HttpStatus;
+import org.springframework.ui.Model;
+import org.springframework.util.Assert;
+
+/**
+ * @author Jon Brisbin <jon@jbrisbin.com>
+ */
+public class SimpleResourceExporterController implements ResourceExporterController {
+
+ private ApplicationContext applicationContext;
+ private String host;
+ private URI baseUri;
+ private DelegatingResolver readResolvers = new DelegatingResolver();
+
+ public String getHost() {
+ return host;
+ }
+
+ public SimpleResourceExporterController setHost(String host) {
+ this.host = host;
+ return this;
+ }
+
+ public URI getBaseUri() {
+ return baseUri;
+ }
+
+ public SimpleResourceExporterController setBaseUri(URI baseUri) {
+ this.baseUri = baseUri;
+ return this;
+ }
+
+ public List<Resolver> getReadResolvers() {
+ return readResolvers.resolvers();
+ }
+
+ public SimpleResourceExporterController setReadResolvers(List<Resolver> readResolvers) {
+ Assert.notNull(readResolvers, "Read resolvers cannot be null.");
+ this.readResolvers.resolvers().addAll(readResolvers);
+ return this;
+ }
+
+
+ @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
+ this.applicationContext = applicationContext;
+ }
+
+ @Override public void afterPropertiesSet() throws Exception {
+
+ }
+
+ @Override public void get(HttpServletRequest request, Model model) {
+ try {
+ URI requestUri = new URI(request.getRequestURL().toString());
+ System.out.println("URI: " + requestUri);
+
+ Object o = null;
+ for (URI uri : UriUtils.explode(baseUri, requestUri)) {
+ o = readResolvers.resolve(uri, o);
+ System.out.println("o: " + o);
+ }
+ if (readResolvers.supports(requestUri, o)) {
+ o = readResolvers.resolve(requestUri, o);
+ }
+ if (null != o) {
+ model.addAttribute("status", HttpStatus.OK);
+ model.addAttribute("resource", o);
+ } else {
+ model.addAttribute("status", HttpStatus.NOT_FOUND);
+ }
+ } catch (URISyntaxException e) {
+ e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
+ }
+ }
+
+}
@@ -1,7 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
+ xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+ http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
+
+ <tx:annotation-driven/>
<!-- Spring MVC >=3.1 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>
@@ -45,7 +49,7 @@
</bean>
<!-- The Exporter -->
- <bean class="org.springframework.data.services.web.exporter.ResourceExporterController">
+ <bean class="org.springframework.data.services.web.exporter.SimpleResourceExporterController">
<property name="baseUri" ref="baseUri"/>
<property name="readResolvers">
<list>

0 comments on commit 6a6da13

Please sign in to comment.