Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
MidTerm
Project
Project
Details
Activity
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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
9731301
MidTerm
Commits
2e18f338
Commit
2e18f338
authored
May 29, 2020
by
9731301
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add RequestLoaderClass
parent
c3a90235
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
198 additions
and
17 deletions
+198
-17
workspace.xml
.idea/workspace.xml
+4
-4
RequestLoader.java
src/com/insomnia/parser/RequestLoader.java
+193
-0
Saver.java
src/com/insomnia/parser/Saver.java
+1
-13
No files found.
.idea/workspace.xml
View file @
2e18f338
...
...
@@ -7,9 +7,7 @@
</component>
<component
name=
"ChangeListManager"
>
<list
default=
"true"
id=
"f2dd20fb-7e97-49ef-9dfe-5631626d7912"
name=
"Default Changelist"
comment=
""
>
<change
afterPath=
"$PROJECT_DIR$/src/com/insomnia/parser/ResponseData.java"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/.idea/workspace.xml"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/.idea/workspace.xml"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/src/com/insomnia/parser/Parser.java"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/src/com/insomnia/parser/Parser.java"
afterDir=
"false"
/>
<change
beforePath=
"$PROJECT_DIR$/src/com/insomnia/parser/Saver.java"
beforeDir=
"false"
afterPath=
"$PROJECT_DIR$/src/com/insomnia/parser/Saver.java"
afterDir=
"false"
/>
</list>
<option
name=
"SHOW_DIALOG"
value=
"false"
/>
...
...
@@ -113,6 +111,8 @@
<workItem
from=
"1590502223611"
duration=
"8513000"
/>
<workItem
from=
"1590582948373"
duration=
"9875000"
/>
<workItem
from=
"1590600904796"
duration=
"12014000"
/>
<workItem
from=
"1590623093663"
duration=
"5335000"
/>
<workItem
from=
"1590682418201"
duration=
"308000"
/>
</task>
<servers
/>
</component>
...
...
@@ -163,10 +163,10 @@
<screen
x=
"0"
y=
"0"
width=
"1536"
height=
"824"
/>
</state>
<state
x=
"249"
y=
"0"
key=
"SettingsEditor/0.0.1536.824@0.0.1536.824"
timestamp=
"1590496679192"
/>
<state
x=
"425"
y=
"237"
key=
"com.intellij.ide.util.TipDialog"
timestamp=
"1590
600909312
"
>
<state
x=
"425"
y=
"237"
key=
"com.intellij.ide.util.TipDialog"
timestamp=
"1590
757579198
"
>
<screen
x=
"0"
y=
"0"
width=
"1536"
height=
"824"
/>
</state>
<state
x=
"425"
y=
"237"
key=
"com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824"
timestamp=
"1590
600909312
"
/>
<state
x=
"425"
y=
"237"
key=
"com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824"
timestamp=
"1590
757579198
"
/>
<state
x=
"499"
y=
"179"
key=
"extract.method.dialog"
timestamp=
"1590145349556"
>
<screen
x=
"0"
y=
"0"
width=
"1536"
height=
"824"
/>
</state>
...
...
src/com/insomnia/parser/RequestLoader.java
0 → 100644
View file @
2e18f338
package
com
.
insomnia
.
parser
;
import
java.io.BufferedInputStream
;
import
java.io.BufferedReader
;
import
java.io.DataOutputStream
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.InputStreamReader
;
import
java.io.OutputStream
;
import
java.net.HttpURLConnection
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.util.HashMap
;
public
class
RequestLoader
{
private
static
String
twoHyphens
=
"--"
;
private
static
String
crlf
=
"\r\n"
;
public
static
ResponseData
run
(
RequestData
data
){
String
boundary
=
"*****"
+
System
.
currentTimeMillis
()
+
"*****"
;
ResponseData
responseData
=
new
ResponseData
(
data
);
if
(
data
.
getUrl
().
isEmpty
()){
responseData
.
setSyntaxError
(
true
);
responseData
.
setSyntaxErrorLog
(
"enter url"
);
return
responseData
.
finish
();
}
if
(
data
.
isJson
()
){
responseData
.
setSyntaxError
(
true
);
responseData
.
setSyntaxErrorLog
(
"enter a valid json data"
);
return
responseData
.
finish
();
}
File
file
=
null
;
if
(!
data
.
getUploadPath
().
isEmpty
()){
file
=
new
File
(
data
.
getUploadPath
());
if
(!
file
.
exists
()){
responseData
.
setSyntaxError
(
true
);
responseData
.
setSyntaxErrorLog
(
"enter a valid upload path"
);
return
responseData
.
finish
();
}
}
try
{
String
mUrl
=
data
.
getUrl
();
if
(
data
.
getUrl
().
equals
(
RequestData
.
GET
))
{
if
(
mUrl
.
contains
(
"?"
))
{
mUrl
=
mUrl
+
"&"
+
data
.
getBody
();
}
else
{
mUrl
=
mUrl
+
"?"
+
data
.
getBody
();
}
}
URL
url
=
new
URL
(
mUrl
);
HttpURLConnection
con
=
(
HttpURLConnection
)
url
.
openConnection
();
con
.
setUseCaches
(
false
);
if
(
data
.
getMethod
().
equals
(
RequestData
.
POST
)
&&
(!
data
.
getBody
().
isEmpty
()||!
data
.
getUploadPath
().
isEmpty
())
){
con
.
setDoOutput
(
true
);
}
con
.
setDoInput
(
true
);
con
.
setInstanceFollowRedirects
(
data
.
isFollowRedirects
());
con
.
setRequestMethod
(
data
.
getMethod
());
if
(
data
.
getHeaders
()!=
null
){
for
(
String
key
:
data
.
getHeaders
().
keySet
()){
con
.
setRequestProperty
(
key
,
data
.
getHeaders
().
get
(
key
));
}
}
if
(
data
.
isJson
()){
con
.
setRequestProperty
(
"Content-Type"
,
"application/json; charset=UTF-8"
);
}
con
.
setRequestProperty
(
"Connection"
,
"Keep-Alive"
);
con
.
setRequestProperty
(
"Cache-Control"
,
"no-cache"
);
if
(!
data
.
getUploadPath
().
isEmpty
()){
con
.
setRequestProperty
(
"Content-Type"
,
"multipart/form-data;boundary="
+
boundary
);
}
if
(
con
.
getDoOutput
()){
if
(!
data
.
getUploadPath
().
isEmpty
()){
DataOutputStream
request
=
new
DataOutputStream
(
con
.
getOutputStream
());
if
(!
data
.
getBody
().
isEmpty
())
{
HashMap
<
String
,
String
>
fields
=
new
HashMap
<>();
if
(!
data
.
getBody
().
contains
(
";"
))
{
String
[]
f1
=
data
.
getBody
().
split
(
":"
);
fields
.
put
(
f1
[
0
],
f1
[
1
]);
}
else
{
String
[]
mFields
=
data
.
getBody
().
split
(
";"
);
for
(
String
f
:
mFields
)
{
String
[]
f0
=
f
.
split
(
":"
);
fields
.
put
(
f0
[
0
],
f0
[
1
]);
}
}
for
(
String
key
:
fields
.
keySet
())
{
request
.
writeBytes
(
twoHyphens
+
boundary
+
crlf
);
request
.
writeBytes
(
"Content-Disposition: form-data; name=\""
+
key
+
"\""
+
crlf
);
request
.
writeBytes
(
"Content-Type: text/plain; charset=UTF-8"
+
crlf
);
request
.
writeBytes
(
crlf
);
request
.
writeBytes
(
fields
.
get
(
key
)+
crlf
);
request
.
flush
();
}
}
String
fieldName
=
"file"
;
String
fileName
=
file
.
getName
();
request
.
writeBytes
(
twoHyphens
+
boundary
+
crlf
);
request
.
writeBytes
(
"Content-Disposition: form-data; name=\""
+
fieldName
+
"\";filename=\""
+
fileName
+
"\""
+
crlf
);
request
.
writeBytes
(
crlf
);
FileInputStream
fileInputStream
=
new
FileInputStream
(
file
);
int
bytesRead
,
bytesAvailable
,
bufferSize
;
byte
[]
buffer
;
int
maxBufferSize
=
1
*
1024
*
1024
;
bytesAvailable
=
fileInputStream
.
available
();
bufferSize
=
Math
.
min
(
bytesAvailable
,
maxBufferSize
);
buffer
=
new
byte
[
bufferSize
];
bytesRead
=
fileInputStream
.
read
(
buffer
,
0
,
bufferSize
);
while
(
bytesRead
>
0
)
{
request
.
write
(
buffer
,
0
,
bufferSize
);
bytesAvailable
=
fileInputStream
.
available
();
bufferSize
=
Math
.
min
(
bytesAvailable
,
maxBufferSize
);
bytesRead
=
fileInputStream
.
read
(
buffer
,
0
,
bufferSize
);
}
request
.
writeBytes
(
crlf
);
request
.
writeBytes
(
twoHyphens
+
boundary
+
twoHyphens
+
crlf
);
request
.
flush
();
request
.
close
();
}
else
{
OutputStream
os
=
con
.
getOutputStream
();
os
.
write
(
data
.
getBody
().
getBytes
(
"UTF-8"
));
os
.
close
();
}
}
int
status
=
con
.
getResponseCode
();
responseData
.
setStatus
(
status
);
if
(
status
==
HttpURLConnection
.
HTTP_OK
)
{
InputStream
responseStream
=
new
BufferedInputStream
(
con
.
getInputStream
());
responseData
.
setInputStream
(
responseStream
);
responseData
.
setResponseHeaders
(
con
.
getHeaderFields
());
responseData
.
setSuccessful
(
true
);
//Save files
Saver
.
saveResponse
(
responseData
);
if
(
data
.
isSaveRequest
())
Saver
.
saveRequest
(
data
);
responseData
.
res
=
responseData
.
convertStreamToString
(
responseStream
,
true
);
con
.
disconnect
();
}
else
{
responseData
.
setSuccessful
(
false
);
if
(
con
.
getErrorStream
()!=
null
)
{
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
((
con
.
getErrorStream
())));
StringBuilder
builder
=
new
StringBuilder
();
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
builder
.
append
(
line
);
}
responseData
.
setErrorMessage
(
builder
.
toString
());
}
}
}
catch
(
MalformedURLException
e
)
{
responseData
.
setSyntaxError
(
true
);
responseData
.
setSyntaxErrorLog
(
"enter a valid url"
);
return
responseData
.
finish
();
}
catch
(
IOException
e
)
{
responseData
.
setSuccessful
(
false
);
responseData
.
setErrorMessage
(
e
.
getMessage
());
}
return
responseData
.
finish
();
}
}
src/com/insomnia/parser/Saver.java
View file @
2e18f338
...
...
@@ -11,8 +11,8 @@ import java.util.Date;
public
class
Saver
{
private
static
final
String
appName
=
"Insomnia"
;
private
static
int
os
=
0
;
//get address of app
public
static
String
getDirApp
(){
return
System
.
getProperty
(
"user.dir"
);
}
...
...
@@ -24,16 +24,6 @@ public class Saver {
}
public
static
String
getRequestsDir
(){
if
(
os
==
0
)
{
String
s
=
System
.
getProperty
(
"os.name"
,
""
).
toLowerCase
();
os
=
s
.
contains
(
"win"
)
?
1
:
2
;
}
if
(
os
==
1
)
{
File
res
=
new
File
(
System
.
getenv
(
"AppData"
),
appName
);
res
.
mkdir
();
return
res
.
toString
();
}
File
f
=
new
File
(
getDirApp
(),
"Requests"
);
f
.
mkdir
();
return
f
.
toString
();
...
...
@@ -63,11 +53,9 @@ public class Saver {
out
.
writeObject
(
data
);
out
.
close
();
fo
.
close
();
return
fileName
;
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
return
""
;
}
}
...
...
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