Skip to content

Instantly share code, notes, and snippets.

View eugenp's full-sized avatar

Eugen eugenp

View GitHub Profile
@eugenp
eugenp / WebConfig.java
Created October 22, 2011 21:37
Bootstraping a web application with Spring 3.1 and Java based Configuration, part 2 - the WebConfig
@Configuration
@EnableWebMvc
public class WebConfig{
//
}
@eugenp
eugenp / FooController.java
Created October 22, 2011 22:00
Bootstraping a web application with Spring 3.1 and Java based Configuration, part 2 - the controller
@Controller
final class FooController{
@Autowired
IFooService service;
@RequestMapping( value = "foo",method = RequestMethod.GET )
@ResponseBody
public final List< Foo > getAll(){
return service.getAll();
@eugenp
eugenp / RESTExceptions.java
Created October 22, 2011 22:20
Bootstraping a web application with Spring 3.1 and Java based Configuration, part 2 - the exceptions
@ResponseStatus( value = HttpStatus.BAD_REQUEST )
public final class BadRequestException extends RuntimeException{
//
}
@ResponseStatus( value = HttpStatus.NOT_FOUND )
public final class ResourceNotFoundException extends RuntimeException{
//
}
@eugenp
eugenp / SpringTest.java
Created October 22, 2011 22:25
Bootstraping a web application with Spring 3.1 and Java based Configuration, part 2 - the test
@RunWith( SpringJUnit4ClassRunner.class )
@ContextConfiguration( classes = { ApplicationConfig.class, PersistenceConfig.class },loader = AnnotationConfigContextLoader.class )
public final class SpringTest{
@Test
public final void whenSpringContextIsInstantiated_thenNoExceptions(){
// When
}
}
@eugenp
eugenp / pom_part2.xml
Created October 25, 2011 17:53
Building a RESTful API with Spring 3.1 and Java based Configuration, part 2 - the pom.xml
<dependencies>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson-mapper-asl.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
@eugenp
eugenp / web.xml
Created October 29, 2011 14:42
Securing a RESTful Web Service with Spring Security 3.1, part 3 - the web.xml
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
@eugenp
eugenp / RestAuthenticationEntryPoint.java
Created October 29, 2011 21:08
Securing a RESTful Web Service with Spring Security 3.1, part 3 - the entry point
@Component( "restAuthenticationEntryPoint" )
public final class RestAuthenticationEntryPoint implements AuthenticationEntryPoint{
@Override
public final void commence
( HttpServletRequest request, HttpServletResponse response, AuthenticationException authException )
throws IOException{
response.sendError( HttpServletResponse.SC_UNAUTHORIZED, "Unauthorized" );
}
}
@eugenp
eugenp / MySavedRequestAwareAuthenticationSuccessHandler.java
Created October 30, 2011 11:11
Securing a RESTful Web Service with Spring Security 3.1, part 3 - the authentication success handler
// Use the DefaultSavedRequest URL
// final String targetUrl = savedRequest.getRedirectUrl();
// this.logger.debug( "Redirecting to DefaultSavedRequest Url: " + targetUrl );
// this.getRedirectStrategy().sendRedirect( request, response, targetUrl );
@eugenp
eugenp / pom_part3.xml
Created October 30, 2011 11:20
Securing a RESTful Web Service with Spring Security 3.1, part 3 - the pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring-security.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring-security.version}</version>
@eugenp
eugenp / DiscoverabilityCreationTest.java
Created November 3, 2011 22:36
RESTful Web Service Discoverability with Spring MVC 3.1, part 4 - the CREATION test
@Test
public void whenResourceIsCreated_thenURIOfTheNewlyCreatedResourceIsDiscoverable(){
// When
Foo unpersistedResource = new Foo( randomAlphabetic( 6 ) );
Response createResponse = givenAuthenticated().contentType( MIME_JSON )
.body( unpersistedResource ).post( paths.getFooURL() );
final String uriOfNewlyCreatedResource = createResp
.getHeader( HttpHeaders.LOCATION );
// Then