<commit_before>/*
  Copyright (c) 2015, Alcatel-Lucent Inc
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
      * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
      * Neither the name of the copyright holder nor the names of its contributors
        may be used to endorse or promote products derived from this software without
        specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.nuagenetworks.bambou;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import net.nuagenetworks.bambou.jms.RestPushCenterJmsActiveMQ;
import net.nuagenetworks.bambou.jms.RestPushCenterJmsDirectActiveMQ;
import net.nuagenetworks.bambou.jms.RestPushCenterJmsDirectJBoss;
import net.nuagenetworks.bambou.jms.RestPushCenterJmsJBoss;
import net.nuagenetworks.bambou.operation.RestSessionOperations;
import net.nuagenetworks.bambou.service.RestClientService;
import net.nuagenetworks.bambou.annotation.RestEntity;
import net.nuagenetworks.bambou.util.BambouUtils;

public class RestSession<R extends RestRootObject> implements RestSessionOperations {

    private static final String ORGANIZATION_HEADER = "X-Nuage-Organization";
    private static final String CONTENT_TYPE_JSON = "application/json";

    private static final Logger logger = LoggerFactory.getLogger(RestSession.class);
    private static final ThreadLocal<RestSession<?>> currentSession = new ThreadLocal<RestSession<?>>();

    @Autowired
    private RestClientService restClientService;

    private String username;
    private String password;
    private String enterprise;
    private String apiUrl;
    private String apiPrefix;
    private String certificate;
    private String privateKey;
    private double version;
    private String apiKey;
    private Class<R> restRootObjClass;
    private R restRootObj;

    public RestSession(Class<R> restRootObjClass) {
        this.restRootObjClass = restRootObjClass;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(String enterprise) {
        this.enterprise = enterprise;
    }

    public String getApiUrl() {
        return apiUrl;
    }

    public void setApiUrl(String apiUrl) {
        this.apiUrl = apiUrl;
    }

    public String getApiPrefix() {
        return apiPrefix;
    }

    public void setApiPrefix(String apiPrefix) {
        this.apiPrefix = apiPrefix;
    }

    public String getCertificate() {
        return certificate;
    }

    public void setCertificate(String certificate) {
        this.certificate = certificate;
    }

    public String getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }

    public double getVersion() {
        return version;
    }

    public void setVersion(double version) {
        this.version = version;
    }

    protected void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }

    protected static RestSession<?> getCurrentSession() {
        return currentSession.get();
    }

    public R getRootObject() {
        return restRootObj;
    }

    public String getVSDVersion() {
        String response = null;
        response = restClientService.sendRawRequest(HttpMethod.GET,this.apiUrl+"/architect/Resources/app-version.js");
        if (response == null) response = restClientService.sendRawRequest(HttpMethod.GET,this.apiUrl+"/Resources/app-version.js");
        if (response == null) return "";

        Pattern r = Pattern.compile("APP_BUILDVERSION='(.*)'");    
        Matcher m = r.matcher(response);
        if (m.find()) {
            return m.group(1);
        } else {
            return "";
        }
    }

    @Override
    public void start() throws RestException {
        currentSession.set(this);
        restClientService.prepareSSLAuthentication(certificate, privateKey);
        authenticate();<commit_msg>reading vsd version<commit_after>/*
  Copyright (c) 2015, Alcatel-Lucent Inc
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
      * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above copyright
        notice, this list of conditions and the following disclaimer in the
        documentation and/or other materials provided with the distribution.
      * Neither the name of the copyright holder nor the names of its contributors
        may be used to endorse or promote products derived from this software without
        specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.nuagenetworks.bambou;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import net.nuagenetworks.bambou.jms.RestPushCenterJmsActiveMQ;
import net.nuagenetworks.bambou.jms.RestPushCenterJmsDirectActiveMQ;
import net.nuagenetworks.bambou.jms.RestPushCenterJmsDirectJBoss;
import net.nuagenetworks.bambou.jms.RestPushCenterJmsJBoss;
import net.nuagenetworks.bambou.operation.RestSessionOperations;
import net.nuagenetworks.bambou.service.RestClientService;
import net.nuagenetworks.bambou.annotation.RestEntity;
import net.nuagenetworks.bambou.util.BambouUtils;

public class RestSession<R extends RestRootObject> implements RestSessionOperations {

    private static final String ORGANIZATION_HEADER = "X-Nuage-Organization";
    private static final String CONTENT_TYPE_JSON = "application/json";

    private static final Logger logger = LoggerFactory.getLogger(RestSession.class);
    private static final ThreadLocal<RestSession<?>> currentSession = new ThreadLocal<RestSession<?>>();

    @Autowired
    private RestClientService restClientService;

    private String username;
    private String password;
    private String enterprise;
    private String apiUrl;
    private String apiPrefix;
    private String certificate;
    private String privateKey;
    private double version;
    private String apiKey;
    private Class<R> restRootObjClass;
    private R restRootObj;

    public RestSession(Class<R> restRootObjClass) {
        this.restRootObjClass = restRootObjClass;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEnterprise() {
        return enterprise;
    }

    public void setEnterprise(String enterprise) {
        this.enterprise = enterprise;
    }

    public String getApiUrl() {
        return apiUrl;
    }

    public void setApiUrl(String apiUrl) {
        this.apiUrl = apiUrl;
    }

    public String getApiPrefix() {
        return apiPrefix;
    }

    public void setApiPrefix(String apiPrefix) {
        this.apiPrefix = apiPrefix;
    }

    public String getCertificate() {
        return certificate;
    }

    public void setCertificate(String certificate) {
        this.certificate = certificate;
    }

    public String getPrivateKey() {
        return privateKey;
    }

    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }

    public double getVersion() {
        return version;
    }

    public void setVersion(double version) {
        this.version = version;
    }

    protected void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }

    protected static RestSession<?> getCurrentSession() {
        return currentSession.get();
    }

    public R getRootObject() {
        return restRootObj;
    }

    public String getVSDVersion() {
        try {
            ResponseEntity<JsonNode> resp = restClientService.sendRequest(HttpMethod.GET,this.apiUrl+"/nuage",null,null,JsonNode.class);
            JsonNode node = resp.getBody();
            return node.get("vsdBuild").textValue();
        } catch (Exception e) {
            logger.error("Error: ",e);
        }
        return null;
    }

    @Override
    public void start() throws RestException {
        currentSession.set(this);
        restClientService.prepareSSLAuthentication(certificate, privateKey);
        authenticate();
{'fraction_non_alphanumeric': 0.07004197271773348, 'fraction_numerical': 0.0014865337530605106, 'mean_word_length': 3.4090208172706244, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, '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': '7388098', 'n_tokens_mistral': 3110, 'n_tokens_neox': 2954, 'n_words': 953}