Merge remote-tracking branch 'eugenp/master'

This commit is contained in:
DOHA 2018-03-07 14:59:27 +02:00
commit 89e273620b
30 changed files with 1038 additions and 49 deletions

68
apache-curator/pom.xml Normal file
View File

@ -0,0 +1,68 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>apache-curator</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<properties>
<curator.version>4.0.1</curator.version>
<zookeeper.version>3.4.11</zookeeper.version>
<jackson-databind.version>2.9.4</jackson-databind.version>
<!-- testing -->
<assertj.version>3.6.1</assertj.version>
</properties>
<dependencies>
<!-- curator -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-async</artifactId>
<version>${curator.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>${curator.version}</version>
</dependency>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<!-- utils -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind.version}</version>
</dependency>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,31 @@
package com.baeldung.apache.curator.modeled;
public class HostConfig {
private String hostname;
private int port;
public HostConfig() {
}
public HostConfig(String hostname, int port) {
this.hostname = hostname;
this.port = port;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getHostname() {
return hostname;
}
public void setHostname(String hostname) {
this.hostname = hostname;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.apache.curator;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.junit.Before;
public abstract class BaseTest {
@Before
public void setup() {
org.apache.log4j.BasicConfigurator.configure();
}
protected CuratorFramework newClient() {
int sleepMsBetweenRetries = 100;
int maxRetries = 3;
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
return CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
}
}

View File

@ -0,0 +1,98 @@
package com.baeldung.apache.curator.configuration;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.x.async.AsyncCuratorFramework;
import org.junit.Test;
import com.baeldung.apache.curator.BaseTest;
public class ConfigurationManagementManualTest extends BaseTest {
private static final String KEY_FORMAT = "/%s";
@Test
public void givenPath_whenCreateKey_thenValueIsStored() throws Exception {
try (CuratorFramework client = newClient()) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
String key = getKey();
String expected = "my_value";
// Create key nodes structure
client
.create()
.forPath(key);
// Set data value for our key
async
.setData()
.forPath(key, expected.getBytes());
// Get data value
AtomicBoolean isEquals = new AtomicBoolean();
async
.getData()
.forPath(key)
.thenAccept(data -> isEquals.set(new String(data).equals(expected)));
Thread.sleep(1000);
assertThat(isEquals.get()).isTrue();
}
}
@Test
public void givenPath_whenWatchAKeyAndStoreAValue_thenWatcherIsTriggered() throws Exception {
try (CuratorFramework client = newClient()) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
String key = getKey();
String expected = "my_value";
// Create key structure
async
.create()
.forPath(key);
List<String> changes = new ArrayList<>();
// Watch data value
async
.watched()
.getData()
.forPath(key)
.event()
.thenAccept(watchedEvent -> {
try {
changes.add(new String(client
.getData()
.forPath(watchedEvent.getPath())));
} catch (Exception e) {
// fail ...
}
});
// Set data value for our key
async
.setData()
.forPath(key, expected.getBytes());
Thread.sleep(1000);
assertThat(changes.size() > 0).isTrue();
}
}
private String getKey() {
return String.format(KEY_FORMAT, UUID
.randomUUID()
.toString());
}
}

View File

@ -0,0 +1,69 @@
package com.baeldung.apache.curator.connection;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.RetryNTimes;
import org.apache.curator.x.async.AsyncCuratorFramework;
import org.junit.Test;
public class ConnectionManagementManualTest {
@Test
public void givenRunningZookeeper_whenOpenConnection_thenClientIsOpened() throws Exception {
int sleepMsBetweenRetries = 100;
int maxRetries = 3;
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
client.start();
assertThat(client
.checkExists()
.forPath("/")).isNotNull();
}
}
@Test
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncNotBlocking_thenClientIsOpened() throws InterruptedException {
int sleepMsBetweenRetries = 100;
int maxRetries = 3;
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
AtomicBoolean exists = new AtomicBoolean(false);
async
.checkExists()
.forPath("/")
.thenAcceptAsync(s -> exists.set(s != null));
Thread.sleep(100);
assertThat(exists.get()).isTrue();
}
}
@Test
public void givenRunningZookeeper_whenOpenConnectionUsingAsyncBlocking_thenClientIsOpened() throws InterruptedException {
int sleepMsBetweenRetries = 100;
int maxRetries = 3;
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
AtomicBoolean exists = new AtomicBoolean(false);
async
.checkExists()
.forPath("/")
.thenAccept(s -> exists.set(s != null));
Thread.sleep(100);
assertThat(exists.get()).isTrue();
}
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.apache.curator.modeled;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.x.async.AsyncCuratorFramework;
import org.apache.curator.x.async.modeled.JacksonModelSerializer;
import org.apache.curator.x.async.modeled.ModelSpec;
import org.apache.curator.x.async.modeled.ModeledFramework;
import org.apache.curator.x.async.modeled.ZPath;
import org.junit.Test;
import com.baeldung.apache.curator.BaseTest;
public class ModelTypedExamplesManualTest extends BaseTest {
@Test
public void givenPath_whenStoreAModel_thenNodesAreCreated() throws InterruptedException {
ModelSpec<HostConfig> mySpec = ModelSpec
.builder(ZPath.parseWithIds("/config/dev"), JacksonModelSerializer.build(HostConfig.class))
.build();
try (CuratorFramework client = newClient()) {
client.start();
AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client);
ModeledFramework<HostConfig> modeledClient = ModeledFramework.wrap(async, mySpec);
modeledClient.set(new HostConfig("host-name", 8080));
modeledClient
.read()
.whenComplete((value, e) -> {
if (e != null) {
fail("Cannot read host config", e);
} else {
assertThat(value).isNotNull();
assertThat(value.getHostname()).isEqualTo("host-name");
assertThat(value.getPort()).isEqualTo(8080);
}
});
}
}
}

View File

@ -0,0 +1,74 @@
package com.baeldung.apache.curator.recipes;
import static org.assertj.core.api.Assertions.assertThat;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.leader.LeaderSelector;
import org.apache.curator.framework.recipes.leader.LeaderSelectorListener;
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex;
import org.apache.curator.framework.recipes.shared.SharedCount;
import org.apache.curator.framework.state.ConnectionState;
import org.junit.Test;
import com.baeldung.apache.curator.BaseTest;
public class RecipesManualTest extends BaseTest {
@Test
public void givenRunningZookeeper_whenUsingLeaderElection_thenNoErrors() {
try (CuratorFramework client = newClient()) {
client.start();
LeaderSelector leaderSelector = new LeaderSelector(client, "/mutex/select/leader/for/job/A", new LeaderSelectorListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
}
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
// I'm the leader of the job A !
}
});
leaderSelector.start();
// Wait until the job A is done among all the members
leaderSelector.close();
}
}
@Test
public void givenRunningZookeeper_whenUsingSharedLock_thenNoErrors() throws Exception {
try (CuratorFramework client = newClient()) {
client.start();
InterProcessSemaphoreMutex sharedLock = new InterProcessSemaphoreMutex(client, "/mutex/process/A");
sharedLock.acquire();
// Do process A
sharedLock.release();
}
}
@Test
public void givenRunningZookeeper_whenUsingSharedCounter_thenCounterIsIncrement() throws Exception {
try (CuratorFramework client = newClient()) {
client.start();
try (SharedCount counter = new SharedCount(client, "/counters/A", 0)) {
counter.start();
counter.setCount(0);
counter.setCount(counter.getCount() + 1);
assertThat(counter.getCount()).isEqualTo(1);
}
}
}
}

View File

@ -6,6 +6,12 @@
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>

View File

@ -38,21 +38,19 @@ public class EC2Application {
}
public static void main(String[] args) {
String yourInstanceId = "<you-instance>";
// 0) - Set up the client
// Set up the client
AmazonEC2 ec2Client = AmazonEC2ClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(Regions.US_EAST_1)
.build();
// 1) - Create a security group
// Create a security group
CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest().withGroupName("BaeldungSecurityGroup")
.withDescription("Baeldung Security Group");
ec2Client.createSecurityGroup(createSecurityGroupRequest);
// 2) - Allow HTTP and SSH traffic
// Allow HTTP and SSH traffic
IpRange ipRange1 = new IpRange().withCidrIp("0.0.0.0/0");
IpPermission ipPermission1 = new IpPermission().withIpv4Ranges(Arrays.asList(new IpRange[] { ipRange1 }))
@ -71,7 +69,7 @@ public class EC2Application {
ec2Client.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);
// 3) - Create KeyPair
// Create KeyPair
CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest()
.withKeyName("baeldung-key-pair");
CreateKeyPairResult createKeyPairResult = ec2Client.createKeyPair(createKeyPairRequest);
@ -79,11 +77,11 @@ public class EC2Application {
.getKeyPair()
.getKeyMaterial(); // make sure you keep it, the private key, Amazon doesn't store the private key
// 4) - See what key-pairs you've got
// See what key-pairs you've got
DescribeKeyPairsRequest describeKeyPairsRequest = new DescribeKeyPairsRequest();
DescribeKeyPairsResult describeKeyPairsResult = ec2Client.describeKeyPairs(describeKeyPairsRequest);
// 5) - Launch an Amazon Instance
// Launch an Amazon Instance
RunInstancesRequest runInstancesRequest = new RunInstancesRequest().withImageId("ami-97785bed") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AMIs.html | https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/usingsharedamis-finding.html
.withInstanceType("t2.micro") // https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html
.withMinCount(1)
@ -91,9 +89,15 @@ public class EC2Application {
.withKeyName("baeldung-key-pair") // optional - if not present, can't connect to instance
.withSecurityGroups("BaeldungSecurityGroup");
ec2Client.runInstances(runInstancesRequest);
String yourInstanceId = ec2Client.runInstances(runInstancesRequest).getReservation().getInstances().get(0).getInstanceId();
// 6) Monitor Instances
// Start an Instance
StartInstancesRequest startInstancesRequest = new StartInstancesRequest()
.withInstanceIds(yourInstanceId);
ec2Client.startInstances(startInstancesRequest);
// Monitor Instances
MonitorInstancesRequest monitorInstancesRequest = new MonitorInstancesRequest()
.withInstanceIds(yourInstanceId);
@ -104,14 +108,14 @@ public class EC2Application {
ec2Client.unmonitorInstances(unmonitorInstancesRequest);
// 7) - Reboot an Instance
// Reboot an Instance
RebootInstancesRequest rebootInstancesRequest = new RebootInstancesRequest()
.withInstanceIds(yourInstanceId);
ec2Client.rebootInstances(rebootInstancesRequest);
// 8) - Stop an Instance
// Stop an Instance
StopInstancesRequest stopInstancesRequest = new StopInstancesRequest()
.withInstanceIds(yourInstanceId);
@ -121,13 +125,7 @@ public class EC2Application {
.getPreviousState()
.getName();
// 9) - Start an Instance
StartInstancesRequest startInstancesRequest = new StartInstancesRequest()
.withInstanceIds("instance-id");
ec2Client.startInstances(startInstancesRequest);
// 10) - Describe an Instance
// Describe an Instance
DescribeInstancesRequest describeInstancesRequest = new DescribeInstancesRequest();
DescribeInstancesResult response = ec2Client.describeInstances(describeInstancesRequest);
System.out.println(response.getReservations()

View File

@ -8,6 +8,6 @@ repositories {
}
dependencies {
compile 'org.codehaus.groovy:groovy-all:2.5.0-alpha-1'
compile 'org.codehaus.groovy:groovy-all:2.4.13'
testCompile 'org.spockframework:spock-core:1.1-groovy-2.4'
}

View File

@ -23,17 +23,17 @@
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy</artifactId>
<version>2.5.0-alpha-1</version>
<version>2.4.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.5.0-alpha-1</version>
<version>2.4.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-sql</artifactId>
<version>2.5.0-alpha-1</version>
<version>2.4.13</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>

View File

@ -1,6 +1,5 @@
package com.baeldung.json
import groovy.json.JsonGenerator
import groovy.json.JsonOutput
import groovy.json.JsonParserType
import groovy.json.JsonSlurper
@ -21,14 +20,6 @@ class JsonParser {
JsonOutput.toJson(account)
}
String toJson(Account account, String dateFormat, String... fieldsToExclude) {
JsonGenerator generator = new JsonGenerator.Options()
.dateFormat(dateFormat)
.excludeFieldsByName(fieldsToExclude)
.build()
generator.toJson(account)
}
String prettyfy(String json) {
JsonOutput.prettyPrint(json)
}

View File

@ -52,20 +52,6 @@ class JsonParserTest extends Specification {
json == '{"value":15.6,"createdAt":"2018-01-01T00:00:00+0000","id":"123"}'
}
def 'Should parse to Json given an Account object, a date format and fields to exclude' () {
given:
Account account = new Account(
id: '123',
value: 15.6,
createdAt: new SimpleDateFormat('MM/dd/yyyy').parse('01/01/2018')
)
when:
def json = jsonParser.toJson(account, 'MM/dd/yyyy', 'value')
then:
json
json == '{"createdAt":"01/01/2018","id":"123"}'
}
def 'Should prettify given a json string' () {
given:
String json = '{"value":15.6,"createdAt":"01/01/2018","id":"123456"}'

63
jersey/pom.xml Normal file
View File

@ -0,0 +1,63 @@
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>jersey</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<properties>
<jersey.version>2.26</jersey.version>
<slf4j.version>1.7.25</slf4j.version>
<maven-war-plugin.version>3.2.0</maven-war-plugin.version>
</properties>
<build>
<finalName>jersey</finalName>
<plugins>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.bundles</groupId>
<artifactId>jaxrs-ri</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,45 @@
package com.baeldung.jersey.client;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Response;
import org.glassfish.jersey.client.ClientConfig;
import com.baeldung.jersey.client.filter.RequestClientFilter;
import com.baeldung.jersey.client.filter.ResponseClientFilter;
import com.baeldung.jersey.client.interceptor.RequestClientWriterInterceptor;
public class JerseyClient {
private static final String URI_GREETINGS = "http://localhost:8080/jersey/greetings";
public static String getHelloGreeting() {
return createClient().target(URI_GREETINGS)
.request()
.get(String.class);
}
public static String getHiGreeting() {
return createClient().target(URI_GREETINGS + "/hi")
.request()
.get(String.class);
}
public static Response getCustomGreeting() {
return createClient().target(URI_GREETINGS + "/custom")
.request()
.post(Entity.text("custom"));
}
private static Client createClient() {
ClientConfig config = new ClientConfig();
config.register(RequestClientFilter.class);
config.register(ResponseClientFilter.class);
config.register(RequestClientWriterInterceptor.class);
return ClientBuilder.newClient(config);
}
}

View File

@ -0,0 +1,24 @@
package com.baeldung.jersey.client.filter;
import java.io.IOException;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import javax.ws.rs.ext.Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Provider
public class RequestClientFilter implements ClientRequestFilter {
private static final Logger LOG = LoggerFactory.getLogger(RequestClientFilter.class);
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
LOG.info("Request client filter");
requestContext.setProperty("test", "test client request filter");
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.jersey.client.filter;
import java.io.IOException;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientResponseContext;
import javax.ws.rs.client.ClientResponseFilter;
import javax.ws.rs.ext.Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Provider
public class ResponseClientFilter implements ClientResponseFilter {
private static final Logger LOG = LoggerFactory.getLogger(ResponseClientFilter.class);
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
LOG.info("Response client filter");
responseContext.getHeaders()
.add("X-Test-Client", "Test response client filter");
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.jersey.client.interceptor;
import java.io.IOException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.WriterInterceptor;
import javax.ws.rs.ext.WriterInterceptorContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Provider
public class RequestClientWriterInterceptor implements WriterInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(RequestClientWriterInterceptor.class);
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException, WebApplicationException {
LOG.info("request writer interceptor in the client side");
context.getOutputStream()
.write(("Message added in the writer interceptor in the client side").getBytes());
context.proceed();
}
}

View File

@ -0,0 +1,32 @@
package com.baeldung.jersey.server;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import com.baeldung.jersey.server.config.HelloBinding;
@Path("/greetings")
public class Greetings {
@GET
@HelloBinding
public String getHelloGreeting() {
return "hello";
}
@GET
@Path("/hi")
public String getHiGreeting() {
return "hi";
}
@POST
@Path("/custom")
public Response getCustomGreeting(String name) {
return Response.status(Status.OK.getStatusCode())
.build();
}
}

View File

@ -0,0 +1,11 @@
package com.baeldung.jersey.server.config;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import javax.ws.rs.NameBinding;
@NameBinding
@Retention(RetentionPolicy.RUNTIME)
public @interface HelloBinding {
}

View File

@ -0,0 +1,31 @@
package com.baeldung.jersey.server.config;
import javax.ws.rs.container.DynamicFeature;
import javax.ws.rs.container.ResourceInfo;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baeldung.jersey.server.Greetings;
import com.baeldung.jersey.server.filter.ResponseServerFilter;
@Provider
public class HelloDynamicBinding implements DynamicFeature {
private static final Logger LOG = LoggerFactory.getLogger(HelloDynamicBinding.class);
@Override
public void configure(ResourceInfo resourceInfo, FeatureContext context) {
LOG.info("Hello dynamic binding");
if (Greetings.class.equals(resourceInfo.getResourceClass()) && resourceInfo.getResourceMethod()
.getName()
.contains("HiGreeting")) {
context.register(ResponseServerFilter.class);
}
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.jersey.server.config;
import javax.ws.rs.ApplicationPath;
import org.glassfish.jersey.server.ResourceConfig;
@ApplicationPath("/*")
public class ServerConfig extends ResourceConfig {
public ServerConfig() {
packages("com.baeldung.jersey.server");
}
}

View File

@ -0,0 +1,27 @@
package com.baeldung.jersey.server.filter;
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.ext.Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Provider
@PreMatching
public class PrematchingRequestFilter implements ContainerRequestFilter {
private static final Logger LOG = LoggerFactory.getLogger(PrematchingRequestFilter.class);
@Override
public void filter(ContainerRequestContext ctx) throws IOException {
LOG.info("prematching filter");
if (ctx.getMethod()
.equals("DELETE")) {
LOG.info("\"Deleting request");
}
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.jersey.server.filter;
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ResponseServerFilter implements ContainerResponseFilter {
private static final Logger LOG = LoggerFactory.getLogger(ResponseServerFilter.class);
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
LOG.info("Response server filter");
responseContext.getHeaders()
.add("X-Test", "Filter test");
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.jersey.server.filter;
import java.io.IOException;
import javax.annotation.Priority;
import javax.ws.rs.Priorities;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.Provider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baeldung.jersey.server.config.HelloBinding;
@Provider
@Priority(Priorities.AUTHORIZATION)
@HelloBinding
public class RestrictedOperationsRequestFilter implements ContainerRequestFilter {
private static final Logger LOG = LoggerFactory.getLogger(RestrictedOperationsRequestFilter.class);
@Override
public void filter(ContainerRequestContext ctx) throws IOException {
LOG.info("Restricted operations filter");
if (ctx.getLanguage() != null && "EN".equals(ctx.getLanguage()
.getLanguage())) {
LOG.info("Aborting request");
ctx.abortWith(Response.status(Response.Status.FORBIDDEN)
.entity("Cannot access")
.build());
}
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.jersey.server.interceptor;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.stream.Collectors;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.ReaderInterceptor;
import javax.ws.rs.ext.ReaderInterceptorContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Provider
public class RequestServerReaderInterceptor implements ReaderInterceptor {
private static final Logger LOG = LoggerFactory.getLogger(RequestServerReaderInterceptor.class);
@Override
public Object aroundReadFrom(ReaderInterceptorContext context) throws IOException, WebApplicationException {
LOG.info("Request reader interceptor in the server side");
InputStream is = context.getInputStream();
String body = new BufferedReader(new InputStreamReader(is)).lines()
.collect(Collectors.joining("\n"));
context.setInputStream(new ByteArrayInputStream((body + " message added in server reader interceptor").getBytes()));
return context.proceed();
}
}

View File

@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern> %date [%thread] %-5level %logger{36} - %message%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,35 @@
package com.baeldung.jersey.client;
import javax.ws.rs.core.Response;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@Ignore
public class JerseyClientTest {
private static int HTTP_OK = 200;
@Test
public void givenGreetingResource_whenCallingHelloGreeting_thenHelloReturned() {
String response = JerseyClient.getHelloGreeting();
Assert.assertEquals("hello", response);
}
@Test
public void givenGreetingResource_whenCallingHiGreeting_thenHiReturned() {
String response = JerseyClient.getHiGreeting();
Assert.assertEquals("hi", response);
}
@Test
public void givenGreetingResource_whenCallingCustomGreeting_thenCustomGreetingReturned() {
Response response = JerseyClient.getCustomGreeting();
Assert.assertEquals(HTTP_OK, response.getStatus());
}
}

View File

@ -39,6 +39,8 @@
<module>apache-fop</module>
<module>apache-poi</module>
<module>apache-thrift</module>
<module>apache-curator</module>
<module>apache-zookeeper</module>
<module>autovalue</module>
<module>axon</module>
<module>bootique</module>
@ -278,8 +280,9 @@
<module>lucene</module>
<module>vraptor</module>
<module>persistence-modules/java-cockroachdb</module>
<module>jersey</module>
<module>java-spi</module>
</modules>
</modules>
<dependencies>
<!-- logging -->
@ -382,4 +385,4 @@
</extension>
</extensions>
</build>
</project>
</project>

View File

@ -0,0 +1,152 @@
package org.baeldung.hamcrest;
import org.junit.Test;
import java.math.BigDecimal;
import java.time.LocalDate;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.closeTo;
public class HamcrestNumberUnitTest {
@Test
public void givenADouble_whenCloseTo_thenCorrect() {
double actual = 1.3;
double operand = 1;
double error = 0.5;
assertThat(actual, is(closeTo(operand, error)));
}
@Test
public void givenADouble_whenNotCloseTo_thenCorrect() {
double actual = 1.6;
double operand = 1;
double error = 0.5;
assertThat(actual, is(not(closeTo(operand, error))));
}
@Test
public void givenABigDecimal_whenCloseTo_thenCorrect() {
BigDecimal actual = new BigDecimal("1.0003");
BigDecimal operand = new BigDecimal("1");
BigDecimal error = new BigDecimal("0.0005");
assertThat(actual, is(closeTo(operand, error)));
}
@Test
public void givenABigDecimal_whenNotCloseTo_thenCorrect() {
BigDecimal actual = new BigDecimal("1.0006");
BigDecimal operand = new BigDecimal("1");
BigDecimal error = new BigDecimal("0.0005");
assertThat(actual, is(not(closeTo(operand, error))));
}
@Test
public void given7_whenGreaterThan5_thenCorrect() {
Integer seven = 7;
Integer five = 5;
assertThat(seven, is(greaterThan(five)));
}
@Test
public void given7_whenGreaterThanOrEqualTo5_thenCorrect() {
Integer seven = 7;
Integer five = 5;
assertThat(seven, is(greaterThanOrEqualTo(five)));
}
@Test
public void given5_whenGreaterThanOrEqualTo5_thenCorrect() {
Integer five = 5;
assertThat(five, is(greaterThanOrEqualTo(five)));
}
@Test
public void given3_whenLessThan5_thenCorrect() {
Integer three = 3;
Integer five = 5;
assertThat(three, is(lessThan(five)));
}
@Test
public void given3_whenLessThanOrEqualTo5_thenCorrect() {
Integer three = 3;
Integer five = 5;
assertThat(three, is(lessThanOrEqualTo(five)));
}
@Test
public void given5_whenLessThanOrEqualTo5_thenCorrect() {
Integer five = 5;
assertThat(five, is(lessThanOrEqualTo(five)));
}
@Test
public void givenBenjamin_whenGreaterThanAmanda_thenCorrect() {
String amanda = "Amanda";
String benjamin = "Benjamin";
assertThat(benjamin, is(greaterThan(amanda)));
}
@Test
public void givenAmanda_whenLessThanBenajmin_thenCorrect() {
String amanda = "Amanda";
String benjamin = "Benjamin";
assertThat(amanda, is(lessThan(benjamin)));
}
@Test
public void givenToday_whenGreaterThanYesterday_thenCorrect() {
LocalDate today = LocalDate.now();
LocalDate yesterday = today.minusDays(1);
assertThat(today, is(greaterThan(yesterday)));
}
@Test
public void givenToday_whenLessThanTomorrow_thenCorrect() {
LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plusDays(1);
assertThat(today, is(lessThan(tomorrow)));
}
@Test
public void givenAmanda_whenOlderThanBenjamin_thenCorrect() {
Person amanda = new Person("Amanda", 20);
Person benjamin = new Person("Benjamin", 18);
assertThat(amanda, is(greaterThan(benjamin)));
}
@Test
public void givenBenjamin_whenYoungerThanAmanda_thenCorrect() {
Person amanda = new Person("Amanda", 20);
Person benjamin = new Person("Benjamin", 18);
assertThat(benjamin, is(lessThan(amanda)));
}
class Person implements Comparable<Person> {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
@Override
public int compareTo(Person o) {
if (this.age == o.getAge()) return 0;
if (this.age > o.age) return 1;
else return -1;
}
}
}