SendToURLService.java 6.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
package adt.mainnode.service;

import adt.mainnode.entity.Credentials;
import adt.mainnode.entity.ScanConfig;
import adt.mainnode.entity.Node;
import adt.mainnode.entity.ScanInfo;
import adt.mainnode.entity.Vlan;
import adt.mainnode.repository.CredentialsRepository;
import adt.mainnode.repository.ScanConfigRepository;
import adt.mainnode.repository.NodeRepository;
import adt.mainnode.repository.VlanRepository;
import adt.mainnode.service.InitializeService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.concurrent.Executors;
import javax.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.util.Arrays;
import java.util.ArrayList;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Response.Status;
import java.io.IOException;
//import org.json.*;//JSONObject;
import java.util.*;
import java.net.*;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import org.apache.http.client.methods.*;
import org.apache.http.impl.client.*;
//import org.springframework.http.*;
import org.apache.http.entity.mime.*;
import org.apache.http.entity.*;
import org.apache.http.*;
import org.apache.http.util.*;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.JSONArray;
import java.io.*;
//import javax.json.stream.*;
import adt.mainnode.entity.changed.*;


@Slf4j
@Service
public class SendToURLService {

    private final InitializeService initializeService;


    @Autowired
    public SendToURLService(InitializeService initializeService) {

        this.initializeService = initializeService;
    }

    void processEvent(List<String> transfer_urls, SnapshotChange object) {

        for(int i=0; i<transfer_urls.size(); i++) {

            log.info("SENDING SNAPSHOT CHANGE TO {}", transfer_urls.get(i));

            try {
                URL url = new URL(transfer_urls.get(i));

                HttpURLConnection con = (HttpURLConnection) url.openConnection();
                con.setRequestMethod("POST");
                con.setRequestProperty("Content-Type", "application/json");
                con.setRequestProperty("Accept", "application/json");
                con.setDoOutput(true);

                final ByteArrayOutputStream out = new ByteArrayOutputStream();
                final ObjectMapper mapper = new ObjectMapper();

                mapper.writeValue(out, object);

                final byte[] data = out.toByteArray();
                //System.out.println(new String(data));
                log.info(new String(data));

                try (OutputStream os = con.getOutputStream()) {
                    //byte[] input = jsonInputString.getBytes("utf-8");
                    os.write(data, 0, data.length);
                }

                try (BufferedReader br = new BufferedReader(
                        new InputStreamReader(con.getInputStream(), "utf-8"))) {
                    StringBuilder response = new StringBuilder();
                    String responseLine = null;
                    while ((responseLine = br.readLine()) != null) {
                        response.append(responseLine.trim());
                    }
                    log.info(response.toString());
                } catch (Exception ex) {
                    log.info("#######{}", ex);
                }

            } catch (Exception ex) {
                // handled
                log.info("#######{}", ex);
            }

        }


    }


    public static String getParamsString(Map<String, String> params)
            throws UnsupportedEncodingException{
        StringBuilder result = new StringBuilder();

        for (Map.Entry<String, String> entry : params.entrySet()) {
            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
            result.append("&");
        }

        String resultString = result.toString();
        return resultString.length() > 0
                ? resultString.substring(0, resultString.length() - 1)
                : resultString;
    }


    public static String extractIP(String s) {
        java.util.regex.Matcher m = java.util.regex.Pattern.compile(
                "(?<!\\d|\\d\\.)" +
                        "(?:[01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                        "(?:[01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                        "(?:[01]?\\d\\d?|2[0-4]\\d|25[0-5])\\." +
                        "(?:[01]?\\d\\d?|2[0-4]\\d|25[0-5])" +
                        "(?!\\d|\\.\\d)").matcher(s);
        return m.find() ? m.group() : null;
    }


    public static String reAssembleURL(String url){

        String url_canon_init = "http://";

        if(extractIP(url) != null){

            if(url.length() < 7 || !url.substring(0,7).equals(url_canon_init)){

                url = url_canon_init + url;
            }
        }else{

            if(url.length() < 11 || !url.substring(0,11).equals(url_canon_init+"www.")){

                url = url_canon_init +"www."+ url;
            }
        }

        return url;

    }

    public static boolean token_caducity(Date token_receipt){

        Date token_receipt_date = token_receipt;

        long diff = new Date().getTime() - token_receipt_date.getTime();

        if(TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS) >= 7){

            return true;
        }

        return false;

    }

}