Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
@@ -34,3 +34,6 @@
|
||||
- [Copy a File with Java](http://www.baeldung.com/java-copy-file)
|
||||
- [Generating Prime Numbers in Java](http://www.baeldung.com/java-generate-prime-numbers)
|
||||
- [Static and Default Methods in Interfaces in Java](http://www.baeldung.com/java-static-default-methods)
|
||||
- [Iterable to Stream in Java](http://www.baeldung.com/java-iterable-to-stream)
|
||||
- [Converting String to Stream of chars](http://www.baeldung.com/java-string-to-stream)
|
||||
- [How to Iterate Over a Stream With Indices](http://www.baeldung.com/java-stream-indices)
|
||||
|
||||
@@ -101,6 +101,23 @@
|
||||
<version>1.19</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.codepoetics</groupId>
|
||||
<artifactId>protonpack</artifactId>
|
||||
<version>${protonpack.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.vavr</groupId>
|
||||
<artifactId>vavr</artifactId>
|
||||
<version>${vavr.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>one.util</groupId>
|
||||
<artifactId>streamex</artifactId>
|
||||
<version>${streamex.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -267,6 +284,9 @@
|
||||
<collections-generic.version>4.01</collections-generic.version>
|
||||
<commons-codec.version>1.10</commons-codec.version>
|
||||
<lombok.version>1.16.12</lombok.version>
|
||||
<vavr.version>0.9.0</vavr.version>
|
||||
<protonpack.version>1.13</protonpack.version>
|
||||
<streamex.version>0.6.5</streamex.version>
|
||||
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
|
||||
@@ -12,11 +12,8 @@
|
||||
- [Java – Directory Size](http://www.baeldung.com/java-folder-size)
|
||||
- [Differences Between the Java WatchService API and the Apache Commons IO Monitor Library](http://www.baeldung.com/java-watchservice-vs-apache-commons-io-monitor-library)
|
||||
- [Calculate the Size of a File in Java](http://www.baeldung.com/java-file-size)
|
||||
- [Iterable to Stream in Java](http://www.baeldung.com/java-iterable-to-stream)
|
||||
- [Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java](http://www.baeldung.com/java-path)
|
||||
- [Using Java MappedByteBuffer](http://www.baeldung.com/java-mapped-byte-buffer)
|
||||
- [Converting String to Stream of chars](http://www.baeldung.com/java-string-to-stream)
|
||||
- [How to Iterate Over a Stream With Indices](http://www.baeldung.com/java-stream-indices)
|
||||
- [Copy a File with Java](http://www.baeldung.com/java-copy-file)
|
||||
- [Java – Append Data to a File](http://www.baeldung.com/java-append-to-file)
|
||||
- [FileNotFoundException in Java](http://www.baeldung.com/java-filenotfound-exception)
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
<?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" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.jenkins-ci.plugins</groupId>
|
||||
<artifactId>plugin</artifactId>
|
||||
<version>2.33</version>
|
||||
<relativePath />
|
||||
</parent>
|
||||
<artifactId>jenkins-hello-world</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>hpi</packaging>
|
||||
<properties>
|
||||
<!-- Baseline Jenkins version you use to build the plugin. Users must have this version or newer to run. -->
|
||||
<jenkins.version>2.7.3</jenkins.version>
|
||||
</properties>
|
||||
<name>Hello World Plugin</name>
|
||||
<description>A sample Jenkins Hello World plugin</description>
|
||||
<url>https://wiki.jenkins-ci.org/display/JENKINS/TODO+Plugin</url>
|
||||
<licenses>
|
||||
<license>
|
||||
<name>MIT License</name>
|
||||
<url>http://opensource.org/licenses/MIT</url>
|
||||
</license>
|
||||
</licenses>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jenkins-ci.plugins</groupId>
|
||||
<artifactId>structs</artifactId>
|
||||
<version>1.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jenkins-ci.plugins.workflow</groupId>
|
||||
<artifactId>workflow-step-api</artifactId>
|
||||
<version>2.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jenkins-ci.plugins.workflow</groupId>
|
||||
<artifactId>workflow-cps</artifactId>
|
||||
<version>2.39</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jenkins-ci.plugins.workflow</groupId>
|
||||
<artifactId>workflow-job</artifactId>
|
||||
<version>2.11.2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jenkins-ci.plugins.workflow</groupId>
|
||||
<artifactId>workflow-basic-steps</artifactId>
|
||||
<version>2.6</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jenkins-ci.plugins.workflow</groupId>
|
||||
<artifactId>workflow-durable-task-step</artifactId>
|
||||
<version>2.13</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jenkins-ci.plugins.workflow</groupId>
|
||||
<artifactId>workflow-api</artifactId>
|
||||
<version>2.20</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jenkins-ci.plugins.workflow</groupId>
|
||||
<artifactId>workflow-support</artifactId>
|
||||
<version>2.14</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>repo.jenkins-ci.org</id>
|
||||
<url>https://repo.jenkins-ci.org/public/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>repo.jenkins-ci.org</id>
|
||||
<url>https://repo.jenkins-ci.org/public/</url>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
</project>
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.jenkins.helloworld;
|
||||
|
||||
public class ProjectStats {
|
||||
|
||||
private final int classesNumber;
|
||||
private final int linesNumber;
|
||||
|
||||
public ProjectStats(int classesNumber, int linesNumber) {
|
||||
this.classesNumber = classesNumber;
|
||||
this.linesNumber = linesNumber;
|
||||
}
|
||||
|
||||
public int getClassesNumber() {
|
||||
return classesNumber;
|
||||
}
|
||||
|
||||
public int getLinesNumber() {
|
||||
return linesNumber;
|
||||
}
|
||||
}
|
||||
+123
@@ -0,0 +1,123 @@
|
||||
package com.baeldung.jenkins.helloworld;
|
||||
|
||||
import hudson.Extension;
|
||||
import hudson.FilePath;
|
||||
import hudson.Launcher;
|
||||
import hudson.model.AbstractBuild;
|
||||
import hudson.model.AbstractProject;
|
||||
import hudson.model.BuildListener;
|
||||
import hudson.tasks.BuildWrapper;
|
||||
import hudson.tasks.BuildWrapperDescriptor;
|
||||
import org.kohsuke.stapler.DataBoundConstructor;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Stack;
|
||||
|
||||
public class ProjectStatsBuildWrapper extends BuildWrapper {
|
||||
|
||||
private static final String REPORT_TEMPLATE_PATH = "/stats.html";
|
||||
private static final String PROJECT_NAME_VAR = "$PROJECT_NAME$";
|
||||
private static final String CLASSES_NUMBER_VAR = "$CLASSES_NUMBER$";
|
||||
private static final String LINES_NUMBER_VAR = "$LINES_NUMBER$";
|
||||
|
||||
@DataBoundConstructor
|
||||
public ProjectStatsBuildWrapper() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Environment setUp(AbstractBuild build, final Launcher launcher, BuildListener listener) {
|
||||
return new Environment() {
|
||||
@Override
|
||||
public boolean tearDown(AbstractBuild build, BuildListener listener)
|
||||
throws IOException, InterruptedException
|
||||
{
|
||||
ProjectStats stats = buildStats(build.getWorkspace());
|
||||
String report = generateReport(build.getProject().getDisplayName(), stats);
|
||||
File artifactsDir = build.getArtifactsDir();
|
||||
if (!artifactsDir.isDirectory()) {
|
||||
boolean success = artifactsDir.mkdirs();
|
||||
if (!success) {
|
||||
listener.getLogger().println("Can't create artifacts directory at "
|
||||
+ artifactsDir.getAbsolutePath());
|
||||
}
|
||||
}
|
||||
String path = artifactsDir.getCanonicalPath() + REPORT_TEMPLATE_PATH;
|
||||
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(path),
|
||||
StandardCharsets.UTF_8))) {
|
||||
writer.write(report);
|
||||
}
|
||||
return super.tearDown(build, listener);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static ProjectStats buildStats(FilePath root) throws IOException, InterruptedException {
|
||||
int classesNumber = 0;
|
||||
int linesNumber = 0;
|
||||
Stack<FilePath> toProcess = new Stack<>();
|
||||
toProcess.push(root);
|
||||
while (!toProcess.isEmpty()) {
|
||||
FilePath path = toProcess.pop();
|
||||
if (path.isDirectory()) {
|
||||
toProcess.addAll(path.list());
|
||||
} else if (path.getName().endsWith(".java")) {
|
||||
classesNumber++;
|
||||
linesNumber += countLines(path);
|
||||
}
|
||||
}
|
||||
return new ProjectStats(classesNumber, linesNumber);
|
||||
}
|
||||
|
||||
private static int countLines(FilePath path) throws IOException, InterruptedException {
|
||||
byte[] buffer = new byte[1024];
|
||||
int result = 1;
|
||||
try (InputStream in = path.read()) {
|
||||
while (true) {
|
||||
int read = in.read(buffer);
|
||||
if (read < 0) {
|
||||
return result;
|
||||
}
|
||||
for (int i = 0; i < read; i++) {
|
||||
if (buffer[i] == '\n') {
|
||||
result++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String generateReport(String projectName, ProjectStats stats) throws IOException {
|
||||
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
||||
try (InputStream in = ProjectStatsBuildWrapper.class.getResourceAsStream(REPORT_TEMPLATE_PATH)) {
|
||||
byte[] buffer = new byte[1024];
|
||||
int read;
|
||||
while ((read = in.read(buffer)) >= 0) {
|
||||
bOut.write(buffer, 0, read);
|
||||
}
|
||||
}
|
||||
String content = new String(bOut.toByteArray(), StandardCharsets.UTF_8);
|
||||
content = content.replace(PROJECT_NAME_VAR, projectName);
|
||||
content = content.replace(CLASSES_NUMBER_VAR, String.valueOf(stats.getClassesNumber()));
|
||||
content = content.replace(LINES_NUMBER_VAR, String.valueOf(stats.getLinesNumber()));
|
||||
return content;
|
||||
}
|
||||
|
||||
@Extension
|
||||
public static final class DescriptorImpl extends BuildWrapperDescriptor {
|
||||
|
||||
@Override
|
||||
public boolean isApplicable(AbstractProject<?, ?> item) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Nonnull
|
||||
@Override
|
||||
public String getDisplayName() {
|
||||
return "Construct project stats during build";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>$PROJECT_NAME$</title>
|
||||
</head>
|
||||
<body>
|
||||
Project $PROJECT_NAME$:
|
||||
<table border="1">
|
||||
<tr>
|
||||
<th>Classes number</th>
|
||||
<th>Lines number</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>$CLASSES_NUMBER$</td>
|
||||
<td>$LINES_NUMBER$</td>
|
||||
</tr>
|
||||
</table>
|
||||
</body>
|
||||
</html>
|
||||
@@ -43,6 +43,10 @@
|
||||
<groupId>javax.json.bind</groupId>
|
||||
<artifactId>javax.json.bind-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- Dependencies for Yasson -->
|
||||
<!-- <dependency> -->
|
||||
@@ -198,4 +202,5 @@
|
||||
<geronimo-json_1.1_spec.version>1.0</geronimo-json_1.1_spec.version>
|
||||
</properties>
|
||||
|
||||
|
||||
</project>
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.actuator;
|
||||
package com.baeldung.reactive.actuator;
|
||||
|
||||
import org.springframework.boot.actuate.health.Health;
|
||||
import org.springframework.boot.actuate.health.ReactiveHealthIndicator;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.actuator;
|
||||
package com.baeldung.reactive.actuator;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.annotation.*;
|
||||
import org.springframework.stereotype.Component;
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package com.baeldung.actuator;
|
||||
package com.baeldung.reactive.actuator;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
|
||||
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
|
||||
@@ -1 +1,4 @@
|
||||
logging.level.root=INFO
|
||||
logging.level.root=INFO
|
||||
|
||||
management.endpoints.web.expose=*
|
||||
info.app.name=Spring Boot 2 actuator Application
|
||||
+4
-3
@@ -1,6 +1,5 @@
|
||||
package com.baeldung.actuator;
|
||||
package com.baeldung.reactive.actuator;
|
||||
|
||||
import com.baeldung.jsonb.Spring5Application;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -10,12 +9,14 @@ import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.reactive.Spring5ReactiveApplication;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Spring5Application.class)
|
||||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Spring5ReactiveApplication.class)
|
||||
public class ActuatorInfoIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
@@ -43,10 +43,6 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-hateoas</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectreactor</groupId>
|
||||
<artifactId>reactor-spring</artifactId>
|
||||
|
||||
@@ -17,7 +17,6 @@ public class SecurityConfig {
|
||||
public SecurityWebFilterChain securitygWebFilterChain(ServerHttpSecurity http) {
|
||||
return http.authorizeExchange()
|
||||
.pathMatchers("/admin").hasAuthority("ROLE_ADMIN")
|
||||
.pathMatchers("/actuator/**").permitAll()
|
||||
.anyExchange().authenticated()
|
||||
.and().formLogin()
|
||||
.and().build();
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
server.port=8081
|
||||
management.endpoints.web.expose=*
|
||||
info.app.name=Spring Boot 2 actuator Application
|
||||
|
||||
logging.level.root=INFO
|
||||
@@ -110,7 +110,7 @@
|
||||
|
||||
<properties>
|
||||
<!-- Spring -->
|
||||
<org.springframework.version>4.3.4.RELEASE</org.springframework.version>
|
||||
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
|
||||
<org.springframework.security.version>4.2.0.RELEASE</org.springframework.security.version>
|
||||
|
||||
<!-- persistence -->
|
||||
@@ -142,4 +142,4 @@
|
||||
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
@@ -8,24 +8,14 @@ import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
public class ExampleOne extends HttpServlet {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
response.setContentType("text/html");
|
||||
PrintWriter out = response.getWriter();
|
||||
out.println(
|
||||
"<!DOCTYPE html><html>" +
|
||||
"<head>" +
|
||||
"<meta charset=\"UTF-8\" />" +
|
||||
"<title>HTML Rendered by Servlet</title>" +
|
||||
"</head>" +
|
||||
"<body>" +
|
||||
"<h1>HTML Rendered by Servlet</h1></br>" +
|
||||
"<p>This page was rendered by the ExampleOne Servlet!</p>" +
|
||||
"</body>" +
|
||||
"</html>"
|
||||
);
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
response.setContentType("text/html");
|
||||
PrintWriter out = response.getWriter();
|
||||
out.println("<!DOCTYPE html><html>" + "<head>" + "<meta charset=\"UTF-8\" />" + "<title>HTML Rendered by Servlet</title>" + "</head>" + "<body>" + "<h1>HTML Rendered by Servlet</h1></br>" + "<p>This page was rendered by the ExampleOne Servlet!</p>"
|
||||
+ "</body>" + "</html>");
|
||||
}
|
||||
}
|
||||
@@ -7,18 +7,14 @@ import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
@WebServlet(
|
||||
name = "ExampleThree",
|
||||
description = "JSP Servlet With Annotations",
|
||||
urlPatterns = {"/ExampleThree"}
|
||||
)
|
||||
@WebServlet(name = "ExampleThree", description = "JSP Servlet With Annotations", urlPatterns = { "/ExampleThree" })
|
||||
public class ExampleThree extends HttpServlet {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
String message = request.getParameter("message");
|
||||
request.setAttribute("text", message);
|
||||
request.getRequestDispatcher("/jsp/ExampleThree.jsp").forward(request, response);
|
||||
}
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
String message = request.getParameter("message");
|
||||
request.setAttribute("text", message);
|
||||
request.getRequestDispatcher("/jsp/ExampleThree.jsp").forward(request, response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
@Controller
|
||||
public class ErrorController {
|
||||
|
||||
|
||||
@RequestMapping(value = "500Error", method = RequestMethod.GET)
|
||||
public void throwRuntimeException() {
|
||||
throw new NullPointerException("Throwing a null pointer exception");
|
||||
@@ -34,19 +34,18 @@ public class ErrorController {
|
||||
errorMsg = "Http Error Code : 404. Resource not found";
|
||||
break;
|
||||
}
|
||||
// Handle other 4xx error codes.
|
||||
// Handle other 4xx error codes.
|
||||
case 500: {
|
||||
errorMsg = "Http Error Code : 500. Internal Server Error";
|
||||
break;
|
||||
}
|
||||
// Handle other 5xx error codes.
|
||||
// Handle other 5xx error codes.
|
||||
}
|
||||
errorPage.addObject("errorMsg", errorMsg);
|
||||
return errorPage;
|
||||
}
|
||||
|
||||
private int getErrorCode(HttpServletRequest httpRequest) {
|
||||
return (Integer) httpRequest
|
||||
.getAttribute("javax.servlet.error.status_code");
|
||||
return (Integer) httpRequest.getAttribute("javax.servlet.error.status_code");
|
||||
}
|
||||
}
|
||||
|
||||
+6
-6
@@ -14,15 +14,15 @@ import com.baeldung.spring.service.RawDBDemoGeoIPLocationService;
|
||||
@Controller
|
||||
public class GeoIPTestController {
|
||||
private RawDBDemoGeoIPLocationService locationService;
|
||||
|
||||
public GeoIPTestController() throws IOException {
|
||||
locationService
|
||||
= new RawDBDemoGeoIPLocationService();
|
||||
locationService = new RawDBDemoGeoIPLocationService();
|
||||
}
|
||||
@RequestMapping(value="/GeoIPTest", method = RequestMethod.POST)
|
||||
|
||||
@RequestMapping(value = "/GeoIPTest", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
public GeoIP getLocation(
|
||||
@RequestParam(value="ipAddress", required=true) String ipAddress) throws Exception {
|
||||
|
||||
public GeoIP getLocation(@RequestParam(value = "ipAddress", required = true) String ipAddress) throws Exception {
|
||||
|
||||
return locationService.getLocation(ipAddress);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ import org.springframework.web.context.support.ServletContextResource;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
|
||||
@@ -5,15 +5,15 @@ public class GeoIP {
|
||||
private String city;
|
||||
private String latitude;
|
||||
private String longitude;
|
||||
|
||||
|
||||
public GeoIP() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
public GeoIP(String ipAddress) {
|
||||
this.ipAddress = ipAddress;
|
||||
}
|
||||
|
||||
|
||||
public GeoIP(String ipAddress, String city, String latitude, String longitude) {
|
||||
this.ipAddress = ipAddress;
|
||||
this.city = city;
|
||||
@@ -52,5 +52,5 @@ public class GeoIP {
|
||||
public void setLongitude(String longitude) {
|
||||
this.longitude = longitude;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+4
-4
@@ -9,18 +9,18 @@ import com.maxmind.geoip2.DatabaseReader;
|
||||
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
||||
import com.maxmind.geoip2.model.CityResponse;
|
||||
|
||||
public class RawDBDemoGeoIPLocationService{
|
||||
public class RawDBDemoGeoIPLocationService {
|
||||
private DatabaseReader dbReader;
|
||||
|
||||
|
||||
public RawDBDemoGeoIPLocationService() throws IOException {
|
||||
File database = new File("your-path-to-db-file");
|
||||
dbReader = new DatabaseReader.Builder(database).build();
|
||||
}
|
||||
|
||||
|
||||
public GeoIP getLocation(String ip) throws IOException, GeoIp2Exception {
|
||||
InetAddress ipAddress = InetAddress.getByName(ip);
|
||||
CityResponse response = dbReader.city(ipAddress);
|
||||
|
||||
|
||||
String cityName = response.getCity().getName();
|
||||
String latitude = response.getLocation().getLatitude().toString();
|
||||
String longitude = response.getLocation().getLongitude().toString();
|
||||
|
||||
@@ -10,22 +10,21 @@ import com.maxmind.geoip2.DatabaseReader;
|
||||
import com.maxmind.geoip2.exception.GeoIp2Exception;
|
||||
import com.maxmind.geoip2.model.CityResponse;
|
||||
|
||||
|
||||
public class GeoIpIntegrationTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void givenIP_whenFetchingCity_thenReturnsCityData() throws IOException, GeoIp2Exception {
|
||||
File database = new File("your-path-to-db-file");
|
||||
DatabaseReader dbReader = new DatabaseReader.Builder(database).build();
|
||||
|
||||
|
||||
InetAddress ipAddress = InetAddress.getByName("your-public-ip");
|
||||
CityResponse response = dbReader.city(ipAddress);
|
||||
|
||||
|
||||
String countryName = response.getCountry().getName();
|
||||
String cityName = response.getCity().getName();
|
||||
String postal = response.getPostal().getCode();
|
||||
String state = response.getLeastSpecificSubdivision().getName();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
package org.baeldung.methodsecurity.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@PreAuthorize("hasRole('VIEWER')")
|
||||
public @interface IsViewer {
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user