Skip to content
HttpPostSIDS.java 5.71 KiB
Newer Older
xtof's avatar
xtof committed
package org.josast.SIDS;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
xtof's avatar
xtof committed
import java.util.ArrayList;
import java.util.List;
xtof's avatar
xtof committed
import java.util.logging.Logger;
xtof's avatar
xtof committed

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
xtof's avatar
xtof committed
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
xtof's avatar
xtof committed

xtof's avatar
xtof committed
/**
 * @author christophe
 *
 */
xtof's avatar
xtof committed
public class HttpPostSIDS {
xtof's avatar
xtof committed
    private static Logger logger = Logger.getLogger("AmsatLogger");
    private static String USERAGENT = "Mozilla/5.0";
    private CloseableHttpClient client = null;
    private HttpPost post = null;
    private StringBuffer result = null;

    /**
     * Create a Http client for sending SIDS data. <br>
     * exemple of URL
     * <li>Satnogs https://db.satnogs.org/api/telemetry/
     *
     * @param url Database http or https adress
     */
    public HttpPostSIDS(final String url) {

        client = getCloseableHttpClient();
        post = new HttpPost(url);
        post.setHeader("User-Agent", USERAGENT);

    }

    /**
     * Create a Http client for sending SIDS data. <br>
     *
     * @param url
     * @param token provided by satnogs if
     */
    public HttpPostSIDS(final String url, final String token) {

        client = getCloseableHttpClient();
        post = new HttpPost(url);
        post.setHeader("User-Agent", USERAGENT);
        addSatnogsToken(token);

    }

    public void addSatnogsToken(final String token) {

        post.addHeader("Authorization", "Token " + token);
    }

    private CloseableHttpClient getCloseableHttpClient() {
        CloseableHttpClient httpClient = null;
        try {
            httpClient = HttpClients.custom()
                    .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                    .setSSLContext(new SSLContextBuilder()
                            .loadTrustMaterial(null, new TrustStrategy() {
                                public boolean isTrusted(
                                        final X509Certificate[] arg0,
                                        final String arg1)
                                        throws CertificateException {
                                    return true;
                                }
                            }).build())
                    .build();
        } catch (KeyManagementException e) {
            logger.severe(
                    "KeyManagementException in creating http client instance "
                            + e.toString());
        } catch (NoSuchAlgorithmException e) {
            logger.severe(
                    "NoSuchAlgorithmException in creating http client instance "
                            + e.toString());
        } catch (KeyStoreException e) {
            logger.severe("KeyStoreException in creating http client instance "
                    + e.toString());
        }
        return httpClient;
    }

    /**
     * @param sids
     * @return
     */
    public int SendSIDSBasic(final SIDSData sids) {
        // Data preparation
        int resultat = 0;
        HttpResponse response = null;
        BufferedReader rd = null;

        List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
        urlParameters
                .add(new BasicNameValuePair("noradID", "" + sids.getNoradID()));
        urlParameters.add(new BasicNameValuePair("source", sids.getSource()));
        urlParameters
                .add(new BasicNameValuePair("timestamp", sids.getTimestamp()));
        urlParameters.add(new BasicNameValuePair("frame", sids.getFrame()));
        urlParameters.add(new BasicNameValuePair("locator", sids.getLocator()));
        urlParameters.add(
                new BasicNameValuePair("longitude", "" + sids.getLongitude()));
        urlParameters.add(
                new BasicNameValuePair("latitude", "" + sids.getLatitude()));
        urlParameters.add(new BasicNameValuePair("version", "2.0.2"));

        try {
            post.setEntity(new UrlEncodedFormEntity(urlParameters));
        } catch (UnsupportedEncodingException e) {
            logger.severe("URL Encoding error" + e);

        }

        try {
            response = client.execute(post);
            resultat = response.getStatusLine().getStatusCode();
            logger.info("URL Result " + resultat);
        } catch (IOException e) {
            logger.severe("Cient executon error" + e.toString());
        }

        if (response != null) {

            try {
                rd = new BufferedReader(new InputStreamReader(
                        response.getEntity().getContent()));
            } catch (UnsupportedOperationException | IOException e) {
                logger.severe("Buffer reader Exception " + e.toString());
            }

            result = new StringBuffer();
            String line = "";
            try {
                while ((line = rd.readLine()) != null) {
                    result.append(line);
                }
            } catch (IOException e) {
                logger.severe(
                        "Error reading line from server  " + e.toString());
            }
        }
        return resultat;

    }

    public StringBuffer getResult() {
        return result;
    }
xtof's avatar
xtof committed

}