Skip to content

[DE-479] x-arango-driver header #484

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>com.arangodb</groupId>
<artifactId>arangodb-java-driver</artifactId>
<version>6.20.0</version>
<version>6.21.0</version>
<inceptionYear>2016</inceptionYear>
<packaging>jar</packaging>

Expand Down Expand Up @@ -234,6 +234,48 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.google.code.maven-replacer-plugin</groupId>
<artifactId>replacer</artifactId>
<version>1.5.3</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>replace</goal>
</goals>
</execution>
</executions>
<configuration>
<file>${project.basedir}/src/main/java/com/arangodb/PackageVersion.java.in</file>
<outputFile>${project.build.directory}/generated-sources/replacer/com/arangodb/PackageVersion.java
</outputFile>
<replacements>
<replacement>
<token>@project.version@</token>
<value>${project.version}</value>
</replacement>
</replacements>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>${project.build.directory}/generated-sources/replacer</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/arangodb/PackageVersion.java.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.arangodb;

/**
* Automatically generated from PackageVersion.java.in by replacer plugin.
*/
public final class PackageVersion {
public final static String VERSION = "@project.version@";
}
4 changes: 4 additions & 0 deletions src/main/java/com/arangodb/internal/http/HttpConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import com.arangodb.ArangoDBException;
import com.arangodb.DbName;
import com.arangodb.PackageVersion;
import com.arangodb.Protocol;
import com.arangodb.internal.net.Connection;
import com.arangodb.internal.net.HostDescription;
Expand Down Expand Up @@ -85,6 +86,8 @@ public class HttpConnection implements Connection {
// max safe UTF-8 json string length, so that it can be converted to byte array
private static final int MAX_JSON_LENGTH = (Integer.MAX_VALUE - 8) / 4;

private static final String X_ARANGO_DRIVER = "JavaDriver/" + PackageVersion.VERSION + " (JVM/" + System.getProperty("java.specification.version") + ")";

public static class Builder {
private String user;
private String password;
Expand Down Expand Up @@ -323,6 +326,7 @@ public Response execute(final Request request) throws ArangoDBException, IOExcep
final String url = buildUrl(buildBaseUrl(host), request);
final HttpRequestBase httpRequest = buildHttpRequestBase(request, url);
httpRequest.setHeader("User-Agent", "Mozilla/5.0 (compatible; ArangoDB-JavaDriver/1.1; +http://mt.orz.at/)");
httpRequest.setHeader("x-arango-driver", X_ARANGO_DRIVER);
if (contentType == Protocol.HTTP_VPACK) {
httpRequest.setHeader("Accept", "application/x-velocypack");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package com.arangodb.internal.velocystream;

import com.arangodb.ArangoDBException;
import com.arangodb.PackageVersion;
import com.arangodb.internal.ArangoDefaults;
import com.arangodb.internal.net.AccessType;
import com.arangodb.internal.net.Host;
Expand Down Expand Up @@ -54,14 +55,13 @@ public abstract class VstCommunication<R, C extends VstConnection> implements Cl
protected static final String ENCRYPTION_PLAIN = "plain";
protected static final String ENCRYPTION_JWT = "jwt";
private static final Logger LOGGER = LoggerFactory.getLogger(VstCommunication.class);

protected static final AtomicLong mId = new AtomicLong(0L);
protected final ArangoSerialization util;
private static final String X_ARANGO_DRIVER = "JavaDriver/" + PackageVersion.VERSION + " (JVM/" + System.getProperty("java.specification.version") + ")";

protected final ArangoSerialization util;
protected final String user;
protected final String password;
protected volatile String jwt;

protected final Integer chunksize;
protected final HostHandler hostHandler;

Expand Down Expand Up @@ -168,6 +168,7 @@ protected Response createResponse(final Message message) throws VPackParserExcep
protected final Message createMessage(final Request request) throws VPackParserException {
request.putHeaderParam("accept", "application/x-velocypack");
request.putHeaderParam("content-type", "application/x-velocypack");
request.putHeaderParam("x-arango-driver", X_ARANGO_DRIVER);
final long id = mId.incrementAndGet();
return new Message(id, util.serialize(request), request.getBody());
}
Expand Down
38 changes: 38 additions & 0 deletions src/test/java/com/arangodb/UserAgentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.arangodb;

import com.arangodb.velocystream.Request;
import com.arangodb.velocystream.RequestType;
import com.arangodb.velocystream.Response;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import static org.assertj.core.api.Assertions.assertThat;

class UserAgentTest extends BaseJunit5 {

private static final String EXPECTED_VERSION = "6.21.0";

@Test
void packageVersion() {
assertThat(PackageVersion.VERSION).isEqualTo(EXPECTED_VERSION);
}

@ParameterizedTest
@EnumSource(Protocol.class)
void userAgentHeader(Protocol protocol) {
ArangoDB adb = new ArangoDB.Builder()
.useProtocol(protocol)
.build();

Request request = new Request(DbName.SYSTEM, RequestType.GET, "/_admin/echo");
Response resp = adb.execute(request);
String headerValue = resp.getBody().get("headers").get("x-arango-driver").getAsString();

String jvmVersion = System.getProperty("java.specification.version");
String expected = "JavaDriver/" + PackageVersion.VERSION + " (JVM/" + jvmVersion + ")";

assertThat(headerValue).isEqualTo(expected);
adb.shutdown();
}
}
28 changes: 28 additions & 0 deletions src/test/java/com/arangodb/async/UserAgentTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.arangodb.async;

import com.arangodb.DbName;
import com.arangodb.PackageVersion;
import com.arangodb.velocystream.Request;
import com.arangodb.velocystream.RequestType;
import com.arangodb.velocystream.Response;
import org.junit.jupiter.api.Test;

import java.util.concurrent.ExecutionException;

import static org.assertj.core.api.Assertions.assertThat;

class UserAgentTest {
@Test
void userAgentHeader() throws ExecutionException, InterruptedException {
ArangoDBAsync adb = new ArangoDBAsync.Builder().build();
Request request = new Request(DbName.SYSTEM, RequestType.GET, "/_admin/echo");
Response resp = adb.execute(request).get();
String headerValue = resp.getBody().get("headers").get("x-arango-driver").getAsString();

String jvmVersion = System.getProperty("java.specification.version");
String expected = "JavaDriver/" + PackageVersion.VERSION + " (JVM/" + jvmVersion + ")";

assertThat(headerValue).isEqualTo(expected);
adb.shutdown();
}
}