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
a72df66f
Commit
a72df66f
authored
Nov 04, 2019
by
Mathieu
Browse files
Not orking /* request with listFiles
parent
e957e667
Changes
2
Hide whitespace changes
Inline
Side-by-side
build.sbt
View file @
a72df66f
...
...
@@ -49,7 +49,9 @@ lazy val server = project.in(file("server")) settings (defaultSettings) settings
"org.json4s"
%%
"json4s-jackson"
%
json4sVersion
,
"com.pauldijou"
%%
"jwt-core"
%
scalaJWTVersion
,
"fr.hmil"
%%
"roshttp"
%
rosHttpVersion
,
"io.skuber"
%%
"skuber"
%
skuberVersion
"org.scalaj"
%%
"scalaj-http"
%
"2.4.2"
,
"io.skuber"
%%
"skuber"
%
skuberVersion
,
"io.suzaku"
%%
"boopickle"
%
"1.2.6"
)
)
dependsOn
(
shared
)
enablePlugins
(
ScalatraPlugin
)
...
...
server/src/main/scala/org/openmole-connect/server/ConnectServlet.scala
View file @
a72df66f
...
...
@@ -15,12 +15,14 @@ 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
org.openmoleconnect.server.DB._
class
ConnectServlet
(
arguments
:
ConnectServer.ServletArguments
)
extends
ScalatraServlet
{
...
...
@@ -32,16 +34,22 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
(
"Access-Control-Allow-Headers"
,
"Content-Type, Accept, X-Requested-With"
)
)
def
waitForGet
(
httpRequest
:
HttpRequest
)
:
SimpleHttpResponse
=
{
Await
.
result
(
httpRequest
.
get
()
,
1
minute
)
def
waitForGet
(
httpRequest
:
HttpRequest
)
=
{
Await
.
result
(
httpRequest
.
get
()
,
1
minute
)
}
def
waitForPost
(
httpRequest
:
HttpRequest
)
=
{
Await
.
result
(
httpRequest
.
withMethod
(
Method
.
POST
).
send
(),
1
minute
)
}
def
waitForPost
(
httpRequest
:
HttpRequest
)
:
SimpleHttpResponse
=
{
def
waitForPost
2
(
httpRequest
:
HttpRequest
)
=
{
Await
.
result
(
httpRequest
.
withMethod
(
Method
.
POST
).
send
()
,
1
minute
)
httpRequest
.
withMethod
(
Method
.
POST
).
send
()
,
1
minute
)
}
...
...
@@ -52,6 +60,7 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
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
)
...
...
@@ -60,8 +69,9 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
}
}
//def withForwardRequest(hostIP: String)(action: HttpRequest => ActionResult): ActionResult = {
def
withForwardRequest
(
hostIP
:
String
)(
action
:
HttpRequest
=>
ActionResult
)
:
ActionResult
=
{
action
(
baseForwardRequest
.
withHost
(
hostIP
).
withPort
(
80
).
withPath
(
""
))
action
(
baseForwardRequest
.
withHost
(
hostIP
).
withPort
(
80
).
withPath
(
""
))
}
def
withAccesToken
(
action
:
TokenData
=>
ActionResult
)
:
Serializable
=
{
...
...
@@ -102,8 +112,17 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
NotFound
()
}
var
incr
=
0
def
toUnsignedByte
(
bytes
:
Array
[
Byte
])
:
Short
=
{
val
aByte
:
Int
=
0xff
&
bytes
(
0
).
asInstanceOf
[
Int
]
aByte
.
asInstanceOf
[
Short
]
}
// OM instance requests
post
(
"/*"
)
{
println
(
"POST /*"
)
withAccesToken
{
tokenData
=>
tokenData
.
host
.
hostIP
.
map
{
hip
=>
withForwardRequest
(
hip
)
{
forwardRequest
=>
...
...
@@ -113,20 +132,98 @@ class ConnectServlet(arguments: ConnectServer.ServletArguments) extends Scalatra
val
bytes
:
Array
[
Byte
]
=
Iterator
.
continually
(
is
.
read
()).
takeWhile
(
_
!=
-
1
).
map
(
_
.
asInstanceOf
[
Byte
]).
toArray
[
Byte
]
val
bb
=
ByteBuffer
.
wrap
(
bytes
)
println
(
"----------- REQUEST "
+
path
)
for
{
b
<-
bytes
}
yield
{
println
(
"B "
+
b
)
}
// val req = waitForPost(
// forwardRequest.withPath(s"/$path").withHeader("Content-Type", "application/octet-stream") //withBody(ByteBufferBody(bb))
// )
//
//
// val reqByte = req.body.asInstanceOf[ByteBuffer]
//
//
// println("Body " + req)
// val data = Array.ofDim[Byte](reqByte.remaining())
// reqByte.get(data)
// // Ok(data)
//
// println("Data " + data)
// if (req.statusCode < 400) Ok(data)
// else NotFound()
val
req
=
waitForPost
(
forwardRequest
.
withPath
(
s
"/$path"
).
withHeader
(
"Content-Type"
,
"application/octet-stream"
).
withBody
(
ByteBufferBody
(
bb
))
)
if
(
req
.
statusCode
<
400
)
Ok
(
req
.
body
)
response
addHeader
(
"Content-Type"
,
"application/octet-stream"
)
println
(
"----------- RESPONSE "
+
path
)
val
is2
=
req
.
body
.
getBytes
//.map{_ & 0xff }
for
{
b
<-
is2
}
yield
{
println
(
"B "
+
b
)
}
println
(
"----------------"
)
val
bbuffer
=
ByteBuffer
.
wrap
(
is2
)
val
data
=
Array
.
ofDim
[
Byte
](
bbuffer
.
remaining
)
val
getdata
=
bbuffer
.
get
(
data
)
// val is2 = req.body.map{TypedArrayBuffer.wrap}
//val is2 = req.body
// println("GET DATA " + getdata)
//
// println("DATA " + data)
// for {
// b <- data
// } yield {
// println("B " + b)
// }
//
// if (incr == 2) {
// println("INCR " + incr)
// println("unpickle: " + Unpickle[ListFilesData].tryFromBytes(data.asInstanceOf[ByteBuffer]))
//
// // val bytes2: Array[Byte] = Iterator.continually(is.read()).takeWhile(_ != -1).map(_.asInstanceOf[Byte]).toArray[Byte]
// // val bb2 = ByteBuffer.wrap(is2)
// } else {
// incr = incr + 1
// }
if
(
req
.
statusCode
<
400
)
Ok
(
data
)
//Ok(is2)
else
NotFound
()
case
None
=>
NotFound
()
}
}
}
}.
getOrElse
(
NotFound
())
}
}
case
class
ListFilesData
(
list
:
Seq
[
TreeNodeData
],
nbFilesOnServer
:
Int
)
case
class
DirData
(
isEmpty
:
Boolean
)
case
class
TreeNodeData
(
name
:
String
,
dirData
:
Option
[
DirData
],
size
:
Long
,
time
:
Long
)
post
(
connectionRoute
)
{
Authentication
.
isValid
(
request
,
TokenType
.
accessToken
)
match
{
case
false
=>
...
...
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