Commit 23f3b1b4 authored by 9731301's avatar 9731301

use packages and add parserClass and SaverClass and RequestDataClass

parent 557c0d2d
<component name="ArtifactManager">
<artifact type="jar" name="Insomnia:jar">
<output-path>$PROJECT_DIR$/out/artifacts/Insomnia_jar</output-path>
<root id="archive" name="Insomnia.jar">
<element id="module-output" name="Insomnia" />
</root>
</artifact>
</component>
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Manifest-Version: 1.0
Main-Class: Insomnia
import java.awt.*;
import java.io.IOException;
public class Insomnia {
public static void main(String [] args) throws IOException {
InsomniaGUI insomniaGUI = new InsomniaGUI();
Frame frame = new Frame();
}
}
Manifest-Version: 1.0
Main-Class: Insomnia
import java.io.*;
import java.net.*;
import java.util.HashMap;
public class Run {
public static void bufferOutFormData(HashMap<String, String> body, String boundary, BufferedOutputStream bufferedOutputStream) throws IOException {
for (String key : body.keySet()) {
bufferedOutputStream.write(("--" + boundary + "\r\n").getBytes());
if (key.contains("file")) {
bufferedOutputStream.write(("Content-Disposition: form-data; filename=\"" + (new File(body.get(key))).getName() + "\"\r\nContent-Type: Auto\r\n\r\n").getBytes());
try {
BufferedInputStream tempBufferedInputStream = new BufferedInputStream(new FileInputStream(new File(body.get(key))));
byte[] filesBytes = tempBufferedInputStream.readAllBytes();
bufferedOutputStream.write(filesBytes);
bufferedOutputStream.write("\r\n".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
} else {
bufferedOutputStream.write(("Content-Disposition: form-data; name=\"" + key + "\"\r\n\r\n").getBytes());
bufferedOutputStream.write((body.get(key) + "\r\n").getBytes());
}
}
bufferedOutputStream.write(("--" + boundary + "--\r\n").getBytes());
bufferedOutputStream.flush();
bufferedOutputStream.close();
}
public static void uploadBinary() {
try {
URL url = new URL("http://apapi.haditabatabaei.ir/tests/post/binary");
File haditabatabaei = new File("haditabatabaei.txt");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/octet-stream");
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(connection.getOutputStream());
BufferedInputStream fileInputStream = new BufferedInputStream(new FileInputStream(haditabatabaei));
bufferedOutputStream.write(fileInputStream.readAllBytes());
bufferedOutputStream.flush();
bufferedOutputStream.close();
BufferedInputStream bufferedInputStream = new BufferedInputStream(connection.getInputStream());
System.out.println(new String(bufferedInputStream.readAllBytes()));
System.out.println(connection.getResponseCode());
System.out.println(connection.getHeaderFields());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void formData() {
HashMap<String, String> fooBody = new HashMap<>();
fooBody.put("name", "hadi");
fooBody.put("lastName", "tabatabaei");
fooBody.put("file", "pic2.png");
fooBody.put("file2", "result.png");
try {
URL url = new URL("http://apapi.haditabatabaei.ir/tests/post/formdata");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String boundary = System.currentTimeMillis() + "";
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
BufferedOutputStream request = new BufferedOutputStream(connection.getOutputStream());
bufferOutFormData(fooBody, boundary, request);
BufferedInputStream bufferedInputStream = new BufferedInputStream(connection.getInputStream());
System.out.println(new String(bufferedInputStream.readAllBytes()));
System.out.println(connection.getResponseCode());
System.out.println(connection.getHeaderFields());
} catch (Exception e) {
}
}
public static void main(String[] args) {
uploadBinary();
formData();
}
}
\ No newline at end of file
package com.insomnia.GUI;
/** /**
* this Class represents the GUI for Insomnia * this Class represents the GUI for Insomnia
* @zahra_Fatehi * @zahra_Fatehi
...@@ -34,10 +35,6 @@ public class InsomniaGUI { ...@@ -34,10 +35,6 @@ public class InsomniaGUI {
LeftPanel leftPanel = new LeftPanel(); LeftPanel leftPanel = new LeftPanel();
leftPanel.createLeftPanel(); leftPanel.createLeftPanel();
//menu upper_Items //menu upper_Items
JMenuBar upperMenuBar = new JMenuBar(); JMenuBar upperMenuBar = new JMenuBar();
JMenu application = new JMenu("Application"); JMenu application = new JMenu("Application");
...@@ -186,6 +183,7 @@ public class InsomniaGUI { ...@@ -186,6 +183,7 @@ public class InsomniaGUI {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
JPanel aboutPanel = new JPanel(); JPanel aboutPanel = new JPanel();
JTextArea aboutText = new JTextArea(); JTextArea aboutText = new JTextArea();
aboutText.setLineWrap(true);
aboutText.setEditable(false); aboutText.setEditable(false);
aboutText.setText("developer\tzahra seyedFatehi \nEmail \ts.fatehi1378@Gmail.com\nstudent number\t9731301"); aboutText.setText("developer\tzahra seyedFatehi \nEmail \ts.fatehi1378@Gmail.com\nstudent number\t9731301");
aboutPanel.add(aboutText); aboutPanel.add(aboutText);
...@@ -205,6 +203,7 @@ public class InsomniaGUI { ...@@ -205,6 +203,7 @@ public class InsomniaGUI {
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
JPanel helpPanel = new JPanel(); JPanel helpPanel = new JPanel();
JTextArea helpText = new JTextArea(); JTextArea helpText = new JTextArea();
helpText.setLineWrap(true);
helpPanel.add(helpText); helpPanel.add(helpText);
frame.setLocation(100,100); frame.setLocation(100,100);
frame.setPreferredSize(new Dimension(200,200)); frame.setPreferredSize(new Dimension(200,200));
......
package com.insomnia.GUI;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.Border; import javax.swing.border.Border;
import java.awt.*; import java.awt.*;
......
package com.insomnia.GUI;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.Border; import javax.swing.border.Border;
...@@ -12,12 +14,12 @@ import java.io.IOException; ...@@ -12,12 +14,12 @@ import java.io.IOException;
public class RequestPanel extends JPanel { public class RequestPanel extends JPanel {
private VerticalFlowLayout vfLayout = new VerticalFlowLayout(); private VerticalFlowLayout vfLayout = new VerticalFlowLayout();
private JPanel body = new JPanel(); private JPanel body = new JPanel();
private JPanel auth = new JPanel(); private JPanel auth = new JPanel();
private JPanel query = new JPanel(); private JPanel query = new JPanel();
private JPanel mainCenterHeader = new JPanel(vfLayout); private JPanel mainCenterHeader = new JPanel(vfLayout);
private JScrollPane scrollPane_Header_Request = new JScrollPane(mainCenterHeader); private JScrollPane scrollPane_Header_Request = new JScrollPane(mainCenterHeader);
JPanel docs = new JPanel(); JPanel docs = new JPanel();
JTabbedPane tabbedPaneCenterRequest = new JTabbedPane(); JTabbedPane tabbedPaneCenterRequest = new JTabbedPane();
JTextField headerField = new JTextField("header"); JTextField headerField = new JTextField("header");
......
package com.insomnia.GUI;
import javax.swing.*; import javax.swing.*;
import javax.swing.border.Border; import javax.swing.border.Border;
import java.awt.*; import java.awt.*;
...@@ -5,20 +7,21 @@ import java.awt.event.MouseEvent; ...@@ -5,20 +7,21 @@ import java.awt.event.MouseEvent;
import java.awt.event.MouseListener; import java.awt.event.MouseListener;
public class RespondPanel extends JPanel { public class RespondPanel extends JPanel {
private Border border = BorderFactory.createLineBorder(Color.BLACK,3); private Border border = BorderFactory.createLineBorder(Color.BLACK,3);
private VerticalFlowLayout vfLayout = new VerticalFlowLayout(); private VerticalFlowLayout vfLayout = new VerticalFlowLayout();
private JPanel row = new JPanel(); private JPanel row = new JPanel();
private JTextArea massageBody = new JTextArea(); private JTextArea massageBody = new JTextArea();
private JPanel mainCenterHeader_respond = new JPanel(vfLayout); private JPanel mainCenterHeader_respond = new JPanel(vfLayout);
private JScrollPane scrollPane_Header_Respond = new JScrollPane(mainCenterHeader_respond); private JScrollPane scrollPane_Header_Respond = new JScrollPane(mainCenterHeader_respond);
private JPanel header_MainPanel = new JPanel(new BorderLayout()); private JPanel header_MainPanel = new JPanel(new BorderLayout());
private JButton copyToClipBoard = new JButton("copy to clip board"); private JButton copyToClipBoard = new JButton("copy to clip board");
private JPanel cookie = new JPanel(); private JPanel cookie = new JPanel();
private JPanel timeline = new JPanel(new BorderLayout()); private JPanel timeline = new JPanel(new BorderLayout());
private JTabbedPane tabbedPaneCenterResponse = new JTabbedPane(); private JTabbedPane tabbedPaneCenterResponse = new JTabbedPane();
private JTextField headerField2 = new JTextField("header"); private JTextField headerField2 = new JTextField("header");
public JTabbedPane tabbedPaneCenterResponse() { public JTabbedPane tabbedPaneCenterResponse() {
massageBody.setLineWrap(true);
massageBody.setPreferredSize(new Dimension(200, 300)); massageBody.setPreferredSize(new Dimension(200, 300));
row.add(massageBody); row.add(massageBody);
row.setBackground(Color.GRAY); row.setBackground(Color.GRAY);
......
package com.insomnia.GUI;
/** /**
* This class represents a layout that works like flow layout but in vertical side * This class represents a layout that works like flow layout but in vertical side
......
package com.insomnia.parser;
import java.util.ArrayList;
import java.util.HashMap;
public class Parser {
static ArrayList<String> fixArgs(String[] args){
ArrayList<String> args2 = new ArrayList<>();
String f = "";
for (String arg : args){
if (arg.trim().startsWith("-")){
if (!f.isEmpty()) args2.add(f.trim());
f = arg.trim()+" ";
}else{
f = f + arg.trim()+" ";
}
}
if (!f.isEmpty()) args2.add(f.trim());
return args2;
}
public static RequestData parse(String[] args){
RequestData requestData = new RequestData();
for (String arg: fixArgs(args)) {
String a = findArg(arg).trim();
String paramName,paramValue="";
if (a.contains(" ")){
paramName = a.substring(0,a.indexOf(" ")).trim();
paramValue = findParamValue(a.substring(a.indexOf(" ")+1).trim());
}else{
paramName = a;
}
switch (paramName.toLowerCase()) {
case "url":
requestData.setUrl(paramValue);
break;
case "method":
case "m":
requestData.setMethod(findMethod(paramValue));
break;
case "headers":
case "h":
if (paramValue.length()>2){
HashMap<String,String> headers = new HashMap<>();
if (!paramValue.contains(";")){
String[] h1 = paramValue.split(":");
headers.put(h1[0],h1[1]);
}else{
String[] mHeaders = paramValue.split(";");
for (String h : mHeaders){
String[] h0 = h.split(":");
headers.put(h0[0],h0[1]);
}
}
requestData.setHeaders(headers);
}
break;
case "i":
requestData.setShowResponseHeaders(true);
break;
case "f":
requestData.setFollowRedirects(true);
break;
case "output":
case "o":
requestData.setOutput( paramValue.isEmpty() ? "-t" : paramValue);
break;
case "save":
case "s":
requestData.setSaveRequest(true);
break;
case "json":
case "j":
requestData.setJson(true);
break;
case "data":
case "d":
requestData.setBody(paramValue);
break;
case "upload":
requestData.setUploadPath(paramValue);
}
}
return requestData;
}
static String findMethod(String v){
switch (v.toUpperCase().trim()){
case "GET":
case "POST":
case "DELETE":
case "PUT":
return v.toUpperCase().trim();
default:
return RequestData.GET;
}
}
static String findParamValue(String v){
if (v.startsWith("\"") && v.endsWith("\"")){
return v.substring(1,v.length()-1);
}
return v;
}
static String findArg (String v){
if (v.startsWith("--")){
return v.substring(2);
}else if (v.startsWith("-")){
return v.substring(1);
}
return v;
}
}
/**
* RequestData represents a class which contains the data for request
*
* @author : zahra_seyedfatehi
* version: 0.0
*/
package com.insomnia.parser;
import java.util.HashMap;
public class RequestData {
//methods requests
public static String GET = "GET", POST = "POST", DELETE="DELETE", PUT="PUT";
//address of request
private String url = "",method = GET;
private boolean followRedirects = false;
private HashMap<String,String> headers = null;
private boolean showResponseHeaders = false;
private String output = "-null";
private boolean saveRequest = false;
private boolean isJson = false;
private String body = "";
private String uploadPath = "";
public boolean isJson() {
return isJson;
}
public void setJson(boolean json) {
isJson = json;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getUploadPath() {
return uploadPath;
}
public void setUploadPath(String uploadPath) {
this.uploadPath = uploadPath;
}
public boolean isShowResponseHeaders() {
return showResponseHeaders;
}
public void setShowResponseHeaders(boolean showResponseHeaders) {
this.showResponseHeaders = showResponseHeaders;
}
public String getOutput() {
return output;
}
public void setOutput(String output) {
this.output = output;
}
public boolean isSaveRequest() {
return saveRequest;
}
public void setSaveRequest(boolean saveRequest) {
this.saveRequest = saveRequest;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public boolean isFollowRedirects() {
return followRedirects;
}
public void setFollowRedirects(boolean followRedirects) {
this.followRedirects = followRedirects;
}
public HashMap<String, String> getHeaders() {
return headers;
}
public void setHeaders(HashMap<String, String> headers) {
this.headers = headers;
}
}
package com.insomnia.parser;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Saver {
private static final String appName = "Insomnia";
private static int os = 0;
public static String getDirApp(){
return System.getProperty("user.dir");
}
public static String getOutputDir(){
File f = new File(getDirApp(),"Outputs");
f.mkdir();
return f.toString();
}
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();
}
/**
* @return saved file name or empty if file didn't save
*/
public static String saveRequest (RequestData data) {
int size = new File(getRequestsDir()).list().length + 1;
String fileName = "Request "+size+".insomnia";
File file = new File(getRequestsDir(),fileName);
boolean fileFound = file.exists();
if (fileFound) {
while (fileFound) {
size++;
fileName = "Request "+size+".insomnia";
file = new File(getRequestsDir(),fileName);
fileFound = file.exists();
}
}
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fo);
out.writeObject(data);
out.close();
fo.close();
return fileName;
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
public static RequestData readRequest (String fileName){
try {
FileInputStream file = new FileInputStream(new File(getRequestsDir(),fileName));
ObjectInputStream in = new ObjectInputStream(file);
RequestData data = (RequestData) in.readObject();
in.close();
file.close();
return data;
} catch(IOException | ClassNotFoundException e) {
e.printStackTrace();
}
return null;
}
public static File[] getRequestFiles(){
return new File(getRequestsDir()).listFiles();
}
}
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