Java HTTP SSL Πρόβλημα

...του ubuntu και έργων ΕΛ/ΛΑΚ (Έργα-Οδηγοί-Προτάσεις)

Συντονιστής: konnn

Java HTTP SSL Πρόβλημα

Δημοσίευσηαπό pc_magas » 13 Μάιος 2013, 22:30

Παίδια έχω ένα μικρό θεματάκι κάνοντας έναν http client σε java (Βασικά lIbrary για http client)

Έτσι έχω το πακέτο net που περιλαμβάνει τις Κλάσεις:
Downloader.java
Κώδικας: Επιλογή όλων

package net;

import net.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.net.*;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;

public class Downloader implements Runnable
{
private class ProgressListener implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
// e.getSource() gives you the object of DownloadCountingOutputStream
// because you set it in the overriden method, afterWrite().
if(total>0)
{
percent=100*((DownloadCountingOutputStream) e.getSource()).getByteCount()/(float)total;
}
}
}

private class SSLManager implements X509TrustManager
{
public X509Certificate[] getAcceptedIssuers()
{
return null;
}

public void checkClientTrusted(X509Certificate[] certs, String authType)
{
}

public void checkServerTrusted(X509Certificate[] certs, String authType)
{
}
}

/**
*Shows the total size of file
*/
private long total=0;
/**
*Displays the total percent of file
*/
private float percent=0;
/**
*The remote url of file
*/
private URL dl;
/**
*The file that we will store
*/
private File fl;

/**
*Stores the status
*/
private String status="Waiting";

/**
*Stores the mime type
*/
private String mime="";

/**
*Tells if it is a sslk connection or not
*/
private boolean ssl=false;

/**
*Variable that configures the connection
*/
private URLConnection conn=null;

public Downloader(String url, String path)
{
this(url,path,null,(long)0);
}

public Downloader(String url, String path,String[] allowedType,long allowedSize)
{
boolean ok=true;
//Needed for ssl

try
{
if(!url.startsWith("http://"))
{
url="http://"+url;
}
else if(url.startsWith("https://"))
{
ssl=true;
}

dl=new URL(url);
fl=new File(path);

conn=dl.openConnection();

if(conn!=null)
{
mime=conn.getContentType();

for(int i=0;allowedType!=null && mime!=null && i<allowedType.length;i++)
{
if(mime.equals(allowedType[i]))
{
System.out.println("Mime OK");
ok=true;
break;
}
else
{
System.out.println("Mime Not OK");
ok=false;
}
}

String s=conn.getHeaderField("Content-Length");
if(s!=null)
{
total=Long.parseLong(s);
if(ok)
{
if(allowedSize>0 && total<allowedSize)
{
System.out.println("OK");
ok=true;
}
else if(allowedSize<=0)
{
System.out.println("No Limit");
ok=true;
}
else
{
System.out.println("Not OK");
ok=false;
}
}
}
}

if(ok)
{
Thread t=new Thread(this);
t.start();
}
else
{
status="Error";
}
}
catch(UnknownHostException u)
{
System.err.println("Uknown Host");
u.printStackTrace();
}
catch(Exception m)
{
m.printStackTrace();
}
}

public void run()
{
OutputStream os = null;
InputStream is = null;

ProgressListener progressListener = new ProgressListener();
try
{
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] { new SSLManager()};

// Install the all-trusting trust manager
final SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier()
{
public boolean verify(String hostname, SSLSession session)
{
return true;
}
};

// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);


os = new FileOutputStream(fl);
is = conn.getInputStream();

DownloadCountingOutputStream dcount = new DownloadCountingOutputStream(os);
dcount.setListener(progressListener);

status="Downloading";
// begin transfer by writing to dcount, not os.
IOUtils.copy(is, dcount);

}
catch(UnknownHostException u)
{
System.err.println("Uknown Host2");
u.printStackTrace();
}
catch (Exception e)
{
System.out.println(e);
}
finally
{
try
{
status="Finished";
if (os != null)
{
os.close();
}
if (is != null)
{
is.close();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}

public float getPercent()
{
return percent;
}

public String getStatus()
{
return status;
}

public long getSize()
{
return total;
}

public String getMime()
{
return mime;
}
}


Και την DownloadCountingOutputStream
Κώδικας: Επιλογή όλων

package net;

import java.awt.event.ActionListener;
import org.apache.commons.io.output.CountingOutputStream;
import java.io.*;
import java.awt.event.ActionEvent;
public class DownloadCountingOutputStream extends CountingOutputStream {

private ActionListener listener = null;

public DownloadCountingOutputStream(OutputStream out) {
super(out);
}

public void setListener(ActionListener listener) {
this.listener = listener;
}

@Override
protected void afterWrite(int n) throws IOException {
super.afterWrite(n);
if (listener != null) {
listener.actionPerformed(new ActionEvent(this, 0, null));
}
}

}


Καθώς και την Κλάση Main για τεστς:
Κώδικας: Επιλογή όλων

import net.Downloader;

public class Main
{
public static void main(String[] args)
{
if(args.length>0)
{
try
{
String[] allowed={"text/plain","text/xml","application/xml","application/octet-stream"};
Downloader d = new Downloader(args[0],args[1],allowed,5*(long)Math.pow(2,10));
float percent=0;

System.out.println("Mime: "+d.getMime());
System.out.println("Filesize: "+d.getSize());

if(!d.getStatus().equals("Error"))
{
while(!d.getStatus().equals("Finished"))
{
percent=d.getPercent();
System.out.println("Κατέβηκαν: "+percent+"%");
try
{
Thread.sleep(100);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
else
{
System.err.println("Error occured durong download");
}
}
catch(Exception e)
{
e.printStackTrace();
}

}
}
}


Compile help
Θέλει την βιβλιοθήκη [url="http://commons.apache.org/io/download_io.cgi"]Apache commons IO[/url]

Έγω στο ίδιο φάκελο έχω το commons-io-2.4.jar,έναν φάκελο με όνομα net που περιέχει το αρχείο DownloadCountingOutputStream.java (για την DownloadCountingOutputStream Κλάση) και το Downloader.java για την κλάση Downloader, και έζω από το net το αρχείο Main.java που περιέχει την κλάση Main.

Το πακέτο net το κάνω compile με:
Κώδικας: Επιλογή όλων
javac -cp commons-io-2.4.jar:. ./net/*.java

Και την main με:
Κώδικας: Επιλογή όλων
javac Main.java


Αυτό που κάνει η Main είναι να κατεβάζει αρχεία .guma (Για την εγαρμογή GUMA ;) ) και .xml Παίρνει σαν παράμετρους πρώτο το url του αρχείου (πχ http://temphost/sample.xml) και δεύτερο το path του αρχείου που θα αποθηκευτεί πχ (samprela.xml).

μως όταν πάω να του βάλω δυνατότητα να υποστηρίζει SSL παίρνω:
Κώδικας: Επιλογή όλων
java -cp commons-io-2.4.jar:. Main https://raw.github.com/facebook/hadoop-20/master/singleNodeHadoop/mapredConf/ssl-server.xml.example sample.xml
Mime: null
Filesize: 0
Κατέβηκαν: 0.0%
Uknown Host2
java.net.UnknownHostException: https
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1674)
at sun.net.www.protocol.http.HttpURLConnection$6.run(HttpURLConnection.java:1672)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(HttpURLConnection.java:1670)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1243)
at net.Downloader.run(Downloader.java:209)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.net.UnknownHostException: https
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:178)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:391)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at sun.net.NetworkClient.doConnect(NetworkClient.java:180)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:378)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:473)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:203)
at sun.net.www.http.HttpClient.New(HttpClient.java:290)
at sun.net.www.http.HttpClient.New(HttpClient.java:306)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:995)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:931)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:849)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1299)
at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:2677)
at java.net.URLConnection.getContentType(URLConnection.java:522)
at net.Downloader.<init>(Downloader.java:116)
at Main.main(Main.java:12)

Γιατί όμως; Αυτό δεν μπορώ να καταλάβω...

Ενώ χωρίς ssl μπορώ να κατεβάζω τα αρχεία:
Κώδικας: Επιλογή όλων

pcmagas@pcmagas-destop:~/Kwdikas/java/Libs/commons-io-2.4$ java -cp commons-io-2.4.jar:. Main http://ubuntuone.com/5hHq6apAbIeekIcAtUaiT7 g.guma
Mime Not OK
Mime Not OK
Mime Not OK
Mime OK
OK
Mime: application/octet-stream
Filesize: 1665
Κατέβηκαν: 0.0%
My blog|Κυπριακή Κοινότητα Ελευθέρου Λογισμικού Λογισμικού ανοικτού Κώδικα
Γνώσεις Linux:Ποτέ αρκετές|Προγραμματισμός: Php, javascript, nodejs, python, bash |Aγγλικά:Καλά
Οι υπολογιστές μου:
Spoiler: show
Ubuntu 16.04 64 bit σεIntel(R) Pentium(R) CPU G4400 @ 3.30GHz, 16Gib Ram, 500Gib Hard Disk, και κάρτα γραφικών Nvidia Geforce GT610
Lubuntu 14.04 σε Dell Inspiron mini 10(1010) intel Atom Z500 1Gb ram και gma500 (εδώθη σε άλλον)
Kubuntu 16.04 Lenovo G70 Intel i5 Nvidia Grapgics Card, Intel Graphics card (έχει 2) με Nouveau, 16Gb RAM, 126GB SSD Σκληρό Δίσκο
Άβαταρ μέλους
pc_magas
powerTUX
powerTUX
 
Δημοσιεύσεις: 2599
Εγγραφή: 12 Απρ 2009, 18:55
Τοποθεσία: Αχαρναί Αττικής
Launchpad: pc_magas
IRC: pc_magas
Εκτύπωση

Re: Java HTTP SSL Πρόβλημα

Δημοσίευσηαπό vasster » 16 Μάιος 2013, 19:11

Αν με ένα browser δοκιμάσεις την secure διεύθυνση και δουλεύει κανονικά τότε υπάρχει θέμα με το πιστοποιητικό ή το chain από τα πιστοποιητικά. Κατέβασε το πιστοποιητικό από το site και στη συνέχεια είτε εισήγαγε το στο cacerts που βρίσκεται στο jdk_path/jre/lib/security είτε δημιούργησε ένα νέο keystore που θα χρησιμοποιείς από το πρόγραμμα σου και θα περιέχει αυτό το πιστοποιητικό.
Γνώσεις Linux: Πολύ καλό ┃ Προγραμματισμού: Πολύ καλό ┃ Αγγλικών: Πολύ καλό
Ubuntu 17.04 (Zesty Zapus) 4.10.0-19-generic 64bit (en_US.UTF-8, Unity ubuntu)
Intel Core i5-6500 CPU @ 3.20GHz ‖ RAM 15915 MiB ‖ MSI H170M PRO-VDH (MS-7982) - MSI MS-7982
Intel Sky Lake Integrated Graphics [8086:1912] {i915_bpo}
enp1s0: Realtek RTL8111/8168/8411 PCI Express Gigabit Ethernet Controller [10ec:8168] (rev 15) ⋮ wlx784476b5edea: 148f:5370 Ralink Technology, Corp. RT5370 Wireless Adapter
vasster
punkTUX
punkTUX
 
Δημοσιεύσεις: 253
Εγγραφή: 23 Μάιος 2010, 09:51
Εκτύπωση

Re: Java HTTP SSL Πρόβλημα

Δημοσίευσηαπό pc_magas » 16 Μάιος 2013, 19:24

Το έλυσα απλά μου έβαζε http:// μπροστά από το https://
My blog|Κυπριακή Κοινότητα Ελευθέρου Λογισμικού Λογισμικού ανοικτού Κώδικα
Γνώσεις Linux:Ποτέ αρκετές|Προγραμματισμός: Php, javascript, nodejs, python, bash |Aγγλικά:Καλά
Οι υπολογιστές μου:
Spoiler: show
Ubuntu 16.04 64 bit σεIntel(R) Pentium(R) CPU G4400 @ 3.30GHz, 16Gib Ram, 500Gib Hard Disk, και κάρτα γραφικών Nvidia Geforce GT610
Lubuntu 14.04 σε Dell Inspiron mini 10(1010) intel Atom Z500 1Gb ram και gma500 (εδώθη σε άλλον)
Kubuntu 16.04 Lenovo G70 Intel i5 Nvidia Grapgics Card, Intel Graphics card (έχει 2) με Nouveau, 16Gb RAM, 126GB SSD Σκληρό Δίσκο
Άβαταρ μέλους
pc_magas
powerTUX
powerTUX
 
Δημοσιεύσεις: 2599
Εγγραφή: 12 Απρ 2009, 18:55
Τοποθεσία: Αχαρναί Αττικής
Launchpad: pc_magas
IRC: pc_magas
Εκτύπωση


  • ΣΧΕΤΙΚΑ ΘΕΜΑΤΑ
    ΑΠΑΝΤΗΣΕΙΣ
    ΠΡΟΒΟΛΕΣ
    ΣΥΓΓΡΑΦΕΑΣ

Επιστροφή στο Ανάπτυξη Λογισμικού / Αλγόριθμοι