Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
ice
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Deploy
Releases
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Eclipse Projects
The Eclipse Integrated Computational Environment
ice
Merge requests
!24
Bug 471356 : Added example of connecting to and using ParaViewWeb service.
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Bug 471356 : Added example of connecting to and using ParaViewWeb service.
github/fork/scottwittenburg/add-paraviewweb-client
into
master
Overview
5
Commits
1
Changes
2
Merged
Eclipse Webmaster
requested to merge
github/fork/scottwittenburg/add-paraviewweb-client
into
master
9 years ago
Overview
5
Commits
1
Changes
2
Expand
Created by: scottwittenburg
Bug 471356
Signed-off-by: Scott Wittenburg
scott.wittenburg@kitware.com
0
0
Merge request reports
Compare
master
master (base)
and
latest version
latest version
2338e614
1 commit,
4 years ago
2 files
+
342
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
2
Search (e.g. *.vue) (Ctrl+P)
src/org.eclipse.ice.viz.service/src/org/eclipse/ice/viz/service/HttpParaViewWebClient.java
0 → 100644
+
269
−
0
Options
/*******************************************************************************
* Copyright (c) 2014, 2015 Kitware Inc. and UT-Battelle, LLC.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Sebastien Jourdain (Kitware Inc.) - initial API and implementation and/or
* initial documentation
* Jordan Deyton (UT-Battelle, LLC.) - implemented disconnect()
* Jordan Deyton (UT-Battelle, LLC.) - updated to use GSON
* Jordan Deyton (UT-Battelle, LLC.) - removed temporary print statements;
* rearranged return value in connect()
*******************************************************************************/
package
org.eclipse.ice.viz.service.paraview.web
;
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.util.concurrent.Callable
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
import
java.util.concurrent.Future
;
import
com.google.gson.JsonArray
;
import
com.google.gson.JsonElement
;
import
com.google.gson.JsonObject
;
import
com.google.gson.JsonParser
;
import
com.google.gson.JsonPrimitive
;
import
com.google.gson.JsonSyntaxException
;
/**
* TODO Documentation
*
* @author Sebastien Jourdain
*
*/
public
class
HttpParaViewWebClient
implements
IParaViewWebClient
{
/**
*
*/
private
String
baseEndPointURL
;
/**
*
*/
private
final
ExecutorService
requestExecutor
;
/**
* The default constructor.
*/
public
HttpParaViewWebClient
()
{
requestExecutor
=
Executors
.
newSingleThreadExecutor
();
}
/*
* Implements a method from ParaViewWebClient.
*/
private
JsonObject
makeRequest
(
String
method
,
JsonObject
content
)
{
JsonObject
retVal
=
null
;
URL
url
;
HttpURLConnection
connection
=
null
;
try
{
// Create connection
String
fullUrl
=
baseEndPointURL
+
method
;
url
=
new
URL
(
fullUrl
);
connection
=
(
HttpURLConnection
)
url
.
openConnection
();
connection
.
setRequestMethod
(
"POST"
);
connection
.
setRequestProperty
(
"Content-Type"
,
"application/octetstream"
);
connection
.
setRequestProperty
(
"Content-Length"
,
Integer
.
toString
(
content
.
entrySet
().
size
()));
connection
.
setRequestProperty
(
"Content-Language"
,
"en-US"
);
connection
.
setUseCaches
(
false
);
connection
.
setDoInput
(
true
);
connection
.
setDoOutput
(
true
);
// Send request
connection
.
getOutputStream
().
write
(
content
.
toString
().
getBytes
());
connection
.
getOutputStream
().
flush
();
connection
.
getOutputStream
().
close
();
// Get Response
InputStream
is
=
connection
.
getInputStream
();
BufferedReader
rd
=
new
BufferedReader
(
new
InputStreamReader
(
is
));
String
line
;
StringBuffer
response
=
new
StringBuffer
();
while
((
line
=
rd
.
readLine
())
!=
null
)
{
response
.
append
(
line
);
response
.
append
(
'\n'
);
}
rd
.
close
();
// Parse the response into a JsonObject if possible.
try
{
JsonParser
parser
=
new
JsonParser
();
JsonElement
element
=
parser
.
parse
(
response
.
toString
());
if
(
element
.
isJsonObject
())
{
retVal
=
element
.
getAsJsonObject
();
}
}
catch
(
JsonSyntaxException
e
)
{
// Do nothing if the response is not a parseable JsonObject.
}
// If a response could not be processed, create an empty one.
if
(
retVal
==
null
)
{
retVal
=
new
JsonObject
();
}
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
// If there was an error connecting, try to disconnect.
if
(
connection
!=
null
)
{
connection
.
disconnect
();
}
}
return
retVal
;
}
/*
* Implements a method from ParaViewWebClient.
*/
@Override
public
Future
<
Boolean
>
connect
(
String
url
)
{
this
.
baseEndPointURL
=
url
;
return
requestExecutor
.
submit
(
new
Callable
<
Boolean
>()
{
@Override
public
Boolean
call
()
throws
Exception
{
boolean
connected
=
false
;
// ---- Send a HEAD request. ---- //
URL
url
=
new
URL
(
baseEndPointURL
);
HttpURLConnection
connection
=
(
HttpURLConnection
)
url
.
openConnection
();
connection
.
setRequestMethod
(
"HEAD"
);
connection
.
setDoInput
(
true
);
connection
.
setDoOutput
(
true
);
// ------------------------------ //
// ---- Get the response. ---- //
// Note: We let the ExecutorService handle the timeout.
// Determine the connection code from the request.
int
code
=
-
1
;
try
{
code
=
connection
.
getResponseCode
();
if
(
code
==
HttpURLConnection
.
HTTP_OK
)
{
connected
=
true
;
}
}
catch
(
IOException
e
)
{
// There was an error. The connection failed.
}
// --------------------------- //
return
connected
;
}
});
}
/*
* Implements a method from ParaViewWebClient.
*/
@Override
public
Future
<
Boolean
>
disconnect
()
{
return
requestExecutor
.
submit
(
new
Callable
<
Boolean
>()
{
@Override
public
Boolean
call
()
throws
Exception
{
return
true
;
}
});
}
/*
* Implements a method from ParaViewWebClient.
*/
@Override
public
Future
<
JsonObject
>
render
(
int
viewId
,
int
quality
,
int
width
,
int
height
)
{
// Set up the size array (with x height).
JsonArray
size
=
new
JsonArray
();
size
.
add
(
new
JsonPrimitive
(
width
));
size
.
add
(
new
JsonPrimitive
(
height
));
// Set up the content of the request.
JsonObject
reqObj
=
new
JsonObject
();
reqObj
.
add
(
"size"
,
size
);
reqObj
.
addProperty
(
"view"
,
viewId
);
reqObj
.
addProperty
(
"quality"
,
quality
);
reqObj
.
addProperty
(
"localtime"
,
System
.
currentTimeMillis
());
// Set up the main request object. Note that it *must* provide an "args"
// property set to a JSON array.
JsonArray
args
=
new
JsonArray
();
args
.
add
(
reqObj
);
final
JsonObject
mainObj
=
new
JsonObject
();
mainObj
.
add
(
"args"
,
args
);
Callable
<
JsonObject
>
request
=
new
Callable
<
JsonObject
>()
{
public
JsonObject
call
()
throws
Exception
{
return
makeRequest
(
"viewport.image.render"
,
mainObj
);
}
};
return
requestExecutor
.
submit
(
request
);
}
/*
* Implements a method from ParaViewWebClient.
*/
@Override
public
Future
<
JsonObject
>
event
(
int
viewId
,
double
x
,
double
y
,
String
action
,
boolean
[]
mouseState
)
{
// Set up the content of the request.
JsonObject
reqObj
=
new
JsonObject
();
reqObj
.
addProperty
(
"view"
,
viewId
);
reqObj
.
addProperty
(
"x"
,
x
);
reqObj
.
addProperty
(
"y"
,
y
);
reqObj
.
addProperty
(
"buttonLeft"
,
mouseState
[
0
]
?
1
:
0
);
reqObj
.
addProperty
(
"buttonMiddle"
,
mouseState
[
1
]
?
1
:
0
);
reqObj
.
addProperty
(
"buttonRight"
,
mouseState
[
2
]
?
1
:
0
);
reqObj
.
addProperty
(
"shiftKey"
,
mouseState
[
3
]
?
1
:
0
);
reqObj
.
addProperty
(
"ctrlKey"
,
mouseState
[
4
]
?
1
:
0
);
reqObj
.
addProperty
(
"altKey"
,
mouseState
[
5
]
?
1
:
0
);
reqObj
.
addProperty
(
"metaKey"
,
mouseState
[
6
]
?
1
:
0
);
reqObj
.
addProperty
(
"action"
,
action
);
// Set up the main request object. Note that it *must* provide an "args"
// property set to a JSON array.
JsonArray
args
=
new
JsonArray
();
args
.
add
(
reqObj
);
final
JsonObject
mainObj
=
new
JsonObject
();
mainObj
.
add
(
"args"
,
args
);
Callable
<
JsonObject
>
request
=
new
Callable
<
JsonObject
>()
{
public
JsonObject
call
()
throws
Exception
{
return
makeRequest
(
"viewport.mouse.interaction"
,
mainObj
);
}
};
return
requestExecutor
.
submit
(
request
);
}
/*
* Implements a method from ParaViewWebClient.
*/
@Override
public
Future
<
JsonObject
>
call
(
String
method
,
JsonArray
args
)
{
final
String
methodRef
=
method
;
final
JsonObject
reqObj
=
new
JsonObject
();
reqObj
.
add
(
"args"
,
args
);
Callable
<
JsonObject
>
request
=
new
Callable
<
JsonObject
>()
{
public
JsonObject
call
()
throws
Exception
{
return
makeRequest
(
methodRef
,
reqObj
);
}
};
return
requestExecutor
.
submit
(
request
);
}
}
Loading