Skip to content

Commit

Permalink
Init DataSourcesChecker
Browse files Browse the repository at this point in the history
  • Loading branch information
bogdanni committed Mar 25, 2017
0 parents commit 82678f4
Show file tree
Hide file tree
Showing 20 changed files with 1,094 additions and 0 deletions.
51 changes: 51 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<project default="all">
<property name="src" location="src"/>
<property name="bin" location="bin"/>
<property name="resources" location="resources"/>
<property name="jarfile" location= "DataSourcesChecker.jar"/>

<fileset id="libfiles" dir="./">
<include name="lib/*.jar"/>
</fileset>

<target name="clean">
<delete dir="${bin}"/>
<delete file="${jarfile}"/>
</target>

<target name="compile">
<mkdir dir="${bin}"/>
<javac srcdir="${src}" destdir="${bin}" target="1.8" source="1.8" debug="on" debuglevel="source,lines,vars" includeantruntime="false" encoding="utf-8">
<classpath>
<fileset dir="${src}"/>
<fileset refid="libfiles"/>
</classpath>
</javac>
</target>

<target name="jar" depends="compile">
<manifestclasspath property="manifest_cp" jarfile="${jarfile}">
<classpath>
<fileset refid="libfiles"/>
</classpath>
</manifestclasspath>
<jar destfile="${jarfile}">
<fileset dir="${bin}"/>
<fileset dir="${resources}"/>
<manifest>
<attribute name="Main-Class" value="org.helioviewer.jhv.DataSourcesChecker"/>
<attribute name="Class-Path" value="${manifest_cp}"/>
<attribute name="version" value="1.0"/>
<attribute name="revision" value="0"/>
</manifest>
</jar>
</target>

<target name="run" depends="jar">
<java jar="${jarfile}" fork="true"/>
</target>

<target name="all" depends="jar"/>

</project>
Binary file added lib/annotations-java8.jar
Binary file not shown.
Binary file added lib/guava-21.0.jar
Binary file not shown.
Binary file added lib/json.jar
Binary file not shown.
Binary file added lib/org.everit.json.schema-1.5.0.jar
Binary file not shown.
52 changes: 52 additions & 0 deletions resources/data/sources_v1.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "DataSources",
"description": "Helioviewer DataSources API response",
"type": "object",
"patternProperties": {
"^.*$": {
"oneOf": [
{ "$ref": "#/definitions/tree_node" },
{ "$ref": "#/definitions/tree_leaf" }
]
}
},
"definitions": {
"root_part": {
"patternProperties": {
"^.*$": {
"oneOf": [
{ "$ref": "#/definitions/tree_node" },
{ "$ref": "#/definitions/tree_leaf" }
]
}
}
},
"tree_node": {
"properties": {
"name": { "type": "string" },
"description": { "type": "string" },
"children": { "$ref": "#/definitions/root_part" },
"default": { "type" : "boolean" }
},
"required": [ "name", "description", "children" ]
},
"tree_leaf": {
"properties": {
"name": { "type": "string" },
"description": { "type": "string" },
"sourceId": { "type": "integer" },
"start": {
"type": "string",
"format": "sql-date-time"
},
"end": {
"type": "string",
"format": "sql-date-time"
},
"default": { "type" : "boolean" }
},
"required": [ "name", "description", "sourceId", "start", "end" ]
}
}
}
11 changes: 11 additions & 0 deletions src/org/helioviewer/jhv/DataSourcesChecker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.helioviewer.jhv;

import org.helioviewer.jhv.io.DataSources;

public class DataSourcesChecker {

public static void main(String[] args) {
DataSources.loadSources();
}

}
25 changes: 25 additions & 0 deletions src/org/helioviewer/jhv/JHVGlobals.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.helioviewer.jhv;

import java.util.concurrent.ExecutorService;

import org.helioviewer.jhv.threads.JHVExecutor;

public class JHVGlobals {

public static String userAgent = "DataSources-schema";

private static final ExecutorService executorService = JHVExecutor.getJHVWorkersExecutorService("MAIN", 10);

public static ExecutorService getExecutorService() {
return executorService;
}

public static int getStdReadTimeout() {
return 180000;
}

public static int getStdConnectTimeout() {
return 60000;
}

}
206 changes: 206 additions & 0 deletions src/org/helioviewer/jhv/base/DownloadStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package org.helioviewer.jhv.base;

import java.awt.EventQueue;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;

import org.helioviewer.jhv.JHVGlobals;
import org.helioviewer.jhv.base.logging.Log;

public class DownloadStream {

// Input stream to read the data from
private InputStream in = null;

// Output to send as a post request
private String output = null;

// Suggested name to save (if wanted)
private String outputName = null;

private String contentDisposition = null;
private int contentLength = -1;
private boolean response400 = false;

// Read timeout in ms
private final int readTimeout;

// Connect timeout in ms
private final int connectTimeout;

// URL to connect
private final URL url;
private final boolean ignore400;

private DownloadStream(URL _url, int _connectTimeout, int _readTimeout, boolean _ignore400) {
url = _url;
readTimeout = _readTimeout;
connectTimeout = _connectTimeout;
ignore400 = _ignore400;
}

private DownloadStream(URL _url, boolean _ignore400) {
this(_url, JHVGlobals.getStdConnectTimeout(), JHVGlobals.getStdReadTimeout(), _ignore400);
}

public DownloadStream(String _url, boolean _ignore400) throws MalformedURLException {
this(new URL(_url), _ignore400);
}

public DownloadStream(URL _url) {
this(_url, JHVGlobals.getStdConnectTimeout(), JHVGlobals.getStdReadTimeout(), false);
}

public DownloadStream(String _url) throws MalformedURLException {
this(new URL(_url));
}

public boolean isResponse400() {
return response400;
}

private static InputStream getEncodedStream(String encoding, InputStream httpStream) throws IOException {
if (encoding != null) {
switch (encoding.toLowerCase()) {
case "gzip":
return new GZIPInputStream(httpStream);
case "deflate":
return new InflaterInputStream(httpStream);
}
}
return httpStream;
}

/**
* Opens the connection with compression if the server supports
*
* @throws IOException
* From accessing the network
*/
private void connect() throws IOException {
if (EventQueue.isDispatchThread())
throw new IOException("Don't do that");

//Log.debug("Connect to " + url);
URLConnection connection = url.openConnection();
// Set timeouts
connection.setConnectTimeout(connectTimeout);
connection.setReadTimeout(readTimeout);

if (connection instanceof HttpURLConnection) {
HttpURLConnection httpC = (HttpURLConnection) connection;
// get compression if supported
httpC.setRequestProperty("Accept-Encoding", "gzip,deflate");
httpC.setRequestProperty("User-Agent", JHVGlobals.userAgent);

// Write post data if necessary
if (output != null) {
connection.setDoOutput(true);
try (OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8)) {
out.write(output);
}
}

try {
httpC.connect();
} catch (IOException e) {
Log.warn("HTTP connection failed: " + url + " " + e);
}

// Check the connection code
int code = httpC.getResponseCode();
if (code > 400) {
throw new IOException("Error opening HTTP connection to " + url + " Response code: " + code);
}

if (!ignore400 && code == 400) {
throw new IOException("Error opening HTTP connection to " + url + " Response code: " + code);
}

InputStream strm;
if (code == 400) {
response400 = true;
strm = httpC.getErrorStream();
if (strm == null)
strm = httpC.getInputStream();
} else {
strm = httpC.getInputStream();
}

contentDisposition = httpC.getHeaderField("Content-Disposition");
in = getEncodedStream(httpC.getContentEncoding(), strm);
} else {
// Not an http connection
// Write post data if necessary
if (output != null) {
connection.setDoOutput(true);
try (OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8)) {
out.write(output);
}
}
// Okay just normal
in = connection.getInputStream();
}
contentLength = connection.getContentLength();
}

/**
* Gives the outstream to read the response, after calling connect. If it is
* not already connected it will automatically connect
*
* @return output stream of the connection
* @throws IOException
* Error from creating the connction
*/
public InputStream getInput() throws IOException {
if (in == null)
connect();
return in;
}

/**
* After requesting the data the associated file name to save from
* Content-Disposition or the url name
*
* @return suggested download name
*/
public String getOutputName() {
if (outputName == null) {
if (contentDisposition != null) {
Matcher m = Regex.ContentDispositionFilename.matcher(contentDisposition);
if (m.find()) {
outputName = m.group(1);
}
}
if (outputName == null) {
outputName = url.getFile().replace('/', '-');
}
}
return outputName;
}

public int getContentLength() {
return contentLength;
}

/**
* Set the output to send to the server (in HTTP as POST)
*
* @param _output
* Send output to the server, null if nothing (GET in HTTP)
*/
public void setOutput(String _output) {
output = _output;
}

}
Loading

0 comments on commit 82678f4

Please sign in to comment.