<commit_before>/**
 * !(#) RestfulNeoAccessor.java
 * Copyright (c) 2014 DNW Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     DNW Technologies - initial API and implementation
 *
 * Create by manbaum since Oct 12, 2014.
 */
package com.dnw.neo;

import java.io.InputStream;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.dnw.depmap.Activator;
import com.dnw.json.J;
import com.dnw.json.L;
import com.dnw.json.M;

/**
 * Accessor of the Neo4j graph database using REST API.
 * 
 * @author manbaum
 * @since Oct 12, 2014
 */
public class RestfulNeoAccessor implements NeoAccessor {

	private final Client client;
	private final WebTarget rootTarget;

	/**
	 * Constructor of RestfulNeoAccessor.
	 * 
	 * @author manbaum
	 * @since Oct 21, 2014
	 * @param serverRoot
	 */
	public RestfulNeoAccessor(String rootUrl) {
		client = ClientBuilder.newBuilder().build();
		rootTarget = client.target(rootUrl);
	}

	/**
	 * Method sendRequest.
	 * 
	 * @author manbaum
	 * @since Oct 21, 2014
	 * @param target
	 * @param data
	 * @return
	 */
	private M sendRequest(WebTarget target, String jsonData) {
		Response response = target.request().accept(MediaType.APPLICATION_JSON)
				.acceptEncoding("UTF-8").post(Entity.json(jsonData));
		if (response.getStatus() != 200) {
			throw new IllegalStateException("error.response: " + response.getStatusInfo());
		}
		M m = (M)J.parse((InputStream)response.getEntity());
		Activator.console.println(" >> Request: " + jsonData);
		Activator.console.println("  <  Result: " + String.valueOf(m));
		return m;
	}

	/**
	 * Overrider method execute.
	 * 
	 * @author manbaum
	 * @since Oct 18, 2014<commit_msg>Outputs log when cypher statement executed with error result.<commit_after>/**
 * !(#) RestfulNeoAccessor.java
 * Copyright (c) 2014 DNW Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     DNW Technologies - initial API and implementation
 *
 * Create by manbaum since Oct 12, 2014.
 */
package com.dnw.neo;

import java.io.InputStream;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.dnw.depmap.Activator;
import com.dnw.json.J;
import com.dnw.json.L;
import com.dnw.json.M;

/**
 * Accessor of the Neo4j graph database using REST API.
 * 
 * @author manbaum
 * @since Oct 12, 2014
 */
public class RestfulNeoAccessor implements NeoAccessor {

	private final Client client;
	private final WebTarget rootTarget;

	/**
	 * Constructor of RestfulNeoAccessor.
	 * 
	 * @author manbaum
	 * @since Oct 21, 2014
	 * @param serverRoot
	 */
	public RestfulNeoAccessor(String rootUrl) {
		client = ClientBuilder.newBuilder().build();
		rootTarget = client.target(rootUrl);
	}

	/**
	 * Method sendRequest.
	 * 
	 * @author manbaum
	 * @since Oct 21, 2014
	 * @param target
	 * @param data
	 * @return
	 */
	private M sendRequest(WebTarget target, String jsonData) {
		Response response = target.request().accept(MediaType.APPLICATION_JSON)
				.acceptEncoding("UTF-8").post(Entity.json(jsonData));
		if (response.getStatus() != 200) {
			throw new IllegalStateException("error.response: " + response.getStatusInfo());
		}
		M m = (M)J.parse((InputStream)response.getEntity());
		L errors = m.vl("errors");
		if (errors.length() > 0) {
			Activator.console.forceprintln(" >> Request: " + jsonData);
			Activator.console.forceprintln("  <  Result: " + String.valueOf(m));
		} else {
			Activator.console.println(" >> Request: " + jsonData);
			Activator.console.println("  <  Result: " + String.valueOf(m));
		}
		return m;
	}

	/**
	 * Overrider method execute.
	 * 
	 * @author manbaum
	 * @since Oct 18, 2014
{'fraction_non_alphanumeric': 0.12608101957214382, 'fraction_numerical': 0.019799726900318615, 'mean_word_length': 3.82967032967033, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5005618', 'n_tokens_mistral': 1551, 'n_tokens_neox': 1352, 'n_words': 366}