Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dummyhttpserver
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Yoshiharu Hirose
dummyhttpserver
Commits
2002efe5
Commit
2002efe5
authored
Mar 24, 2016
by
Yoshiharu Hirose
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ダミーHTTPサーバ実装
parent
e0a4a1b4
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
76 additions
and
0 deletions
+76
-0
DummyHttpServer.scala
src/main/scala/DummyHttpServer.scala
+76
-0
No files found.
src/main/scala/DummyHttpServer.scala
0 → 100644
View file @
2002efe5
import
java.io._
import
java.net.InetSocketAddress
import
com.sun.net.httpserver.
{
HttpExchange
,
HttpHandler
,
HttpServer
}
import
scala.collection.JavaConversions._
object
DummyHttpServer
{
val
BASEDIR
=
"""D:\project\nifty\clisdk\dev\iaas-sdk\src\test\resources"""
val
PORTS
=
Array
(
80
,
8080
,
38080
)
def
main
(
args
:
Array
[
String
])
{
PORTS
.
foreach
(
startServer
)
}
/**
* サーバ起動
* @param port 待ち受けポート番号
*/
def
startServer
(
port
:
Int
)
{
val
server
=
HttpServer
.
create
(
new
InetSocketAddress
(
port
),
10
)
server
.
createContext
(
"/"
,
new
DummyHttpServer
.
MyHandler
)
server
.
start
()
}
/**
* ハンドラ
*/
class
MyHandler
extends
HttpHandler
{
def
handle
(
t
:
HttpExchange
)
:
Unit
=
{
println
(
"------------------------------------------------------------"
)
printf
(
"%s %s\n"
,
t
.
getRequestMethod
,
t
.
getRequestURI
)
t
.
getRequestHeaders
.
entrySet
().
foreach
{
ent
=>
ent
.
getValue
.
foreach
{
v
=>
printf
(
"%s: %s\n"
,
ent
.
getKey
,
v
)
}
}
findFile
(
new
File
(
t
.
getRequestURI
.
getPath
).
getName
,
new
File
(
BASEDIR
))
match
{
case
Some
(
file
)
=>
t
.
sendResponseHeaders
(
200
,
file
.
length
)
val
is
=
new
BufferedInputStream
(
new
FileInputStream
(
file
))
val
os
=
t
.
getResponseBody
val
buf
=
new
Array
[
Byte
](
file
.
length
.
toInt
)
is
.
read
(
buf
)
os
.
write
(
buf
)
os
.
close
()
is
.
close
()
printf
(
" -> 200 (%s)\n"
,
file
.
getAbsolutePath
)
case
_
=>
t
.
sendResponseHeaders
(
404
,
0
)
println
(
" -> 404"
)
}
t
.
close
()
}
/**
* ファイル検索
* @param filename ファイル名
* @param dir 検索対象ディレクトリ
* @return ファイル
*/
def
findFile
(
filename
:
String
,
dir
:
File
)
:
Option
[
File
]
=
{
dir
.
listFiles
.
foreach
{
f
=>
if
(
f
.
isDirectory
)
{
val
file
=
findFile
(
filename
,
f
)
if
(
file
.
isDefined
)
{
return
file
}
}
else
if
(
f
.
isFile
&&
f
.
getName
==
filename
)
{
return
Some
(
f
)
}
}
None
}
}
}
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