Skip to content

Commit

Permalink
Convert all resource servers to Java (from Groovy)
Browse files Browse the repository at this point in the history
and also remove all CorsFilter implementations (since it is
natively supported in Spring MVC now).

The Groovy was distracting and cost some effort to maintain and
explain to people how to use it in their IDE (Eclipse in particular
is mostly broken these days). So it's better to switch to Java.
  • Loading branch information
dsyer committed Mar 29, 2016
1 parent 35e00f1 commit 5638eb2
Show file tree
Hide file tree
Showing 32 changed files with 538 additions and 617 deletions.
28 changes: 0 additions & 28 deletions double/resource/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
Expand All @@ -57,30 +53,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerId>groovy-eclipse-compiler</compilerId>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.9.0-01</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.4.3-01</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>2.8.0-01</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>

Expand Down
60 changes: 0 additions & 60 deletions double/resource/src/main/groovy/demo/ResourceApplication.groovy

This file was deleted.

106 changes: 106 additions & 0 deletions double/resource/src/main/java/demo/ResourceApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package demo;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.http.HttpMethod;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@EnableRedisHttpSession
public class ResourceApplication extends WebSecurityConfigurerAdapter {

private String message = "Hello World";
private List<Change> changes = new ArrayList<>();

@RequestMapping(value = "/", method = RequestMethod.GET)
public Message home() {
return new Message(message);
}

@RequestMapping(value = "/changes", method = RequestMethod.GET)
public List<Change> changes() {
return changes;
}

@RequestMapping(value = "/", method = RequestMethod.POST)
public Message update(@RequestBody Message map, Principal principal) {
if (map.getContent() != null) {
message = map.getContent();
changes.add(new Change(principal.getName(), message));
while (changes.size() > 10) {
changes.remove(0);
}
}
return new Message(message);
}

public static void main(String[] args) {
SpringApplication.run(ResourceApplication.class, args);
}

@Override
protected void configure(HttpSecurity http) throws Exception {
// We need this to prevent the browser from popping up a dialog on a 401
http.httpBasic().disable();
http.authorizeRequests().antMatchers(HttpMethod.POST, "/**").hasRole("WRITER").anyRequest().authenticated();
}
}

class Message {
private String id = UUID.randomUUID().toString();
private String content;

Message() {
}

public Message(String content) {
this.content = content;
}

public String getId() {
return id;
}

public String getContent() {
return content;
}
}

class Change {
private Date timestamp = new Date();
private String user;
private String message;

Change() {
}

public Change(String user, String message) {
this.user = user;
this.message = message;
}

public Date getTimestamp() {
return timestamp;
}

public String getUser() {
return user;
}

public String getMessage() {
return message;
}
}
36 changes: 0 additions & 36 deletions double/resource/src/test/groovy/demo/ApplicationTests.groovy

This file was deleted.

35 changes: 0 additions & 35 deletions double/resource/src/test/groovy/demo/ResourceTests.groovy

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
package demo
package demo;

import static org.junit.Assert.assertEquals;

import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.test.context.web.WebAppConfiguration;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.client.RestTemplate;

@RunWith(SpringJUnit4ClassRunner)
@SpringApplicationConfiguration(classes = ResourceApplication)

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ResourceApplication.class)
@WebAppConfiguration
@IntegrationTest('server.port:0')
@IntegrationTest("server.port:0")
public class ApplicationTests {

@Value('${local.server.port}')
@Value("${local.server.port}")
private int port;

private RestTemplate template = new TestRestTemplate();

@Test
public void resourceLoads() {
ResponseEntity<String> response = template.getForEntity("http://localhost:${port}/", String.class)
assertEquals(HttpStatus.OK, response.getStatusCode())
public void resourceProtected() {
ResponseEntity<String> response = template.getForEntity("http://localhost:{port}/", String.class, port);
// N.B. better if it was UNAUTHORIZED but that means we have to add a custom authentication entry point
assertEquals(HttpStatus.FORBIDDEN, response.getStatusCode());
}

}
35 changes: 35 additions & 0 deletions double/resource/src/test/java/demo/ResourceTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package demo;

import static org.junit.Assert.assertEquals;

import java.security.Principal;

import org.junit.Test;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;


public class ResourceTests {

private ResourceApplication resource = new ResourceApplication();

@Test
public void home() {
assertEquals("Hello World", resource.home().getContent());
}

@Test
public void changes() {
Principal user = new UsernamePasswordAuthenticationToken("admin", "");
resource.update(new Message("Foo"), user);
assertEquals(1, resource.changes().size());
}

@Test
public void changesOverflow() {
for (int i=1; i<=11; i++) { resource.changes().add(new Change("foo", "bar")); }
Principal user = new UsernamePasswordAuthenticationToken("admin", "");
resource.update(new Message("Foo"), user);
assertEquals(10, resource.changes().size());
}

}
Loading

0 comments on commit 5638eb2

Please sign in to comment.