Skip to content

Instantly share code, notes, and snippets.

View eugenp's full-sized avatar

Eugen eugenp

View GitHub Profile
@eugenp
eugenp / PaginationDiscoverabilityListener.java
Last active March 24, 2022 13:07
Pagination with REST - the Discoverability Listener full
package org.baeldung.web.hateoas;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriComponentsBuilder;
import com.google.common.base.Preconditions;
@eugenp
eugenp / spring-security-context.xml
Created October 29, 2011 19:50
Securing a RESTful Web Service with Spring Security 3.1, part 3 - the security configuraiton
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans
xmlns="http://www.springframework.org/schema/security"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:sec="http://www.springframework.org/schema/security"
xsi:schemaLocation="
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd
http://www.springframework.org/schema/beans
@eugenp
eugenp / AbstractJpaDAO.java
Created December 7, 2011 20:02
Simplyfing the DAO layer with Spring and Java Generics - the Abstract JPA DAO
public abstract class AbstractJpaDAO< T extends Serializable > {
private Class< T > clazz;
@PersistenceContext
EntityManager entityManager;
public void setClazz( final Class< T > clazzToSet ){
this.clazz = clazzToSet;
}
@eugenp
eugenp / SecurityUtil.java
Created November 5, 2011 14:23
part 5 - the Security test utils
public String authenticateAsAdmin(){
return this.authenticate( "eparaschiv", "eparaschiv" );
}
public String authenticate( String username, String password ){
Response response = RestAssured.given().
.param( UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_USERNAME_KEY, username )
.param( UsernamePasswordAuthenticationFilter.SPRING_SECURITY_FORM_PASSWORD_KEY, password )
.post( this.examplePaths.getLoginURL() );
Preconditions.checkState( response.getStatusCode() == 302 );
@eugenp
eugenp / HTTPLinkHeaderUtil.java
Created January 5, 2014 15:58
Extracting Link rel values
public static String extractURIByRel(final String linkHeader, final String rel) {
if (linkHeader == null) {
return null;
}
String uriWithSpecifiedRel = null;
final String[] links = linkHeader.split(", ");
String linkRelation = null;
for (final String link : links) {
final int positionOfSeparator = link.indexOf(';');
@eugenp
eugenp / SpringConfig.xml
Created March 11, 2012 15:26
Project configuration with Spring - Spring XML config
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
@eugenp
eugenp / Q1.java
Created March 5, 2012 11:30
Interview Question:
package com.macys.rest.common;
import java.util.HashSet;
import java.util.Set;
/**
* - link: http://thenoisychannel.com/2011/08/08/retiring-a-great-interview-problem/ <br>
* Given an input string and a dictionary of words, segment the input string into a space-separated sequence of dictionary words if possible.
* For example, if the input string is "applepie" and dictionary contains a
* standard set of English words, then we would return the string "apple pie" as output.
@eugenp
eugenp / pom.xml
Created February 25, 2012 23:00
RestTemplate with Authentication in Spring 3.1 - Maven dependencies
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.1.4</version>
@eugenp
eugenp / RestTemplateFactory.java
Created February 25, 2012 22:20
RestTemplate with Authentication in Spring 3.1
@Component
public class RestTemplateFactory implements
FactoryBean< RestTemplate >, InitializingBean{
private RestTemplate restTemplate;
public RestTemplate getObject(){
return restTemplate;
}
public Class< RestTemplate > getObjectType(){
return RestTemplate.class;
@eugenp
eugenp / PropertyManualJava_3.1.java
Created February 6, 2012 14:28
Properties with Spring - Register Property manually in Java with Spring 3.1
@Bean
public static PropertySourcesPlaceholderConfigurer properties(){
PropertySourcesPlaceholderConfigurer pspc =
new PropertySourcesPlaceholderConfigurer();
Resource[] resources = new ClassPathResource[ ]
{ new ClassPathResource( "foo.properties" ) };
pspc.setLocations( resources );
pspc.setIgnoreUnresolvablePlaceholders( true );
return pspc;
}