Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-16822

This commit is contained in:
amit2103 2019-08-29 00:08:06 +05:30
commit 71b8657f76
75 changed files with 1194 additions and 107 deletions

View File

@ -0,0 +1,43 @@
package com.baeldung.exceptions;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class CheckedUncheckedExceptions {
public static void checkedExceptionWithThrows() throws FileNotFoundException {
File file = new File("not_existing_file.txt");
FileInputStream stream = new FileInputStream(file);
}
public static void checkedExceptionWithTryCatch() {
File file = new File("not_existing_file.txt");
try {
FileInputStream stream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static int divideByZero() {
int numerator = 1;
int denominator = 0;
return numerator / denominator;
}
public static void checkFile(String fileName) throws IncorrectFileNameException {
if (fileName == null || fileName.isEmpty()) {
throw new NullOrEmptyException("The filename is null.");
}
if (!isCorrectFileName(fileName)) {
throw new IncorrectFileNameException("Incorrect filename : " + fileName);
}
}
private static boolean isCorrectFileName(String fileName) {
if (fileName.equals("wrongFileName.txt"))
return false;
else
return true;
}
}

View File

@ -0,0 +1,13 @@
package com.baeldung.exceptions;
public class IncorrectFileNameException extends Exception {
private static final long serialVersionUID = 1L;
public IncorrectFileNameException(String errorMessage) {
super(errorMessage);
}
public IncorrectFileNameException(String errorMessage, Throwable thr) {
super(errorMessage, thr);
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.exceptions;
public class NullOrEmptyException extends RuntimeException {
private static final long serialVersionUID = 1L;
public NullOrEmptyException(String errorMessage) {
super(errorMessage);
}
public NullOrEmptyException(String errorMessage, Throwable thr) {
super(errorMessage, thr);
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.exceptions;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.FileNotFoundException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* Tests the {@link CheckedUncheckedExceptions}.
*/
public class CheckedUncheckedExceptionsUnitTest {
@Test
public void whenFileNotExist_thenThrowException() {
assertThrows(FileNotFoundException.class, () -> {
CheckedUncheckedExceptions.checkedExceptionWithThrows();
});
}
@Test
public void whenTryCatchExcetpion_thenSuccess() {
try {
CheckedUncheckedExceptions.checkedExceptionWithTryCatch();
} catch (Exception e) {
Assertions.fail(e.getMessage());
}
}
@Test
public void whenDivideByZero_thenThrowException() {
assertThrows(ArithmeticException.class, () -> {
CheckedUncheckedExceptions.divideByZero();
});
}
@Test
public void whenInvalidFile_thenThrowException() {
assertThrows(IncorrectFileNameException.class, () -> {
CheckedUncheckedExceptions.checkFile("wrongFileName.txt");
});
}
@Test
public void whenNullOrEmptyFile_thenThrowException() {
assertThrows(NullOrEmptyException.class, () -> {
CheckedUncheckedExceptions.checkFile(null);
});
assertThrows(NullOrEmptyException.class, () -> {
CheckedUncheckedExceptions.checkFile("");
});
}
}

6
gradle-5/.gitignore vendored
View File

@ -1,6 +0,0 @@
/build/
/bin/
.settings
.gradle
.project
.classpath

26
java-blockchain/.gitignore vendored Normal file
View File

@ -0,0 +1,26 @@
*.class
0.*
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
.resourceCache
# Packaged files #
*.jar
*.war
*.ear
# Files generated by integration tests
*.txt
backup-pom.xml
/bin/
/temp
#IntelliJ specific
.idea/
*.iml

View File

@ -0,0 +1,6 @@
=========
## Basic Implementation of Blockchian in Java
### Relevant Articles:
- []()

40
java-blockchain/pom.xml Normal file
View File

@ -0,0 +1,40 @@
<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>
<groupId>com.baeldung.blockchain</groupId>
<artifactId>java-blockchain</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>java-blockchain</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-java</relativePath>
</parent>
<build>
<finalName>java-blockchain</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
</project>

View File

@ -0,0 +1,65 @@
package com.baeldung.blockchain;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Block {
private static Logger logger = Logger.getLogger(Block.class.getName());
private String hash;
private String previousHash;
private String data;
private long timeStamp;
private int nonce;
public Block(String data, String previousHash) {
this.data = data;
this.previousHash = previousHash;
this.timeStamp = new Date().getTime();
this.hash = calculateBlockHash();
}
public String mineBlock(int prefix) {
String prefixString = new String(new char[prefix]).replace('\0', '0');
while (!hash.substring(0, prefix)
.equals(prefixString)) {
nonce++;
hash = calculateBlockHash();
}
return hash;
}
public String calculateBlockHash() {
String dataToHash = previousHash + Long.toString(timeStamp) + Integer.toString(nonce) + data;
MessageDigest digest = null;
byte[] bytes = null;
try {
digest = MessageDigest.getInstance("SHA-256");
bytes = digest.digest(dataToHash.getBytes("UTF-8"));
} catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) {
logger.log(Level.SEVERE, ex.getMessage());
}
StringBuffer buffer = new StringBuffer();
for (byte b : bytes) {
buffer.append(String.format("%02x", b));
}
return buffer.toString();
}
public String getHash() {
return this.hash;
}
public String getPreviousHash() {
return this.previousHash;
}
public void setData(String data) {
this.data = data;
}
}

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>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,67 @@
package com.baeldung.blockchain;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
public class BlockchainUnitTest {
public static List<Block> blockchain = new ArrayList<Block>();
public static int prefix = 4;
public static String prefixString = new String(new char[prefix]).replace('\0', '0');
@BeforeClass
public static void setUp() {
Block genesisBlock = new Block("The is the Genesis Block.", "0");
genesisBlock.mineBlock(prefix);
blockchain.add(genesisBlock);
Block firstBlock = new Block("The is the First Block.", genesisBlock.getHash());
firstBlock.mineBlock(prefix);
blockchain.add(firstBlock);
}
@Test
public void givenBlockchain_whenNewBlockAdded_thenSuccess() {
Block newBlock = new Block("The is a New Block.", blockchain.get(blockchain.size() - 1)
.getHash());
newBlock.mineBlock(prefix);
assertTrue(newBlock.getHash()
.substring(0, prefix)
.equals(prefixString));
blockchain.add(newBlock);
}
@Test
public void givenBlockchain_whenValidated_thenSuccess() {
boolean flag = true;
for (int i = 0; i < blockchain.size(); i++) {
String previousHash = i == 0 ? "0"
: blockchain.get(i - 1)
.getHash();
flag = blockchain.get(i)
.getHash()
.equals(blockchain.get(i)
.calculateBlockHash())
&& previousHash.equals(blockchain.get(i)
.getPreviousHash())
&& blockchain.get(i)
.getHash()
.substring(0, prefix)
.equals(prefixString);
if (!flag)
break;
}
assertTrue(flag);
}
@AfterClass
public static void tearDown() {
blockchain.clear();
}
}

View File

@ -0,0 +1,13 @@
*.class
#folders#
/target
/neoDb*
/data
/src/main/webapp/WEB-INF/classes
*/META-INF/*
# Packaged files #
*.jar
*.war
*.ear

View File

@ -1,6 +1,6 @@
package com.baeldung.jdi;
public class JDIExample {
public class JDIExampleDebuggee {
public static void main(String[] args) {
String jpda = "Java Platform Debugger Architecture";

View File

@ -123,7 +123,7 @@ public class JDIExampleDebugger {
public static void main(String[] args) throws Exception {
JDIExampleDebugger debuggerInstance = new JDIExampleDebugger();
debuggerInstance.setDebugClass(JDIExample.class);
debuggerInstance.setDebugClass(JDIExampleDebuggee.class);
int[] breakPoints = {6, 9};
debuggerInstance.setBreakPointLines(breakPoints);
VirtualMachine vm = null;

View File

@ -0,0 +1,76 @@
package com.baeldung.string.streamtokenizer;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class StreamTokenizerDemo {
private static final String INPUT_FILE = "/stream-tokenizer-example.txt";
private static final int QUOTE_CHARACTER = '\'';
private static final int DOUBLE_QUOTE_CHARACTER = '"';
public static List<Object> streamTokenizerWithDefaultConfiguration(Reader reader) throws IOException {
StreamTokenizer streamTokenizer = new StreamTokenizer(reader);
List<Object> tokens = new ArrayList<>();
int currentToken = streamTokenizer.nextToken();
while (currentToken != StreamTokenizer.TT_EOF) {
if (streamTokenizer.ttype == StreamTokenizer.TT_NUMBER) {
tokens.add(streamTokenizer.nval);
} else if (streamTokenizer.ttype == StreamTokenizer.TT_WORD
|| streamTokenizer.ttype == QUOTE_CHARACTER
|| streamTokenizer.ttype == DOUBLE_QUOTE_CHARACTER) {
tokens.add(streamTokenizer.sval);
} else {
tokens.add((char) currentToken);
}
currentToken = streamTokenizer.nextToken();
}
return tokens;
}
public static List<Object> streamTokenizerWithCustomConfiguration(Reader reader) throws IOException {
StreamTokenizer streamTokenizer = new StreamTokenizer(reader);
List<Object> tokens = new ArrayList<>();
streamTokenizer.wordChars('!', '-');
streamTokenizer.ordinaryChar('/');
streamTokenizer.commentChar('#');
streamTokenizer.eolIsSignificant(true);
int currentToken = streamTokenizer.nextToken();
while (currentToken != StreamTokenizer.TT_EOF) {
if (streamTokenizer.ttype == StreamTokenizer.TT_NUMBER) {
tokens.add(streamTokenizer.nval);
} else if (streamTokenizer.ttype == StreamTokenizer.TT_WORD
|| streamTokenizer.ttype == QUOTE_CHARACTER
|| streamTokenizer.ttype == DOUBLE_QUOTE_CHARACTER) {
tokens.add(streamTokenizer.sval);
} else {
tokens.add((char) currentToken);
}
currentToken = streamTokenizer.nextToken();
}
return tokens;
}
public static Reader createReaderFromFile() throws FileNotFoundException {
String inputFile = StreamTokenizerDemo.class.getResource(INPUT_FILE).getFile();
return new FileReader(inputFile);
}
public static void main(String[] args) throws IOException {
List<Object> tokens1 = streamTokenizerWithDefaultConfiguration(createReaderFromFile());
List<Object> tokens2 = streamTokenizerWithCustomConfiguration(createReaderFromFile());
System.out.println(tokens1);
System.out.println(tokens2);
}
}

View File

@ -0,0 +1,3 @@
3 quick brown foxes jump over the "lazy" dog!
#test1
//test2

View File

@ -0,0 +1,34 @@
package com.baeldung.string.streamtokenizer;
import org.junit.Test;
import java.io.IOException;
import java.io.Reader;
import java.util.Arrays;
import java.util.List;
import static org.junit.Assert.assertArrayEquals;
public class StreamTokenizerDemoUnitTest {
@Test
public void whenStreamTokenizerWithDefaultConfigurationIsCalled_ThenCorrectTokensAreReturned() throws IOException {
Reader reader = StreamTokenizerDemo.createReaderFromFile();
List<Object> expectedTokens = Arrays.asList(3.0, "quick", "brown", "foxes", "jump", "over", "the", "lazy", "dog", '!', '#', "test1");
List<Object> actualTokens = StreamTokenizerDemo.streamTokenizerWithDefaultConfiguration(reader);
assertArrayEquals(expectedTokens.toArray(), actualTokens.toArray());
}
@Test
public void whenStreamTokenizerWithCustomConfigurationIsCalled_ThenCorrectTokensAreReturned() throws IOException {
Reader reader = StreamTokenizerDemo.createReaderFromFile();
List<Object> expectedTokens = Arrays.asList(3.0, "quick", "brown", "foxes", "jump", "over", "the", "\"lazy\"", "dog!", '\n', '\n', '/', '/', "test2");
List<Object> actualTokens = StreamTokenizerDemo.streamTokenizerWithCustomConfiguration(reader);
assertArrayEquals(expectedTokens.toArray(), actualTokens.toArray());
}
}

View File

@ -32,7 +32,7 @@
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-servlet</artifactId>
<version>${jersey.version}</version>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
@ -44,6 +44,21 @@
<artifactId>jersey-bean-validation</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.security</groupId>
<artifactId>oauth1-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.security</groupId>
<artifactId>oauth2-client</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-sse</artifactId>
<version>${jersey.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>

View File

@ -0,0 +1,189 @@
package com.baeldung.jersey.client;
import com.baeldung.jersey.client.filter.AddHeaderOnRequestFilter;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import org.glassfish.jersey.client.oauth1.AccessToken;
import org.glassfish.jersey.client.oauth1.ConsumerCredentials;
import org.glassfish.jersey.client.oauth1.OAuth1ClientSupport;
import org.glassfish.jersey.client.oauth2.OAuth2ClientSupport;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Feature;
import javax.ws.rs.core.Response;
import javax.ws.rs.sse.InboundSseEvent;
import javax.ws.rs.sse.SseEventSource;
import static org.glassfish.jersey.client.authentication.HttpAuthenticationFeature.*;
public class JerseyClientHeaders {
private static final String BEARER_CONSUMER_SECRET = "my-consumer-secret";
private static final String BEARER_ACCESS_TOKEN_SECRET = "my-access-token-secret";
private static final String TARGET = "http://localhost:9998/";
private static final String MAIN_RESOURCE = "echo-headers";
private static final String RESOURCE_AUTH_DIGEST = "digest";
private static String sseHeaderValue;
public static Response simpleHeader(String headerKey, String headerValue) {
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(TARGET);
WebTarget resourceWebTarget = webTarget.path(MAIN_RESOURCE);
Invocation.Builder invocationBuilder = resourceWebTarget.request();
invocationBuilder.header(headerKey, headerValue);
return invocationBuilder.get();
}
public static Response simpleHeaderFluently(String headerKey, String headerValue) {
Client client = ClientBuilder.newClient();
return client.target(TARGET)
.path(MAIN_RESOURCE)
.request()
.header(headerKey, headerValue)
.get();
}
public static Response basicAuthenticationAtClientLevel(String username, String password) {
//To simplify we removed de SSL/TLS protection, but it's required to have an encryption
// when using basic authentication schema as it's send only on Base64 encoding
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(username, password);
Client client = ClientBuilder.newBuilder().register(feature).build();
return client.target(TARGET)
.path(MAIN_RESOURCE)
.request()
.get();
}
public static Response basicAuthenticationAtRequestLevel(String username, String password) {
//To simplify we removed de SSL/TLS protection, but it's required to have an encryption
// when using basic authentication schema as it's send only on Base64 encoding
HttpAuthenticationFeature feature = HttpAuthenticationFeature.basicBuilder().build();
Client client = ClientBuilder.newBuilder().register(feature).build();
return client.target(TARGET)
.path(MAIN_RESOURCE)
.request()
.property(HTTP_AUTHENTICATION_BASIC_USERNAME, username)
.property(HTTP_AUTHENTICATION_BASIC_PASSWORD, password)
.get();
}
public static Response digestAuthenticationAtClientLevel(String username, String password) {
HttpAuthenticationFeature feature = HttpAuthenticationFeature.digest(username, password);
Client client = ClientBuilder.newBuilder().register(feature).build();
return client.target(TARGET)
.path(MAIN_RESOURCE)
.path(RESOURCE_AUTH_DIGEST)
.request()
.get();
}
public static Response digestAuthenticationAtRequestLevel(String username, String password) {
HttpAuthenticationFeature feature = HttpAuthenticationFeature.digest();
Client client = ClientBuilder.newBuilder().register(feature).build();
return client.target(TARGET)
.path(MAIN_RESOURCE)
.path(RESOURCE_AUTH_DIGEST)
.request()
.property(HTTP_AUTHENTICATION_DIGEST_USERNAME, username)
.property(HTTP_AUTHENTICATION_DIGEST_PASSWORD, password)
.get();
}
public static Response bearerAuthenticationWithOAuth1AtClientLevel(String token, String consumerKey) {
ConsumerCredentials consumerCredential = new ConsumerCredentials(consumerKey, BEARER_CONSUMER_SECRET);
AccessToken accessToken = new AccessToken(token, BEARER_ACCESS_TOKEN_SECRET);
Feature feature = OAuth1ClientSupport
.builder(consumerCredential)
.feature()
.accessToken(accessToken)
.build();
Client client = ClientBuilder.newBuilder().register(feature).build();
return client.target(TARGET)
.path(MAIN_RESOURCE)
.request()
.get();
}
public static Response bearerAuthenticationWithOAuth1AtRequestLevel(String token, String consumerKey) {
ConsumerCredentials consumerCredential = new ConsumerCredentials(consumerKey, BEARER_CONSUMER_SECRET);
AccessToken accessToken = new AccessToken(token, BEARER_ACCESS_TOKEN_SECRET);
Feature feature = OAuth1ClientSupport
.builder(consumerCredential)
.feature()
.build();
Client client = ClientBuilder.newBuilder().register(feature).build();
return client.target(TARGET)
.path(MAIN_RESOURCE)
.request()
.property(OAuth1ClientSupport.OAUTH_PROPERTY_ACCESS_TOKEN, accessToken)
.get();
}
public static Response bearerAuthenticationWithOAuth2AtClientLevel(String token) {
Feature feature = OAuth2ClientSupport.feature(token);
Client client = ClientBuilder.newBuilder().register(feature).build();
return client.target(TARGET)
.path(MAIN_RESOURCE)
.request()
.get();
}
public static Response bearerAuthenticationWithOAuth2AtRequestLevel(String token, String otherToken) {
Feature feature = OAuth2ClientSupport.feature(token);
Client client = ClientBuilder.newBuilder().register(feature).build();
return client.target(TARGET)
.path(MAIN_RESOURCE)
.request()
.property(OAuth2ClientSupport.OAUTH2_PROPERTY_ACCESS_TOKEN, otherToken)
.get();
}
public static Response filter() {
Client client = ClientBuilder.newBuilder().register(AddHeaderOnRequestFilter.class).build();
return client.target(TARGET)
.path(MAIN_RESOURCE)
.request()
.get();
}
public static Response sendRestrictedHeaderThroughDefaultTransportConnector(String headerKey, String headerValue) {
Client client = ClientBuilder.newClient();
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
return client.target(TARGET)
.path(MAIN_RESOURCE)
.request()
.header(headerKey, headerValue)
.get();
}
public static String simpleSSEHeader() throws InterruptedException {
Client client = ClientBuilder.newBuilder()
.register(AddHeaderOnRequestFilter.class)
.build();
WebTarget webTarget = client.target(TARGET)
.path(MAIN_RESOURCE)
.path("events");
SseEventSource sseEventSource = SseEventSource.target(webTarget).build();
sseEventSource.register(JerseyClientHeaders::receiveEvent);
sseEventSource.open();
Thread.sleep(3_000);
sseEventSource.close();
return sseHeaderValue;
}
private static void receiveEvent(InboundSseEvent event) {
sseHeaderValue = event.readData();
}
}

View File

@ -0,0 +1,16 @@
package com.baeldung.jersey.client.filter;
import javax.ws.rs.client.ClientRequestContext;
import javax.ws.rs.client.ClientRequestFilter;
import java.io.IOException;
public class AddHeaderOnRequestFilter implements ClientRequestFilter {
public static final String FILTER_HEADER_VALUE = "filter-header-value";
public static final String FILTER_HEADER_KEY = "x-filter-header";
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
requestContext.getHeaders().add(FILTER_HEADER_KEY, FILTER_HEADER_VALUE);
}
}

View File

@ -0,0 +1,72 @@
package com.baeldung.jersey.server;
import com.baeldung.jersey.client.filter.AddHeaderOnRequestFilter;
import javax.annotation.security.RolesAllowed;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.*;
import javax.ws.rs.sse.OutboundSseEvent;
import javax.ws.rs.sse.Sse;
import javax.ws.rs.sse.SseEventSink;
@Path("/echo-headers")
public class EchoHeaders {
static final String REALM_KEY = "realm";
static final String REALM_VALUE = "Baeldung";
static final String QOP_KEY = "qop";
static final String QOP_VALUE = "auth";
static final String NONCE_KEY = "nonce";
static final String NONCE_VALUE = "dcd98b7102dd2f0e8b11d0f600bfb0c093";
static final String OPAQUE_KEY = "opaque";
static final String OPAQUE_VALUE = "5ccc069c403ebaf9f0171e9517f40e41";
static final String SSE_HEADER_KEY = "x-sse-header-key";
@Context
HttpHeaders headers;
@GET
public Response getHeadersBack() {
return echoHeaders();
}
@RolesAllowed("ADMIN")
@GET
@Path("/digest")
public Response getHeadersBackFromDigestAuthentication() {
// As the Digest authentication require some complex steps to work we'll simulate the process
// https://en.wikipedia.org/wiki/Digest_access_authentication#Example_with_explanation
if (headers.getHeaderString("authorization") == null) {
String authenticationRequired = "Digest " + REALM_KEY + "=\"" + REALM_VALUE + "\", " + QOP_KEY + "=\"" + QOP_VALUE + "\", " + NONCE_KEY + "=\"" + NONCE_VALUE + "\", " + OPAQUE_KEY + "=\"" + OPAQUE_VALUE + "\"";
return Response.status(Response.Status.UNAUTHORIZED)
.header("WWW-Authenticate", authenticationRequired)
.build();
} else {
return echoHeaders();
}
}
@GET
@Path("/events")
@Produces(MediaType.SERVER_SENT_EVENTS)
public void getServerSentEvents(@Context SseEventSink eventSink, @Context Sse sse) {
OutboundSseEvent event = sse.newEventBuilder()
.name("echo-headers")
.data(String.class, headers.getHeaderString(AddHeaderOnRequestFilter.FILTER_HEADER_KEY))
.build();
eventSink.send(event);
}
private Response echoHeaders() {
Response.ResponseBuilder responseBuilder = Response.noContent();
headers.getRequestHeaders()
.forEach((k, v) -> {
v.forEach(value -> responseBuilder.header(k, value));
});
return responseBuilder.build();
}
}

View File

@ -0,0 +1,197 @@
package com.baeldung.jersey.server;
import com.baeldung.jersey.client.JerseyClientHeaders;
import com.baeldung.jersey.client.filter.AddHeaderOnRequestFilter;
import org.glassfish.jersey.media.sse.SseFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.test.JerseyTest;
import org.junit.Test;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class EchoHeadersUnitTest extends JerseyTest {
private static final String SIMPLE_HEADER_KEY = "my-header-key";
private static final String SIMPLE_HEADER_VALUE = "my-header-value";
private static final String USERNAME = "baeldung";
private static final String PASSWORD = "super-secret";
private static final String AUTHORIZATION_HEADER_KEY = "authorization";
private static final String BEARER_TOKEN_VALUE = "my-token";
private static final String BEARER_CONSUMER_KEY_VALUE = "my-consumer-key";
private static final String BEARER_REQUEST_TOKEN_VALUE = "my-request-token";
@Test
public void whenCallingSimpleHeader_thenHeadersReturnedBack() {
Response response = JerseyClientHeaders.simpleHeader(SIMPLE_HEADER_KEY, SIMPLE_HEADER_VALUE);
assertEquals(response.getHeaderString(SIMPLE_HEADER_KEY), SIMPLE_HEADER_VALUE);
}
@Test
public void whenCallingSimpleHeaderFluently_thenHeadersReturnedBack() {
Response response = JerseyClientHeaders.simpleHeaderFluently(SIMPLE_HEADER_KEY, SIMPLE_HEADER_VALUE);
assertEquals(response.getHeaderString(SIMPLE_HEADER_KEY), SIMPLE_HEADER_VALUE);
}
@Test
public void whenCallingBasicAuthenticationAtClientLevel_thenHeadersReturnedBack() {
Response response = JerseyClientHeaders.basicAuthenticationAtClientLevel(USERNAME, PASSWORD);
assertBasicAuthenticationHeaders(response);
}
@Test
public void whenCallingBasicAuthenticationAtRequestLevel_thenHeadersReturnedBack() {
Response response = JerseyClientHeaders.basicAuthenticationAtRequestLevel(USERNAME, PASSWORD);
assertBasicAuthenticationHeaders(response);
}
@Test
public void whenCallingDigestAuthenticationAtClientLevel_thenHeadersReturnedBack() {
Response response = JerseyClientHeaders.digestAuthenticationAtClientLevel(USERNAME, PASSWORD);
Map<String, String> subHeadersMap = parseAuthenticationSubHeader(response, 7);
assertDigestAuthenticationHeaders(subHeadersMap);
}
@Test
public void whenCallingDigestAuthenticationAtRequestLevel_thenHeadersReturnedBack() {
Response response = JerseyClientHeaders.digestAuthenticationAtRequestLevel(USERNAME, PASSWORD);
Map<String, String> subHeadersMap = parseAuthenticationSubHeader(response, 7);
assertDigestAuthenticationHeaders(subHeadersMap);
}
@Test
public void whenCallingBearerAuthenticationWithOAuth1AtClientLevel_thenHeadersReturnedBack() {
Response response = JerseyClientHeaders.bearerAuthenticationWithOAuth1AtClientLevel(BEARER_TOKEN_VALUE, BEARER_CONSUMER_KEY_VALUE);
Map<String, String> subHeadersMap = parseAuthenticationSubHeader(response, 6);
assertBearerAuthenticationHeaders(subHeadersMap);
}
@Test
public void whenCallingBearerAuthenticationWithOAuth1AtRequestLevel_thenHeadersReturnedBack() {
Response response = JerseyClientHeaders.bearerAuthenticationWithOAuth1AtRequestLevel(BEARER_TOKEN_VALUE, BEARER_CONSUMER_KEY_VALUE);
Map<String, String> subHeadersMap = parseAuthenticationSubHeader(response, 6);
assertBearerAuthenticationHeaders(subHeadersMap);
}
@Test
public void whenCallingBearerAuthenticationWithOAuth2AtClientLevel_thenHeadersReturnedBack() {
Response response = JerseyClientHeaders.bearerAuthenticationWithOAuth2AtClientLevel(BEARER_TOKEN_VALUE);
assertEquals("Bearer " + BEARER_TOKEN_VALUE, response.getHeaderString(AUTHORIZATION_HEADER_KEY));
}
@Test
public void whenCallingBearerAuthenticationWithOAuth2AtRequestLevel_thenHeadersReturnedBack() {
Response response = JerseyClientHeaders.bearerAuthenticationWithOAuth2AtRequestLevel(BEARER_TOKEN_VALUE, BEARER_REQUEST_TOKEN_VALUE);
assertEquals("Bearer " + BEARER_REQUEST_TOKEN_VALUE, response.getHeaderString(AUTHORIZATION_HEADER_KEY));
}
@Test
public void whenCallingFilter_thenHeadersReturnedBack() {
Response response = JerseyClientHeaders.filter();
assertEquals(AddHeaderOnRequestFilter.FILTER_HEADER_VALUE, response.getHeaderString(AddHeaderOnRequestFilter.FILTER_HEADER_KEY));
}
@Test
public void whenCallingSendRestrictedHeaderThroughDefaultTransportConnector_thenHeadersReturnedBack() {
Response response = JerseyClientHeaders.sendRestrictedHeaderThroughDefaultTransportConnector("keep-alive", "keep-alive-value");
assertEquals("keep-alive-value", response.getHeaderString("keep-alive"));
}
@Test
public void whenCallingSimpleSSEHeader_thenHeadersReturnedBack() throws InterruptedException {
String sseHeaderBackValue = JerseyClientHeaders.simpleSSEHeader();
assertEquals(AddHeaderOnRequestFilter.FILTER_HEADER_VALUE, sseHeaderBackValue);
}
private void assertBearerAuthenticationHeaders(Map<String, String> subHeadersMap) {
assertEquals(BEARER_TOKEN_VALUE, subHeadersMap.get("oauth_token"));
assertEquals(BEARER_CONSUMER_KEY_VALUE, subHeadersMap.get("oauth_consumer_key"));
assertNotNull(subHeadersMap.get("oauth_nonce"));
assertNotNull(subHeadersMap.get("oauth_signature"));
assertNotNull(subHeadersMap.get("oauth_callback"));
assertNotNull(subHeadersMap.get("oauth_signature_method"));
assertNotNull(subHeadersMap.get("oauth_version"));
assertNotNull(subHeadersMap.get("oauth_timestamp"));
}
private void assertDigestAuthenticationHeaders(Map<String, String> subHeadersMap) {
assertEquals(EchoHeaders.NONCE_VALUE, subHeadersMap.get(EchoHeaders.NONCE_KEY));
assertEquals(EchoHeaders.OPAQUE_VALUE, subHeadersMap.get(EchoHeaders.OPAQUE_KEY));
assertEquals(EchoHeaders.QOP_VALUE, subHeadersMap.get(EchoHeaders.QOP_KEY));
assertEquals(EchoHeaders.REALM_VALUE, subHeadersMap.get(EchoHeaders.REALM_KEY));
assertEquals(USERNAME, subHeadersMap.get("username"));
assertEquals("/echo-headers/digest", subHeadersMap.get("uri"));
assertNotNull(subHeadersMap.get("cnonce"));
assertEquals("00000001", subHeadersMap.get("nc"));
assertNotNull(subHeadersMap.get("response"));
}
private Map<String, String> parseAuthenticationSubHeader(Response response, int startAt) {
String authorizationHeader = response.getHeaderString(AUTHORIZATION_HEADER_KEY);
// The substring(startAt) is used to cut off the authentication schema part from the value returned.
String[] subHeadersKeyValue = authorizationHeader.substring(startAt).split(",");
Map<String, String> subHeadersMap = new HashMap<>();
for (String subHeader : subHeadersKeyValue) {
String[] keyValue = subHeader.split("=");
if (keyValue[1].startsWith("\"")) {
keyValue[1] = keyValue[1].substring(1, keyValue[1].length() - 1);
}
subHeadersMap.put(keyValue[0].trim(), keyValue[1].trim());
}
return subHeadersMap;
}
private void assertBasicAuthenticationHeaders(Response response) {
String base64Credentials = response.getHeaderString(AUTHORIZATION_HEADER_KEY);
// The substring(6) is used to cut the "Basic " part of the value returned,
// as it's used to indicates the authentication schema and does not belong to the credentials
byte[] credentials = Base64.getDecoder().decode(base64Credentials.substring(6));
String[] credentialsParsed = new String(credentials).split(":");
assertEquals(credentialsParsed[0], USERNAME);
assertEquals(credentialsParsed[1], PASSWORD);
}
@Override
protected Application configure() {
return new ResourceConfig()
.register(EchoHeaders.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
// We need this definition here, because if you are running
// the complete suit test the sendingRestrictedHeaderThroughDefaultTransportConnector_shouldReturnThanBack
// will fail if only defined on the client method, since the JerseyTest is created once.
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
}
}

View File

@ -1,3 +1,10 @@
### Relevant Articles:
- [Performance of Java Mapping Frameworks](http://www.baeldung.com/java-performance-mapping-frameworks)
### Running
To run the performance benchmarks:
1: `mvn clean install`
2: `java -jar target/benchmarks.jar`

View File

@ -1,10 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<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>
<groupId>com.baeldung</groupId>
<artifactId>performance-tests</artifactId>
<name>performance-tests</name>
<version>1.0</version>
<packaging>jar</packaging>
<parent>
<artifactId>parent-modules</artifactId>
@ -12,6 +13,8 @@
<version>1.0.0-SNAPSHOT</version>
</parent>
<name>performance-tests</name>
<dependencies>
<dependency>
<groupId>ma.glasnost.orika</groupId>
@ -32,7 +35,16 @@
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${mapstruct-jdk8.version}</version>
<optional>true</optional>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct-processor -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.2.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
@ -46,46 +58,141 @@
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator.version}</version>
<version>${jmh.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct-processor.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<!--
JMH version to use with this project.
-->
<jmh.version>1.21</jmh.version>
<orika.version>1.5.2</orika.version>
<dozer.version>5.5.1</dozer.version>
<dozer-jdk8-support.version>1.0.2</dozer-jdk8-support.version>
<mapstruct-jdk8.version>1.2.0.Final</mapstruct-jdk8.version>
<modelmapper.version>1.1.0</modelmapper.version>
<jmapper.version>1.6.0.1</jmapper.version>
<java.version>1.8</java.version>
<mapstruct-processor.version>1.2.0.Final</mapstruct-processor.version>
<jmh-core.version>1.21</jmh-core.version>
<jmh-generator.version>1.21</jmh-generator.version>
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
<!--
Java source/target to use for compilation.
-->
<javac.target>1.8</javac.target>
<!--
Name of the benchmark Uber-JAR to generate.
-->
<uberjar.name>benchmarks</uberjar.name>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<compilerVersion>${javac.target}</compilerVersion>
<source>${javac.target}</source>
<target>${javac.target}</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>${mapstruct-processor.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>${uberjar.name}</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>org.openjdk.jmh.Main</mainClass>
</transformer>
</transformers>
<filters>
<filter>
<!--
Shading signed JARs will fail without this.
http://stackoverflow.com/questions/999489/invalid-signature-file-when-attempting-to-run-a-jar
-->
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>2.5</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.1</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
</plugin>
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
</plugin>
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.3</version>
</plugin>
<plugin>
<artifactId>maven-source-plugin</artifactId>
<version>2.2.1</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -566,6 +566,7 @@
<module>oauth2-framework-impl</module>
<module>spring-boot-nashorn</module>
<module>java-blockchain</module>
</modules>
</profile>
@ -802,6 +803,7 @@
<module>spring-security-kerberos</module>
<module>spring-boot-nashorn</module>
<module>java-blockchain</module>
</modules>

View File

@ -86,6 +86,11 @@
<artifactId>javafaker</artifactId>
<version>0.18</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>
</dependencies>
<build>
@ -167,5 +172,6 @@
<jayway-rest-assured.version>2.9.0</jayway-rest-assured.version>
<kotlin.version>1.1.2</kotlin.version>
<start-class>com.baeldung.Spring5Application</start-class>
<httpclient.version>4.5.8</httpclient.version>
</properties>
</project>

View File

@ -1,7 +1,7 @@
package com.baeldung.web.controller;
package com.baeldung.requestresponsebody;
import com.baeldung.services.ExampleService;
import com.baeldung.transfer.ResponseTransfer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -10,7 +10,6 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.baeldung.transfer.LoginForm;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

View File

@ -1,4 +1,4 @@
package com.baeldung.transfer;
package com.baeldung.requestresponsebody;
public class LoginForm {
private String username;

View File

@ -1,4 +1,4 @@
package com.baeldung.transfer;
package com.baeldung.requestresponsebody;
public class ResponseTransfer {

View File

@ -1,8 +1,9 @@
package com.baeldung.services;
import com.baeldung.transfer.LoginForm;
import org.springframework.stereotype.Service;
import com.baeldung.requestresponsebody.LoginForm;
@Service
public class ExampleService {

View File

@ -17,9 +17,9 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.baeldung.SpringBootRestApplication;
import com.baeldung.requestresponsebody.ExamplePostController;
import com.baeldung.requestresponsebody.LoginForm;
import com.baeldung.services.ExampleService;
import com.baeldung.transfer.LoginForm;
import com.baeldung.web.controller.ExamplePostController;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootRestApplication.class)

View File

@ -18,9 +18,9 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.baeldung.SpringBootRestApplication;
import com.baeldung.requestresponsebody.ExamplePostController;
import com.baeldung.requestresponsebody.LoginForm;
import com.baeldung.services.ExampleService;
import com.baeldung.transfer.LoginForm;
import com.baeldung.web.controller.ExamplePostController;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SpringBootRestApplication.class)

View File

@ -1,4 +1,4 @@
package org.baeldung.app;
package com.baeldung.app;
import javax.servlet.Filter;
@ -12,9 +12,9 @@ import org.springframework.web.filter.DelegatingFilterProxy;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
@SpringBootApplication
@EnableJpaRepositories("org.baeldung.repository")
@ComponentScan("org.baeldung")
@EntityScan("org.baeldung.entity")
@EnableJpaRepositories("com.baeldung.repository")
@ComponentScan("com.baeldung")
@EntityScan("com.baeldung.entity")
public class App extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(App.class, args);

View File

@ -1,4 +1,4 @@
package org.baeldung.auditing;
package com.baeldung.auditing;
import org.springframework.boot.actuate.audit.AuditEvent;
import org.springframework.boot.actuate.security.AbstractAuthorizationAuditListener;

View File

@ -1,4 +1,4 @@
package org.baeldung.auditing;
package com.baeldung.auditing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

View File

@ -1,11 +1,12 @@
package org.baeldung.config;
package com.baeldung.config;
import org.baeldung.entity.Task;
import org.baeldung.repository.TaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.baeldung.entity.Task;
import com.baeldung.repository.TaskRepository;
@Component
public class DatabaseLoader implements CommandLineRunner {

View File

@ -1,4 +1,4 @@
package org.baeldung.config;
package com.baeldung.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

View File

@ -1,7 +1,5 @@
package org.baeldung.controller;
package com.baeldung.controller;
import org.baeldung.entity.Task;
import org.baeldung.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
@ -9,6 +7,9 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.baeldung.entity.Task;
import com.baeldung.service.TaskService;
@Controller
@RequestMapping("api/tasks")
public class TaskController {

View File

@ -1,4 +1,4 @@
package org.baeldung.entity;
package com.baeldung.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;

View File

@ -1,4 +1,4 @@
package org.baeldung.filter;
package com.baeldung.filter;
import java.io.IOException;

View File

@ -1,4 +1,4 @@
package org.baeldung.methodsecurity.annotation;
package com.baeldung.methodsecurity.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;

View File

@ -1,4 +1,4 @@
package org.baeldung.methodsecurity.config;
package com.baeldung.methodsecurity.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;

View File

@ -1,4 +1,4 @@
package org.baeldung.methodsecurity.entity;
package com.baeldung.methodsecurity.entity;
import java.util.Collection;

View File

@ -1,16 +1,17 @@
package org.baeldung.methodsecurity.repository;
package com.baeldung.methodsecurity.repository;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.baeldung.methodsecurity.entity.CustomUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import com.baeldung.methodsecurity.entity.CustomUser;
@Service
public class UserRoleRepository {

View File

@ -1,11 +1,12 @@
package org.baeldung.methodsecurity.service;
package com.baeldung.methodsecurity.service;
import org.baeldung.methodsecurity.repository.UserRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Service;
import com.baeldung.methodsecurity.repository.UserRoleRepository;
@Service("userDetailService")
public class CustomUserDetailsService implements UserDetailsService {

View File

@ -1,4 +1,4 @@
package org.baeldung.methodsecurity.service;
package com.baeldung.methodsecurity.service;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;

View File

@ -1,13 +1,10 @@
package org.baeldung.methodsecurity.service;
package com.baeldung.methodsecurity.service;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.security.RolesAllowed;
import org.baeldung.methodsecurity.annotation.IsViewer;
import org.baeldung.methodsecurity.entity.CustomUser;
import org.baeldung.methodsecurity.repository.UserRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PostAuthorize;
@ -18,6 +15,10 @@ import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.baeldung.methodsecurity.annotation.IsViewer;
import com.baeldung.methodsecurity.entity.CustomUser;
import com.baeldung.methodsecurity.repository.UserRoleRepository;
@Service
public class UserRoleService {

View File

@ -1,8 +1,9 @@
package org.baeldung.repository;
package com.baeldung.repository;
import org.baeldung.entity.Task;
import org.springframework.data.repository.CrudRepository;
import com.baeldung.entity.Task;
public interface TaskRepository extends CrudRepository<Task, Long> {
}

View File

@ -1,12 +1,13 @@
package org.baeldung.service;
package com.baeldung.service;
import org.baeldung.entity.Task;
import org.baeldung.repository.TaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostFilter;
import org.springframework.security.access.prepost.PreFilter;
import org.springframework.stereotype.Service;
import com.baeldung.entity.Task;
import com.baeldung.repository.TaskRepository;
@Service
public class TaskService {

View File

@ -1,11 +1,12 @@
package org.baeldung;
package com.baeldung;
import org.baeldung.app.App;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.app.App;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class SpringContextIntegrationTest {

View File

@ -1,11 +1,12 @@
package org.baeldung;
package com.baeldung;
import org.baeldung.app.App;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.app.App;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = App.class)
public class SpringContextTest {

View File

@ -1,8 +1,7 @@
package org.baeldung.methodsecurity;
package com.baeldung.methodsecurity;
import static org.junit.Assert.*;
import org.baeldung.methodsecurity.service.SystemService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -13,15 +12,17 @@ import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.methodsecurity.service.SystemService;
@RunWith(SpringRunner.class)
@ContextConfiguration
public class TestClassLevelSecurity {
public class ClassLevelSecurityIntegrationTest {
@Autowired
SystemService systemService;
@Configuration
@ComponentScan("org.baeldung.methodsecurity.*")
@ComponentScan("com.baeldung.methodsecurity.*")
public static class SpringConfig {
}

View File

@ -1,4 +1,4 @@
package org.baeldung.methodsecurity;
package com.baeldung.methodsecurity;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
@ -6,7 +6,6 @@ import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.baeldung.methodsecurity.service.UserRoleService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -19,15 +18,17 @@ import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.methodsecurity.service.UserRoleService;
@RunWith(SpringRunner.class)
@ContextConfiguration
public class TestMethodSecurity {
public class MethodSecurityIntegrationTest {
@Autowired
UserRoleService userRoleService;
@Configuration
@ComponentScan("org.baeldung.methodsecurity.*")
@ComponentScan("com.baeldung.methodsecurity.*")
public static class SpringConfig {
}

View File

@ -1,8 +1,7 @@
package org.baeldung.methodsecurity;
package com.baeldung.methodsecurity;
import static org.junit.Assert.assertEquals;
import org.baeldung.methodsecurity.service.UserRoleService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -12,10 +11,12 @@ import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.methodsecurity.service.UserRoleService;
@RunWith(SpringRunner.class)
@ContextConfiguration
@WithMockUser(username = "john", roles = { "VIEWER" })
public class TestWithMockUserAtClassLevel {
public class MockUserAtClassLevelIntegrationTest {
@Test
public void givenRoleViewer_whenCallGetUsername_thenReturnUsername() {
@ -27,7 +28,7 @@ public class TestWithMockUserAtClassLevel {
UserRoleService userService;
@Configuration
@ComponentScan("org.baeldung.methodsecurity.*")
@ComponentScan("com.baeldung.methodsecurity.*")
public static class SpringConfig {
}

View File

@ -1,9 +1,7 @@
package org.baeldung.methodsecurity;
package com.baeldung.methodsecurity;
import static org.junit.Assert.assertEquals;
import org.baeldung.methodsecurity.entity.CustomUser;
import org.baeldung.methodsecurity.service.UserRoleService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
@ -14,15 +12,18 @@ import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.baeldung.methodsecurity.entity.CustomUser;
import com.baeldung.methodsecurity.service.UserRoleService;
@RunWith(SpringRunner.class)
@ContextConfiguration
public class TestWithUserDetails {
public class UserDetailsIntegrationTest {
@Autowired
UserRoleService userService;
@Configuration
@ComponentScan("org.baeldung.methodsecurity.*")
@ComponentScan("com.baeldung.methodsecurity.*")
public static class SpringConfig {
}

View File

@ -1,4 +1,4 @@
package org.baeldung.methodsecurity;
package com.baeldung.methodsecurity;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

View File

@ -1,11 +1,10 @@
package org.baeldung.test;
package com.baeldung.test;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.baeldung.app.App;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
@ -20,6 +19,8 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.app.App;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = App.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)