Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
openmole
openmole-connect
Commits
40c84e90
Commit
40c84e90
authored
Nov 07, 2019
by
Mathieu
Browse files
Get rid off ros.http library, fix downloads
parent
fea3a484
Changes
2
Hide whitespace changes
Inline
Side-by-side
build.sbt
View file @
40c84e90
...
...
@@ -13,6 +13,7 @@ val scalajsDomVersion = "0.9.7"
val
scalaJWTVersion
=
"4.0.0"
val
rosHttpVersion
=
"2.2.4"
val
skuberVersion
=
"2.2.0"
val
httpComponentsVersion
=
"4.5.9"
val
Resolvers
=
Seq
(
Resolver
.
sonatypeRepo
(
"snapshots"
),
"Typesafe repository"
at
"http://repo.typesafe.com/typesafe/releases/"
,
...
...
@@ -48,10 +49,9 @@ lazy val server = project.in(file("server")) settings (defaultSettings) settings
"org.eclipse.jetty"
%
"jetty-server"
%
jettyVersion
,
"org.json4s"
%%
"json4s-jackson"
%
json4sVersion
,
"com.pauldijou"
%%
"jwt-core"
%
scalaJWTVersion
,
"fr.hmil"
%%
"roshttp"
%
rosHttpVersion
,
"org.scalaj"
%%
"scalaj-http"
%
"2.4.2"
,
"io.skuber"
%%
"skuber"
%
skuberVersion
,
"io.
suzaku"
%%
"boopickle"
%
"1.3.1
"
,
"io.
monix"
%%
"monix"
%
"3.0.0
"
,
"org.apache.httpcomponents"
%
"httpclient"
%
"4.5.9"
,
"org.apache.httpcomponents"
%
"httpmime"
%
"4.5.9"
,
)
...
...
server/src/main/scala/org/openmole-connect/server/ConnectServlet.scala
View file @
40c84e90
package
org.openmoleconnect.server
import
java.n
io.
{
ByteBuffer
,
ByteOrder
}
import
java.n
et.URI
import
fr.hmil.roshttp.Method
import
fr.hmil.roshttp.body.
{
ByteBufferBody
,
PlainTextBody
}
import
javax.servlet.http.HttpServletRequest
import
org.openmoleconnect.server.JWT._
import
org.scalatra._
import
scala.concurrent.Await
import
scala.collection.JavaConversions._
import
scalatags.Text.all._
import
scalatags.Text.
{
all
=>
tags
}
import
scala.concurrent.duration._
import
shared.Data._
import
boopickle.Default._
import
fr.hmil.roshttp.HttpRequest
import
monix.execution.Scheduler.Implicits.global
import
fr.hmil.roshttp.response.SimpleHttpResponse
import
javax.servlet.ServletInputStream
import
org.apache.commons.io.IOUtils
import
org.apache.http.client.HttpClient
import
org.apache.http.client.methods.HttpPost
import
org.apache.http.client.methods.
{
HttpGet
,
HttpPost
}
import
org.apache.http.client.utils.URIBuilder
import
org.apache.http.entity.InputStreamEntity
import
org.apache.http.impl.client.HttpClients
import
org.openmoleconnect.server.DB._
class
ConnectServlet
(
arguments
:
ConnectServer.ServletArguments
)
extends
ScalatraServlet
{
...
...
@@ -34,51 +25,15 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
implicit
val
secret
:
JWT.Secret
=
arguments
.
secret
val
allowHeaders
=
Seq
(
(
"Access-Control-Allow-Origin"
,
"*"
),
(
"Access-Control-Allow-Methods"
,
"POST, GET, PUT, UPDATE, OPTIONS"
),
(
"Access-Control-Allow-Headers"
,
"Content-Type, Accept, X-Requested-With"
)
)
val
httpClient
=
HttpClients
.
createDefault
()
def
waitForGet
(
httpRequest
:
HttpRequest
)
=
{
Await
.
result
(
httpRequest
.
get
()
,
1
minute
)
}
def
waitForPost
(
httpRequest
:
HttpRequest
)
=
{
Await
.
result
(
httpRequest
.
withMethod
(
Method
.
POST
).
send
(),
1
minute
)
}
def
waitForPost2
(
httpRequest
:
HttpRequest
)
=
{
Await
.
result
(
httpRequest
.
withMethod
(
Method
.
POST
).
send
(),
1
minute
)
}
val
baseForwardRequest
=
HttpRequest
()
.
withProtocol
(
fr
.
hmil
.
roshttp
.
Protocol
.
HTTP
)
def
uriBuilder
(
hostIP
:
String
,
path
:
String
)
=
new
URIBuilder
()
.
setScheme
(
"http"
)
.
setHost
(
hostIP
)
.
setPort
(
80
)
.
setPath
(
path
)
def
headers
(
request
:
HttpServletRequest
)
=
request
.
getHeaderNames
.
map
{
hn
=>
hn
->
request
.
getHeader
(
hn
)
}.
toSeq
def
proxyRequest
(
hostIP
:
Option
[
String
])
=
{
hostIP
.
map
{
hip
=>
println
(
"HIP "
+
hip
)
withForwardRequest
(
hip
)
{
forwardRequest
=>
val
req
=
forwardRequest
.
withHeaders
((
headers
(
request
))
:
_
*
).
withHeaders
(
allowHeaders
:
_
*
)
val
fR
=
waitForGet
(
req
)
Ok
(
fR
.
body
,
fR
.
headers
)
}
}
}
//def withForwardRequest(hostIP: String)(action: HttpRequest => ActionResult): ActionResult = {
def
withForwardRequest
(
hostIP
:
String
)(
action
:
HttpRequest
=>
ActionResult
)
:
ActionResult
=
{
action
(
baseForwardRequest
.
withHost
(
hostIP
).
withPort
(
80
).
withPath
(
""
))
}
def
uri
(
hostIP
:
String
,
path
:
String
)
=
uriBuilder
(
hostIP
,
path
).
build
()
def
withAccesToken
(
action
:
TokenData
=>
ActionResult
)
:
Serializable
=
{
Authentication
.
tokenData
(
request
,
TokenType
.
accessToken
)
match
{
...
...
@@ -105,7 +60,10 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
def
connectionAppRedirection
=
{
withAccesToken
{
tokenData
=>
proxyRequest
(
tokenData
.
host
.
hostIP
).
getOrElse
(
NotFound
())
tokenData
.
host
.
hostIP
.
map
{
hip
=>
getFromHip
(
hip
)
Ok
()
}.
getOrElse
(
NotFound
())
}
}
...
...
@@ -122,40 +80,25 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
post
(
"/*"
)
{
withAccesToken
{
tokenData
=>
tokenData
.
host
.
hostIP
.
map
{
hip
=>
// withForwardRequest(hip) { forwardRequest =>
val
forwardRequest
=
baseForwardRequest
.
withHost
(
hip
).
withPort
(
80
).
withPath
(
""
)
multiParams
(
"splat"
).
headOption
match
{
case
Some
(
path
)
=>
val
is
=
request
.
getInputStream
println
(
"request: "
+
s
"http://$hip/$path"
)
val
uri
=
new
URIBuilder
()
.
setScheme
(
"http"
)
.
setHost
(
hip
)
.
setPort
(
80
)
.
setPath
(
path
)
.
build
()
val
httpPost
=
new
HttpPost
(
uri
)
val
httpPost
=
new
HttpPost
(
uri
(
hip
,
path
))
httpPost
.
setEntity
(
new
InputStreamEntity
(
is
))
val
filtred
=
Seq
(
"Content-Length"
)
request
.
getHeaderNames
.
filter
(
n
=>
!
filtred
.
contains
(
n
)).
foreach
{
n
=>
httpPost
.
setHeader
(
n
,
request
.
getHeader
(
n
))
}
// Add timeout
val
client
=
HttpClients
.
createDefault
()
val
forwardResponse
=
client
.
execute
(
httpPost
)
// TODO: Add timeout
val
forwardResponse
=
httpClient
.
execute
(
httpPost
)
response
.
setStatus
(
forwardResponse
.
getStatusLine
.
getStatusCode
)
IOUtils
.
copy
(
forwardResponse
.
getEntity
.
getContent
,
response
.
getOutputStream
())
Ok
()
case
None
=>
NotFound
()
// }
}
}.
getOrElse
(
NotFound
())
}
...
...
@@ -219,32 +162,38 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
}
else
{
withAccesToken
{
tokenData
=>
tokenData
.
host
.
hostIP
.
map
{
hip
=>
withForwardRequest
(
hip
)
{
forwardRequest
=>
Ok
(
waitForGet
(
forwardRequest
.
withHeader
(
"Content-Type"
,
requestContentType
).
withPath
(
s
"$path"
)
).
body
)
val
u
=
uriBuilder
(
hip
,
path
)
request
.
getParameterNames
.
foreach
{
pn
=>
u
.
addParameter
(
pn
,
request
.
getParameter
(
pn
))
}
getFromURI
(
u
.
build
(),
requestContentType
)
response
Ok
()
}.
getOrElse
(
NotFound
())
}
}
}
get
(
"/js/*.*"
)
{
getResource
(
request
.
uri
.
getPath
,
"application/javascript"
)
}
get
(
"/css/*.*"
)
{
getResource
(
request
.
uri
.
getPath
,
"text/css"
)
def
getFromURI
(
uri
:
URI
,
requestContentType
:
String
)
:
Int
=
{
val
httpGet
=
new
HttpGet
(
uri
)
httpGet
.
setHeader
(
"Content-Type"
,
requestContentType
)
val
forwardResponse
=
httpClient
.
execute
(
httpGet
)
response
.
setStatus
(
forwardResponse
.
getStatusLine
.
getStatusCode
)
IOUtils
.
copy
(
forwardResponse
.
getEntity
.
getContent
,
response
.
getOutputStream
())
}
get
(
"/img/*.*"
)
{
getResource
(
request
.
uri
.
getPath
,
request
.
contentType
.
getOrElse
(
""
))
def
getFromHip
(
hip
:
String
)
:
Int
=
{
getFromURI
(
uri
(
hip
,
""
),
"html"
)
}
get
(
"/
fonts/*.
*"
)
{
getResource
(
request
.
uri
.
getPath
,
request
.
c
ontentType
.
getOrElse
(
""
)
)
get
(
"/*"
)
{
getResource
(
request
.
uri
.
getPath
,
request
.
getC
ontentType
)
}
get
(
"/"
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment