2020import com .google .gson .JsonParser ;
2121
2222public class Request {
23-
23+
2424 private URL url ;
2525 private RequestMethod method ;
2626 private String parameters ;
2727 private Action action ;
28-
28+
2929 private JsonObject response ;
3030 private boolean hasError ;
3131 private int errorCode = -1 ;
32-
32+
3333 public Request (URL baseUrl , Action action , String ... parameters ) {
3434 try {
35- url = new URL (appendCharacter (baseUrl .toString (), '/' ) + action .toString ());
35+ if (action .method == RequestMethod .GET ){
36+ // Check for ? to see if we need ? or & (non-friendly URL support)
37+ Character appendChar = '?' ;
38+ if (baseUrl .toString ().contains ("?" )){
39+ appendChar = '&' ;
40+ }
41+
42+ url = new URL (appendCharacter (appendCharacter (baseUrl .toString (), '/' ) + action .toString (), appendChar ) + String .join ("&" , parameters ));
43+
44+ } else {
45+ url = new URL (appendCharacter (baseUrl .toString (), '/' ) + action .toString ());
46+ }
3647 } catch (MalformedURLException e ) {
3748 throw new IllegalArgumentException ("URL or action is malformed (" + e .getMessage () + ")" );
3849 }
3950 this .action = action ;
4051 this .method = action .method ;
4152 this .parameters = String .join ("&" , parameters );
4253 }
43-
54+
4455 public boolean hasError () {
4556 return hasError ;
4657 }
47-
58+
4859 public int getError () throws NamelessException {
4960 if (!hasError ) {
5061 throw new NamelessException ("Requested error code but there is no error." );
5162 }
52-
63+
5364 return errorCode ;
5465 }
55-
56- public JsonObject getResponse () throws NamelessException {
66+
67+ public JsonObject getResponse () throws NamelessException {
5768 return response ;
5869 }
59-
70+
6071 public void connect () throws NamelessException {
61-
72+
6273 if (NamelessAPI .DEBUG_MODE ) {
6374 System .out .printf ("NamelessAPI > Making %s request (%s)\n " , action .toString (), method .toString ());
6475 System .out .printf ("NamelessAPI > URL: %s\n " , url );
6576 System .out .printf ("NamelessAPI > Parameters: %s\n " , parameters );
6677 }
67-
78+
6879 try {
6980 if (url .toString ().startsWith ("https://" )){
7081 HttpsURLConnection connection = (HttpsURLConnection ) url .openConnection ();
71-
82+
7283 connection .setRequestMethod (method .toString ());
73- // connection.setRequestProperty("Content-Length", Integer.toString(parameters.length()));
84+ connection .setRequestProperty ("Content-Length" , Integer .toString (parameters .length ()));
7485 connection .setRequestProperty ("Content-Type" , "application/x-www-form-urlencoded" );
7586 connection .setDoOutput (true );
7687 connection .addRequestProperty ("User-Agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" );
77-
88+
89+ if (method == RequestMethod .GET ) {
90+ connection .setRequestMethod ("GET" );
91+ }
92+
7893 DataOutputStream outputStream = new DataOutputStream (connection .getOutputStream ());
7994 BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (outputStream , "UTF-8" ));
8095 writer .write (parameters );
8196 writer .flush ();
8297 writer .close ();
8398 outputStream .close ();
84-
99+
85100 // Initialize input stream
86101 InputStream inputStream = connection .getInputStream ();
87-
102+
88103 // Handle response
89104 BufferedReader streamReader = new BufferedReader (new InputStreamReader (inputStream , "UTF-8" ));
90105 StringBuilder responseBuilder = new StringBuilder ();
91-
106+
92107 String responseString ;
93108 while ((responseString = streamReader .readLine ()) != null )
94109 responseBuilder .append (responseString );
95-
110+
96111 JsonParser parser = new JsonParser ();
97-
112+
98113 response = parser .parse (responseBuilder .toString ()).getAsJsonObject ();
99114
100115 inputStream .close ();
101-
116+
102117 // Disconnect
103118 connection .disconnect ();
104119 } else {
105120 HttpURLConnection connection = (HttpURLConnection ) url .openConnection ();
106-
121+
107122 connection .setRequestMethod (method .toString ());
108- // connection.setRequestProperty("Content-Length", Integer.toString(parameters.length()));
123+ connection .setRequestProperty ("Content-Length" , Integer .toString (parameters .length ()));
109124 connection .setRequestProperty ("Content-Type" , "application/x-www-form-urlencoded" );
110125 connection .setDoOutput (true );
111126 connection .addRequestProperty ("User-Agent" , "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" );
112-
127+
128+ if (method == RequestMethod .GET ) {
129+ connection .setRequestMethod ("GET" );
130+ }
131+
113132 DataOutputStream outputStream = new DataOutputStream (connection .getOutputStream ());
114133 BufferedWriter writer = new BufferedWriter (new OutputStreamWriter (outputStream , "UTF-8" ));
115134 writer .write (parameters );
116135 writer .flush ();
117136 writer .close ();
118137 outputStream .close ();
119-
138+
120139 // Initialize input stream
121140 InputStream inputStream = connection .getInputStream ();
122-
141+
123142 // Handle response
124143 BufferedReader streamReader = new BufferedReader (new InputStreamReader (inputStream , "UTF-8" ));
125144 StringBuilder responseBuilder = new StringBuilder ();
126-
145+
127146 String responseString ;
128147 while ((responseString = streamReader .readLine ()) != null )
129148 responseBuilder .append (responseString );
130-
149+
131150 JsonParser parser = new JsonParser ();
132-
151+
133152 response = parser .parse (responseBuilder .toString ()).getAsJsonObject ();
134153
135154 inputStream .close ();
136-
155+
137156 // Disconnect
138157 connection .disconnect ();
139158 }
140-
159+
141160 hasError = response .get ("error" ).getAsBoolean ();
142161 if (hasError ) {
143162 errorCode = response .get ("code" ).getAsInt ();
@@ -146,17 +165,17 @@ public void connect() throws NamelessException {
146165 throw new NamelessException (e );
147166 }
148167 }
149-
168+
150169 private static String appendCharacter (String string , char c ) {
151170 if (string .endsWith (c + "" )) {
152171 return string ;
153172 } else {
154173 return string + c ;
155174 }
156175 }
157-
176+
158177 public static enum Action {
159-
178+
160179 INFO ("info" , GET ),
161180 GET_ANNOUNCEMENTS ("getAnnouncements" , GET ),
162181 REGISTER ("register" , POST ),
@@ -166,17 +185,17 @@ public static enum Action {
166185 GET_NOTIFICATIONS ("getNotifications" , GET ),
167186 SERVER_INFO ("serverInfo" , POST ),
168187 VALIDATE_USER ("validateUser" , POST ),
169-
188+
170189 ;
171-
190+
172191 RequestMethod method ;
173192 String name ;
174-
193+
175194 Action (String name , RequestMethod method ){
176195 this .name = name ;
177196 this .method = method ;
178197 }
179-
198+
180199 @ Override
181200 public String toString () {
182201 return name ;
@@ -192,11 +211,11 @@ public String toString() {
192211 }*/
193212
194213 }
195-
214+
196215 public static enum RequestMethod {
197-
216+
198217 GET , POST
199-
218+
200219 }
201220
202221}
0 commit comments