Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added sending user-agent

  • Loading branch information...
commit 9713e86b1a77df804a9eb58ce007c79ad44c2b3c 1 parent 4f096a7
@jexp jexp authored
View
9 src/main/java/org/neo4j/rest/graphdb/ExecutingRestRequest.java
@@ -24,6 +24,7 @@
import java.net.URISyntaxException;
import java.net.URLEncoder;
import java.util.Map;
+import java.util.Properties;
import java.util.concurrent.TimeUnit;
import javax.ws.rs.core.MediaType;
@@ -46,6 +47,7 @@
public static final int READ_TIMEOUT = (int) TimeUnit.SECONDS.toMillis(30);
public static final MediaType STREAMING_JSON_TYPE = new MediaType(APPLICATION_JSON_TYPE.getType(),APPLICATION_JSON_TYPE.getSubtype(), MapUtil.stringMap("stream","true"));
private final String baseUri;
+ private final UserAgent userAgent = new UserAgent();
private final Client client;
public ExecutingRestRequest( String baseUri ) {
@@ -59,6 +61,9 @@ public ExecutingRestRequest( String baseUri, String username, String password )
}
+ public void setUserAgent(String newValue) {
+ userAgent.setUserAgent(newValue);
+ }
protected void addAuthFilter(String username, String password) {
if (username == null) return;
client.addFilter( new HTTPBasicAuthFilter( username, password ) );
@@ -69,6 +74,7 @@ protected Client createClient() {
client.setConnectTimeout(CONNECT_TIMEOUT);
client.setReadTimeout(READ_TIMEOUT);
client.setChunkedEncodingSize(8*1024);
+ userAgent.install(client);
return client;
}
@@ -78,8 +84,7 @@ private ExecutingRestRequest( String uri, Client client ) {
}
protected String uriWithoutSlash( String uri ) {
- String uriString = uri;
- return (uriString.endsWith( "/" ) ? uriString.substring( 0, uriString.length() - 1 ) : uri);
+ return (uri.endsWith("/") ? uri.substring(0, uri.length() - 1) : uri);
}
public static String encode( Object value ) {
View
70 src/main/java/org/neo4j/rest/graphdb/UserAgent.java
@@ -0,0 +1,70 @@
+/**
+ * Copyright (c) 2002-2012 "Neo Technology,"
+ * Network Engine for Objects in Lund AB [http://neotechnology.com]
+ *
+ * This file is part of Neo4j.
+ *
+ * Neo4j is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.neo4j.rest.graphdb;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Properties;
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientHandlerException;
+import com.sun.jersey.api.client.ClientRequest;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.filter.ClientFilter;
+
+/**
+ * @author mh
+ * @since 10.08.12
+ */
+public class UserAgent {
+ private String userAgent = determineUserAgent();
+
+ private String determineUserAgent() {
+ final Properties props = loadPomProperties();
+ return String.format("%s/%s",props.getProperty("artifactId","neo4j-rest-graphdb"),props.getProperty("version","0"));
+ }
+
+ private Properties loadPomProperties() {
+ final Properties props = new Properties();
+ try {
+ final InputStream is = getClass().getClassLoader().getResourceAsStream("/META-INF/maven/org.neo4j/neo4j-rest-graphdb/pom.properties");
+ if (is!=null) {
+ props.load(is);
+ is.close();
+ }
+ } catch (Exception e) {
+ // ignore
+ }
+ return props;
+ }
+
+ public void install(Client client) {
+ client.addFilter(new ClientFilter() {
+ @Override
+ public ClientResponse handle(ClientRequest cr) throws ClientHandlerException {
+ cr.getHeaders().add("User-Agent", userAgent);
+ return getNext().handle(cr);
+ }
+ });
+ }
+
+ public void setUserAgent(String newValue) {
+ this.userAgent=newValue;
+ }
+}
View
35 src/test/java/org/neo4j/rest/graphdb/LocalTestServer.java
@@ -34,7 +34,10 @@
import org.neo4j.server.web.WebServer;
import org.neo4j.test.ImpermanentGraphDatabase;
+import javax.servlet.*;
+import javax.servlet.http.HttpServletRequest;
import java.io.File;
+import java.io.IOException;
import java.net.URI;
import java.net.URL;
import java.util.concurrent.CountDownLatch;
@@ -52,6 +55,7 @@
private final String hostname;
protected String propertiesFile = "test-db.properties";
private final ImpermanentGraphDatabase graphDatabase;
+ private String userAgent;
public LocalTestServer() {
this("localhost",7473);
@@ -67,7 +71,7 @@ public void start() {
if (neoServer!=null) throw new IllegalStateException("Server already running");
URL url = getClass().getResource("/" + propertiesFile);
if (url==null) throw new IllegalArgumentException("Could not resolve properties file "+propertiesFile);
- final Jetty6WebServer jettyWebServer = new Jetty6WebServer() {
+ final Jetty6WebServer jettyWebServer = new Jetty6WebServer(); /* {
@Override
protected void startJetty() {
final Server jettyServer = getJetty();
@@ -82,7 +86,6 @@ protected void startJetty() {
jettyServer.removeLifeCycleListener(startupListener);
// System.err.println("jetty is started after notification " + jettyServer.isStarted());
}
-
@Override
public void stop() {
final Server jettyServer = getJetty();
@@ -94,7 +97,17 @@ public void stop() {
listener.await();
jettyServer.removeLifeCycleListener(listener);
}
- };
+ }; */
+ jettyWebServer.addFilter(new Filter() {
+ public void init(FilterConfig filterConfig) throws ServletException { }
+
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
+ userAgent = ((HttpServletRequest)request).getHeader("User-Agent");
+ filterChain.doFilter(request, response);
+ }
+
+ public void destroy() { }
+ },"/*");
neoServer = new CommunityNeoServer(new PropertyFileConfigurator(new File(url.getPath()))) {
@Override
protected int getWebServerPort() {
@@ -122,6 +135,11 @@ protected WebServer createWebServer() {
}
};
neoServer.start();
+ try {
+ Thread.sleep(500);
+ } catch (InterruptedException e) {
+ throw new RuntimeException(e);
+ }
}
public void stop() {
@@ -162,6 +180,10 @@ public GraphDatabaseService getGraphDatabase() {
return getDatabase().graph;
}
+ public String getUserAgent() {
+ return userAgent;
+ }
+
private static class JettyStartupListener implements LifeCycle.Listener {
CountDownLatch latch=new CountDownLatch(1);
public void await() {
@@ -175,26 +197,31 @@ public void await() {
@Override
public void lifeCycleStarting(LifeCycle event) {
+ System.err.println("STARTING");
}
@Override
public void lifeCycleStarted(LifeCycle event) {
+ System.err.println("STARTED");
latch.countDown();
}
@Override
public void lifeCycleFailure(LifeCycle event, Throwable cause) {
+ System.err.println("FAILURE "+cause.getMessage());
latch.countDown();
throw new RuntimeException(cause);
}
@Override
public void lifeCycleStopping(LifeCycle event) {
-
+ System.err.println("STOPPING");
}
@Override
public void lifeCycleStopped(LifeCycle event) {
+ System.err.println("STOPPED");
+ latch.countDown();
}
}
}
View
10 src/test/java/org/neo4j/rest/graphdb/RestAPITest.java
@@ -49,8 +49,14 @@
public void init(){
this.restAPI = ((RestGraphDatabase)getRestGraphDb()).getRestAPI();
}
-
- @Test
+
+ @Test
+ public void testUserAgent() throws Exception {
+ restAPI.createNode(map());
+ assertTrue(getUserAgent().matches("neo4j-rest-graphdb/[\\d.]+"));
+ }
+
+ @Test
public void testCreateNodeWithParams() {
Map<String, Object> props = new HashMap<String, Object>();
props.put("name", "test");
View
21 src/test/java/org/neo4j/rest/graphdb/RestTestBase.java
@@ -34,6 +34,8 @@
import java.net.URISyntaxException;
import java.util.Iterator;
+import static org.junit.Assert.assertEquals;
+
public class RestTestBase {
private GraphDatabaseService restGraphDb;
@@ -48,6 +50,22 @@
@BeforeClass
public static void startDb() throws Exception {
neoServer.start();
+ tryConnect();
+ }
+
+ private static void tryConnect() throws InterruptedException {
+ int retryCount = 3;
+ for (int i = 0; i < retryCount; i++) {
+ try {
+ RequestResult result = new ExecutingRestRequest(SERVER_ROOT_URI).get("");
+ assertEquals(200, result.getStatus());
+ System.err.println("Successful HTTP connection to "+SERVER_ROOT_URI);
+ return;
+ } catch (Exception e) {
+ System.err.println("Error retrieving ROOT URI " + e.getMessage());
+ Thread.sleep(500);
+ }
+ }
}
@Before
@@ -92,4 +110,7 @@ protected int countExistingNodes() {
protected Node loadRealNode(Node node) {
return getGraphDatabase().getNodeById(node.getId());
}
+ public String getUserAgent() {
+ return neoServer.getUserAgent();
+ }
}
Please sign in to comment.
Something went wrong with that request. Please try again.