8000 - Minor code clean-up in the Request class · javaxt-project/javaxt-core@86b6cbd · GitHub
[go: up one dir, main page]

Skip to content

Commit 86b6cbd

Browse files
committed
- Minor code clean-up in the Request class
- Improved support for arrays [] in the JSONObject class git-svn-id: svn://192.168.0.80/JavaXT/javaxt-core@1582 2c7b0aa6-e0b2-3c4e-bb4a-8b65b6c465ff
1 parent d749196 commit 86b6cbd

File tree

2 files changed

+64
-32
lines changed

2 files changed

+64
-32
lines changed

src/javaxt/http/Request.java

Lines changed: 28 additions & 30 deletions
+
List list = (List) requestHeaders.get(key);
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package javaxt.http;
22
import java.io.*;
33
import java.net.*;
4+
import java.util.*;
45
import java.text.DateFormat;
56
import java.text.SimpleDateFormat;
6-
import java.util.List;
7-
import java.util.HashMap;
8-
import java.util.ArrayList;
97
import javax.net.ssl.*;
108

119
//******************************************************************************
@@ -44,11 +42,11 @@ public class Request {
4442
private String password;
4543
private String method;
4644

47-
private java.util.Map<String, List<String>> requestHeaders = null;
45+
private Map<String, List<String>> requestHeaders = null;
4846
private HashMap<String, List<String>> RequestProperties = new HashMap<>();
4947

5048
//Http response properties
51-
private java.util.Map<String, List<String>> headers = null;
49+
private Map<String, List<String>> headers = null;
5250
private String protocol;
5351
private String version;
5452
private int responseCode;
@@ -466,14 +464,14 @@ public void write(javaxt.html.Input[] inputs){
466464

467465
//Generate boundary
468466
String boundary = "---------------------------";
469-
for (int i=0; i<14; i++) boundary += new java.util.Random().nextInt(10);
467+
for (int i=0; i<14; i++) boundary += new Random().nextInt(10);
470468
int boundarySize = boundary.length();
471469

472470
try{
473471

474472
//Compute payload size and generate content metadata for each input
475473
long size = 0;
476-
java.util.ArrayList<byte[]> metadata = new java.util.ArrayList<byte[]>();
474+
ArrayList<byte[]> metadata = new ArrayList<>();
477475
for (int i=0; i<inputs.length; i++){
478476

479477
javaxt.html.Input input = inputs[i];
@@ -555,7 +553,7 @@ public void write(javaxt.html.Input[] inputs){
555553

556554

557555
public List<String> getHeader(String key){
558-
java.util.Iterator<String> it = RequestProperties.keySet().iterator();
556+
Iterator<String> it = RequestProperties.keySet().iterator();
559557
while (it.hasNext()){
560558
String currKey = it.next();
561559
if (key.equalsIgnoreCase(currKey)){
@@ -573,20 +571,20 @@ public List<String> getHeader(String key){
573571
public void setHeader(String key, String value){
574572

575573
boolean foundProperty = false;
576-
java.util.Iterator<String> it = RequestProperties.keySet().iterator();
574+
Iterator<String> it = RequestProperties.keySet().iterator();
577575
while (it.hasNext()){
578576
String currKey = it.next();
579577
if (key.equalsIgnoreCase(currKey)){
580578
foundProperty = true;
581-
List<String> values = new ArrayList<String>();
579+
57AE List<String> values = new ArrayList<>();
582580
values.add(value);
583581
RequestProperties.put(currKey, values);
584582
break;
585583
}
586584
}
587585

588586
if (!foundProperty){
589-
List<String> values = new ArrayList<String>();
587+
List<String> values = new ArrayList<>();
590588
values.add(value);
591589
RequestProperties.put(key, values);
592590
}
@@ -604,13 +602,13 @@ public void addHeader(String key, String value){
604602
}
605603

606604
boolean foundProperty = false;
607-
java.util.Iterator<String> it = RequestProperties.keySet().iterator();
605+
Iterator<String> it = RequestProperties.keySet().iterator();
608606
while (it.hasNext()){
609607
String currKey = it.next();
610608
if (key.equalsIgnoreCase(currKey)){
611609
foundProperty = true;
612610
List<String> values = RequestProperties.get(currKey);
613-
if (values==null) values = new ArrayList<String>();
611+
if (values==null) values = new ArrayList<>();
614612
values.add(value);
615613
RequestProperties.put(currKey, values);
616614
break;
@@ -680,14 +678,14 @@ private URLConnection connect(boolean doOutput){
680678

681679
//Set request method as needed
682680
if (method!=null){
681+
HttpURLConnection con;
683682
if (ssl){
684-
HttpsURLConnection con = (HttpsURLConnection)conn;
685-
con.setRequestMethod(method);
683+
con = (HttpsURLConnection)conn;
686684
}
687685
else{
688-
HttpURLConnection con = (HttpURLConnection)conn;
689-
con.setRequestMethod(method);
686+
con = (HttpURLConnection)conn;
690687
}
688+
con.setRequestMethod(method);
691689
}
692690

693691

@@ -729,7 +727,7 @@ private URLConnection connect(boolean doOutput){
729727
String credentials = getCredentials();
730728
if (credentials!=null) conn.setRequestProperty ("Authorization", "Basic " + credentials);
731729

732-
java.util.Iterator<String> it = RequestProperties.keySet().iterator();
730+
Iterator<String> it = RequestProperties.keySet().iterator();
733731
while (it.hasNext()){
734732
String key = it.next();
735733
List<String> values = RequestProperties.get(key);
@@ -738,7 +736,7 @@ private URLConnection connect(boolean doOutput){
738736
conn.setRequestProperty(key, values.iterator().next());
739737
}
740738
else{
741-
java.util.Iterator<String> value = values.iterator();
739+
Iterator<String> value = values.iterator();
742740
while (value.hasNext()){
743741
conn.addRequestProperty(key, value.next());
744742
}
@@ -858,7 +856,7 @@ private void parseResponse(URLConnection conn){
858856
List status = (List)headers.get(null);
859857
if (status!=null){
860858

861-
java.util.StringTokenizer st = new java.util.StringTokenizer( (String)(status).get(0) );
859+
StringTokenizer st = new StringTokenizer( (String)(status).get(0) );
862860
if (st.hasMoreTokens()) protocol = st.nextToken().trim().toUpperCase();
863861
if (protocol.contains("/")) {
864862
String temp = protocol;
@@ -898,7 +896,7 @@ private Long getExpiration(URLConnection connection, long baseTime) {
898896

899897
String cacheControl = connection.getHeaderField("Cache-Control");
900898
if (cacheControl != null) {
901-
java.util.StringTokenizer tok = new java.util.StringTokenizer(cacheControl, ",");
899+
StringTokenizer tok = new StringTokenizer(cacheControl, ",");
902900
while(tok.hasMoreTokens()) {
903901
String token = tok.nextToken().trim().toLowerCase();
904902
if ("must-revalidate".equals(token)) {
@@ -966,11 +964,11 @@ protected String getResponseMessage(){
966964
}
967965

968966

969-
protected java.util.Map<String, List<String>> getResponseHeaders(){
967+
protected Map<String, List<String>> getResponseHeaders(){
970968
return headers;
971969
}
972970

973-
public java.util.Map<String, List<String>> getRequestHeaders(){
971+
public Map<String, List<String>> getRequestHeaders(){
974972
if (requestHeaders!=null) return requestHeaders;
975973
else{
976974
return RequestProperties;
@@ -983,15 +981,15 @@ protected String[] getResponseHeaders(String headerName){
983981
if (headers==null) return new String[0];
984982

985983
//Iterate through the headers and find the matching header
986-
java.util.ArrayList<String> values = new java.util.ArrayList<String>();
987-
java.util.Iterator<String> it = headers.keySet().iterator();
984+
ArrayList<String> values = new ArrayList<>();
985+
Iterator<String> it = headers.keySet().iterator();
988986
while(it.hasNext()){
989987
String key = it.next();
990988
if (key!=null){
991989
if (key.equalsIgnoreCase(headerName)){
992990

993-
java.util.List<String> list = headers.get(key);
994-
java.util.Iterator<String> val = list.iterator();
991+
List<String> list = headers.get(key);
992+
Iterator<String> val = list.iterator();
995993
while (val.hasNext()){
996994
values.add(val.next());
997995
}
@@ -1119,13 +1117,13 @@ public String toString(){
11191117
//System.out.println("Request Header");
11201118
//System.out.println("------------------------------------------------");
11211119
out.append(url + "\r\n");
1122-
java.util.Map<String,List<String>> requestHeaders = getRequestHeaders();
1120+
Map<String,List<String>> requestHeaders = getRequestHeaders();
11231121
if (requestHeaders!=null){
1124-
java.util.Iterator it = requestHeaders.keySet().iterator();
1122+
Iterator it = requestHeaders.keySet().iterator();
11251123
while(it.hasNext()){
11261124
String key = (String) it.next();
11271125
if (key!=null){
1128-
java.util.List list = (java.util.List) requestHeaders.get(key);
1126
11291127
for (int i=0; i<list.size(); i++){
11301128
String value = list.get(i).toString();
11311129
out.append(key + ": " + value + "\r\n");

src/javaxt/json/JSONObject.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.io.Writer;
55
import java.util.Map.Entry;
6+
import java.lang.reflect.Array;
67

78
//******************************************************************************
89
//** JSONObject
@@ -225,6 +226,19 @@ public void set(String key, Object value) throws JSONException {
225226

226227

227228
if (value!=null) {
229+
230+
231+
if (value.getClass().isArray()){
232+
JSONArray arr = new JSONArray();
233+
for (int i=0; i<Array.getLength(value); i++) {
234+
Object o = Array.get(value, i);
235+
arr.add(o);
236+
}
237+
value = arr;
238+
}
239+
240+
241+
228242
testValidity(value);
229243
super.set(key, value);
230244
}
@@ -347,7 +361,26 @@ else if (value instanceof JSONArray) {
347361
// new JSONArray(value).write(writer, indentFactor, indent);
348362
}
349363
else {
350-
quote(value.toString(), writer);
364+
365+
if (value.getClass().isArray()){
366+
367+
JSONArray arr = new JSONArray();
368+
for (int i=0; i<Array.getLength(value); i++) {
369+
Object o = Array.get(value, i);
370+
arr.add(o);
371+
}
372+
373+
java.io.StringWriter w = new java.io.StringWriter();
374+
synchronized (w.getBuffer()) {
375+
writeValue(w, arr, 0, 0);
376+
writer.write(w.toString());
377+
}
378+
379+
}
380+
else{
381+
382+
quote(value.toString(), writer);
383+
}
351384
}
352385
return writer;
353386
}
@@ -357,7 +390,8 @@ else if (value instanceof JSONArray) {
357390
//** write
358391
//**************************************************************************
359392
private Writer write(Writer writer, int indentFactor, int indent)
360-
throws JSONException {
393+
throws JSONException {
394+
361395
try {
362396
boolean commanate = false;
363397
final int length = this.length();

0 commit comments

Comments
 (0)
0