Commit 2e18f338 authored by 9731301's avatar 9731301

add RequestLoaderClass

parent c3a90235
...@@ -7,9 +7,7 @@ ...@@ -7,9 +7,7 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="f2dd20fb-7e97-49ef-9dfe-5631626d7912" name="Default Changelist" comment=""> <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$/.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" /> <change beforePath="$PROJECT_DIR$/src/com/insomnia/parser/Saver.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/com/insomnia/parser/Saver.java" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
...@@ -113,6 +111,8 @@ ...@@ -113,6 +111,8 @@
<workItem from="1590502223611" duration="8513000" /> <workItem from="1590502223611" duration="8513000" />
<workItem from="1590582948373" duration="9875000" /> <workItem from="1590582948373" duration="9875000" />
<workItem from="1590600904796" duration="12014000" /> <workItem from="1590600904796" duration="12014000" />
<workItem from="1590623093663" duration="5335000" />
<workItem from="1590682418201" duration="308000" />
</task> </task>
<servers /> <servers />
</component> </component>
...@@ -163,10 +163,10 @@ ...@@ -163,10 +163,10 @@
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="249" y="0" key="SettingsEditor/0.0.1536.824@0.0.1536.824" timestamp="1590496679192" /> <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="1590600909312"> <state x="425" y="237" key="com.intellij.ide.util.TipDialog" timestamp="1590757579198">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
<state x="425" y="237" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1590600909312" /> <state x="425" y="237" key="com.intellij.ide.util.TipDialog/0.0.1536.824@0.0.1536.824" timestamp="1590757579198" />
<state x="499" y="179" key="extract.method.dialog" timestamp="1590145349556"> <state x="499" y="179" key="extract.method.dialog" timestamp="1590145349556">
<screen x="0" y="0" width="1536" height="824" /> <screen x="0" y="0" width="1536" height="824" />
</state> </state>
......
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();
}
}
...@@ -11,8 +11,8 @@ import java.util.Date; ...@@ -11,8 +11,8 @@ import java.util.Date;
public class Saver { public class Saver {
private static final String appName = "Insomnia"; private static final String appName = "Insomnia";
private static int os = 0;
//get address of app
public static String getDirApp(){ public static String getDirApp(){
return System.getProperty("user.dir"); return System.getProperty("user.dir");
} }
...@@ -24,16 +24,6 @@ public class Saver { ...@@ -24,16 +24,6 @@ public class Saver {
} }
public static String getRequestsDir(){ 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"); File f = new File(getDirApp(),"Requests");
f.mkdir(); f.mkdir();
return f.toString(); return f.toString();
...@@ -63,11 +53,9 @@ public class Saver { ...@@ -63,11 +53,9 @@ public class Saver {
out.writeObject(data); out.writeObject(data);
out.close(); out.close();
fo.close(); fo.close();
return fileName; return fileName;
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
return ""; return "";
} }
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment