Relocate Agent to web module

This commit is contained in:
Stephane Nicoll
2019-02-08 10:56:41 +01:00
parent b6657211f6
commit 478c0c41c6
6 changed files with 10 additions and 10 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,8 +21,8 @@ import java.util.List;
import javax.servlet.http.HttpServletRequest;
import io.spring.initializr.util.Agent;
import io.spring.initializr.util.Agent.AgentId;
import io.spring.initializr.web.support.Agent;
import io.spring.initializr.web.support.Agent.AgentId;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;

View File

@@ -34,13 +34,13 @@ import io.spring.initializr.metadata.DependencyMetadata;
import io.spring.initializr.metadata.DependencyMetadataProvider;
import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.metadata.InitializrMetadataProvider;
import io.spring.initializr.util.Agent;
import io.spring.initializr.util.Agent.AgentId;
import io.spring.initializr.web.mapper.DependencyMetadataV21JsonMapper;
import io.spring.initializr.web.mapper.InitializrMetadataJsonMapper;
import io.spring.initializr.web.mapper.InitializrMetadataV21JsonMapper;
import io.spring.initializr.web.mapper.InitializrMetadataV2JsonMapper;
import io.spring.initializr.web.mapper.InitializrMetadataVersion;
import io.spring.initializr.web.support.Agent;
import io.spring.initializr.web.support.Agent.AgentId;
import io.spring.initializr.web.support.CommandLineHelpGenerator;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Tar;

View File

@@ -0,0 +1,176 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.web.support;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Defines the agent that submitted a request.
*
* @author Stephane Nicoll
*/
public class Agent {
/**
* The {@link AgentId}.
*/
private final AgentId id;
/**
* The version of the agent, if any.
*/
private final String version;
public Agent(AgentId id, String version) {
this.id = id;
this.version = version;
}
public AgentId getId() {
return this.id;
}
public String getVersion() {
return this.version;
}
/**
* Create an {@link Agent} based on the specified {@code User-Agent} header.
* @param userAgent the user agent
* @return an {@link Agent} instance or {@code null}
*/
public static Agent fromUserAgent(String userAgent) {
return UserAgentHandler.parse(userAgent);
}
/**
* Defines the various known agents.
*/
public enum AgentId {
/**
* CURL.
*/
CURL("curl", "curl"),
/**
* HTTPie.
*/
HTTPIE("httpie", "HTTPie"),
/**
* JBoss Forge.
*/
JBOSS_FORGE("jbossforge", "SpringBootForgeCli"),
/**
* The Spring Boot CLI.
*/
SPRING_BOOT_CLI("spring", "SpringBootCli"),
/**
* Spring Tools Suite.
*/
STS("sts", "STS"),
/**
* IntelliJ IDEA.
*/
INTELLIJ_IDEA("intellijidea", "IntelliJ IDEA"),
/**
* Netbeans.
*/
NETBEANS("netbeans", "NetBeans"),
/**
* Visual Studio Code.
*/
VSCODE("vscode", "vscode"),
/**
* Jenkins X.
*/
JENKINSX("jenkinsx", "jx"),
/**
* A generic browser.
*/
BROWSER("browser", "Browser");
final String id;
final String name;
public String getId() {
return this.id;
}
public String getName() {
return this.name;
}
AgentId(String id, String name) {
this.id = id;
this.name = name;
}
}
private static class UserAgentHandler {
private static final Pattern TOOL_REGEX = Pattern
.compile("([^\\/]*)\\/([^ ]*).*");
private static final Pattern STS_REGEX = Pattern.compile("STS (.*)");
private static final Pattern NETBEANS_REGEX = Pattern
.compile("nb-springboot-plugin\\/(.*)");
public static Agent parse(String userAgent) {
Matcher matcher = TOOL_REGEX.matcher(userAgent);
if (matcher.matches()) {
String name = matcher.group(1);
for (AgentId id : AgentId.values()) {
if (name.equals(id.name)) {
String version = matcher.group(2);
return new Agent(id, version);
}
}
}
matcher = STS_REGEX.matcher(userAgent);
if (matcher.matches()) {
return new Agent(AgentId.STS, matcher.group(1));
}
matcher = NETBEANS_REGEX.matcher(userAgent);
if (matcher.matches()) {
return new Agent(AgentId.NETBEANS, matcher.group(1));
}
if (userAgent.equals(AgentId.INTELLIJ_IDEA.name)) {
return new Agent(AgentId.INTELLIJ_IDEA, null);
}
if (userAgent.contains("Mozilla/5.0")) { // Super heuristics
return new Agent(AgentId.BROWSER, null);
}
return null;
}
}
}

View File

@@ -0,0 +1,115 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.web.support;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link Agent}.
*
* @author Stephane Nicoll
*/
class AgentTests {
@Test
void checkCurl() {
Agent agent = Agent.fromUserAgent("curl/1.2.4");
assertThat(agent.getId()).isEqualTo(Agent.AgentId.CURL);
assertThat(agent.getVersion()).isEqualTo("1.2.4");
}
@Test
void checkHttpie() {
Agent agent = Agent.fromUserAgent("HTTPie/0.8.0");
assertThat(agent.getId()).isEqualTo(Agent.AgentId.HTTPIE);
assertThat(agent.getVersion()).isEqualTo("0.8.0");
}
@Test
void checkJBossForge() {
Agent agent = Agent.fromUserAgent("SpringBootForgeCli/1.0.0.Alpha4");
assertThat(agent.getId()).isEqualTo(Agent.AgentId.JBOSS_FORGE);
assertThat(agent.getVersion()).isEqualTo("1.0.0.Alpha4");
}
@Test
void checkSpringBootCli() {
Agent agent = Agent.fromUserAgent("SpringBootCli/1.3.1.RELEASE");
assertThat(agent.getId()).isEqualTo(Agent.AgentId.SPRING_BOOT_CLI);
assertThat(agent.getVersion()).isEqualTo("1.3.1.RELEASE");
}
@Test
void checkSts() {
Agent agent = Agent.fromUserAgent("STS 3.7.0.201506251244-RELEASE");
assertThat(agent.getId()).isEqualTo(Agent.AgentId.STS);
assertThat(agent.getVersion()).isEqualTo("3.7.0.201506251244-RELEASE");
}
@Test
void checkIntelliJIDEA() {
Agent agent = Agent.fromUserAgent("IntelliJ IDEA");
assertThat(agent.getId()).isEqualTo(Agent.AgentId.INTELLIJ_IDEA);
assertThat(agent.getVersion()).isNull();
}
@Test
void checkIntelliJIDEAWithVersion() {
Agent agent = Agent
.fromUserAgent("IntelliJ IDEA/144.2 (Community edition; en-us)");
assertThat(agent.getId()).isEqualTo(Agent.AgentId.INTELLIJ_IDEA);
assertThat(agent.getVersion()).isEqualTo("144.2");
}
@Test
void checkNetBeans() {
Agent agent = Agent.fromUserAgent("nb-springboot-plugin/0.1");
assertThat(agent.getId()).isEqualTo(Agent.AgentId.NETBEANS);
assertThat(agent.getVersion()).isEqualTo("0.1");
}
@Test
void checkVsCode() {
Agent agent = Agent.fromUserAgent("vscode/0.2.0");
assertThat(agent.getId()).isEqualTo(Agent.AgentId.VSCODE);
assertThat(agent.getVersion()).isEqualTo("0.2.0");
}
@Test
void checkJenkinsX() {
Agent agent = Agent.fromUserAgent("jx/1.1.71");
assertThat(agent.getId()).isEqualTo(Agent.AgentId.JENKINSX);
assertThat(agent.getVersion()).isEqualTo("1.1.71");
}
@Test
void checkGenericBrowser() {
Agent agent = Agent.fromUserAgent(
"Mozilla/5.0 (Linux; Android 6.0.1; Nexus 5 Build/MMB29K) ");
assertThat(agent.getId()).isEqualTo(Agent.AgentId.BROWSER);
assertThat(agent.getVersion()).isNull();
}
@Test
void checkRobot() {
Agent agent = Agent.fromUserAgent("Googlebot-Mobile");
assertThat(agent).isNull();
}
}