Skip to content

Commit 57a274c

Browse files
override response hot-fix
1 parent d9b54e3 commit 57a274c

File tree

6 files changed

+1204
-1233
lines changed

6 files changed

+1204
-1233
lines changed

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# Authors
22
- [Contentstack](https://www.contentstack.com/)
3+
- [Shailesh-Mishra](shailesh.mishra@contentstack.com)

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11

22
## CHANGELOG
33

4+
5+
## Version 1.3.3
6+
### Date: 21-June-2019
7+
Override response hot-fix
8+
9+
------------------------------------------------
10+
411
## Version 1.3.2
512
### Date: 13-May-2019
613
Removed println

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>com.contentstack.sdk</groupId>
88
<artifactId>java</artifactId>
9-
<version>1.3.2</version>
9+
<version>1.3.3-SNAPSHOT</version>
1010
<packaging>jar</packaging>
1111

1212
<name>contentstack-java</name>

src/main/java/com/contentstack/sdk/CSHttpConnection.java

Lines changed: 25 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,7 @@ private String getParams(HashMap<String, Object> params) {
242242
@Override
243243
public void send() {
244244

245-
String url = null;
246-
String httpsORhttp = CSAppConstants.URLSCHEMA_HTTPS;
247-
int requestId = getRequestId(requestMethod);
248-
final LinkedHashMap<String, String> headers = new LinkedHashMap<>();
249-
int count = this.headers.size();
250-
245+
String url = null;
251246
if(requestMethod == CSAppConstants.RequestMethod.GET){
252247
String params = setFormParamsGET(formParams);
253248
if( params != null) {
@@ -260,140 +255,54 @@ public void send() {
260255
url = urlPath;
261256
}
262257

263-
if(url.contains(CSAppConstants.URLSCHEMA_HTTPS)){
264-
httpsORhttp = CSAppConstants.URLSCHEMA_HTTPS;
265-
}
266-
267-
for (Map.Entry<String, Object> entry : this.headers.entrySet())
268-
{
269-
String key = entry.getKey();
270-
String value = (String) entry.getValue();
271-
headers.put(key, value);
272-
}
273-
274-
headers.put("Content-Type", "application/json");
275-
headers.put("User-Agent", defaultUserAgent()+"/"+ CSAppConstants.SDK_VERSION);
276-
277258
try {
278-
requestJSON.put("_method",getRequestMethod().toString());
279-
this.callNetworkRequest(url, getRequestMethod().toString(), requestJSON, headers);
280-
281-
} catch (IOException | JSONException e) {
282-
Stack.log(TAG,"Localized Message : "+e.getLocalizedMessage());
259+
sendGET(url);
260+
} catch (IOException e) {
261+
e.printStackTrace();
262+
} catch (JSONException e) {
283263
e.printStackTrace();
284264
}
285265
}
286266

287267

288268

289-
private String getRequest( String urlString, String requestMethod ) {
290-
291-
try {
292-
URL url = new URL(urlString);
293-
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
294-
conn.setRequestMethod(requestMethod);
295-
conn.setRequestProperty("Accept", "application/json");
296-
297-
conn.connect();
269+
private void sendGET(String GET_URL) throws IOException, JSONException {
298270

299-
BufferedReader br = new BufferedReader(new InputStreamReader(
300-
conn.getInputStream(), "UTF-8"));
271+
URL obj = new URL(GET_URL);
272+
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
273+
con.setRequestMethod("GET");
301274

302-
StringBuilder sb = new StringBuilder(2048);
303-
for (String line; (line = br.readLine()) != null; ) {
304-
sb.append(line);
305-
}
306-
conn.disconnect();
307-
308-
return sb.toString();
309-
310-
} catch (IOException ex) {
311-
Logger.getLogger(CSHttpConnection.class.getName()).log(Level.SEVERE, null, ex);
312-
return ex.toString();
275+
if (this.headers.containsKey("api_key")){
276+
con.setRequestProperty("api_key", headers.get("api_key").toString());
313277
}
314-
}
315-
316-
317-
318-
private void callNetworkRequest(String url, String requestMethod, JSONObject jsonParam, Map<String, String> reqHeaders) throws IOException {
319-
320-
URL objURL = new URL(url);
321-
HttpURLConnection con = (HttpURLConnection) objURL.openConnection();
322-
con.setRequestMethod(requestMethod);
323-
324-
StringBuilder stringBuilder = new StringBuilder();
325-
326-
for (Map.Entry<String, String> entry : reqHeaders.entrySet())
327-
{
328-
String key = entry.getKey();
329-
String value = String.valueOf(entry.getValue());
330-
con.setRequestProperty(key, value);
331-
stringBuilder.append(key+":"+value+",");
278+
if (this.headers.containsKey("access_token")){
279+
con.setRequestProperty("access_token", headers.get("access_token").toString());
332280
}
333-
334-
con.setDoOutput(true);
335-
con.setDoInput(true);
336-
337-
OutputStream outputStream = con.getOutputStream();
338-
String paramString = jsonParam.toString().trim();
339-
340-
if (!paramString.isEmpty())
341-
{
342-
byte[] postParams = paramString.getBytes("UTF-8");
343-
outputStream.write(postParams);
281+
if (this.headers.containsKey("environment")){
282+
con.setRequestProperty("environment", headers.get("environment").toString());
344283
}
345-
346-
outputStream.flush();
347-
outputStream.close();
284+
con.setRequestProperty("X-User-Agent", defaultUserAgent()+"/"+ CSAppConstants.SDK_VERSION);
285+
con.setRequestProperty("Content-Type", "application/json");
348286
int responseCode = con.getResponseCode();
349287

350-
if (responseCode == HttpURLConnection.HTTP_OK)
351-
{
352-
BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
288+
if (responseCode == HttpURLConnection.HTTP_OK) { // success
289+
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
353290
String inputLine;
354291
StringBuffer response = new StringBuffer();
355-
while ((inputLine = inputStreamReader.readLine()) != null) {
292+
while ((inputLine = in.readLine()) != null) {
356293
response.append(inputLine);
357294
}
295+
in.close();
358296

359-
inputStreamReader.close();
360-
361-
try {
362-
responseJSON = new JSONObject(response.toString());
363-
} catch (JSONException e) {
364-
e.printStackTrace();
365-
}
366-
367-
if (responseJSON != null) {
368-
connectionRequest.onRequestFinished(CSHttpConnection.this);
369-
}
370-
297+
responseJSON = new JSONObject(response.toString());
298+
connectionRequest.onRequestFinished(CSHttpConnection.this);
371299
} else {
300+
Stack.log("CSHttpConnection", "GET request not worked");
372301
generateBuiltError(con);
373302
}
374303

375-
376-
}
377-
378-
379-
380-
private int getRequestId(CSAppConstants.RequestMethod requestMethod) {
381-
382-
switch (requestMethod){
383-
case GET:
384-
return 0;
385-
case POST:
386-
return 1;
387-
case PUT:
388-
return 2;
389-
case DELETE:
390-
return 3;
391-
default:
392-
return 1;
393-
}
394304
}
395305

396-
397306
private String defaultUserAgent() {
398307
String agent = System.getProperty("http.agent");
399308
return agent != null ? agent : ("Java" + System.getProperty("java.version"));
@@ -412,13 +321,10 @@ private void generateBuiltError(HttpURLConnection connection){
412321
String responseMessage = connection.getResponseMessage();
413322
statusCode = connection.getResponseCode();
414323

415-
416324
try {
417325

418326
if (connection.getResponseMessage() == null && connection.getResponseMessage().equalsIgnoreCase("")) {
419327
statusCode = connection.getResponseCode();
420-
//String responseBody = new String(error.networkResponse.data, "utf-8");
421-
//responseJSON = responseBody != null ? new JSONObject(responseBody) : new JSONObject();
422328
responseJSON.put("error_message", CSAppConstants.ErrorMessage_Default);
423329

424330
}else{
@@ -448,15 +354,12 @@ private void generateBuiltError(HttpURLConnection connection){
448354
responseJSON.put("error_message", CSAppConstants.ErrorMessage_VolleyServerError);
449355

450356
} else {
451-
if (responseMessage != null) {
452-
responseJSON.put("error_message", responseMessage);
453-
}
357+
responseJSON.put("error_message", responseMessage);
454358
}
455359

456360
JSONObject jsonObject = new JSONObject();
457361
jsonObject.put("errors", responseMessage.toString());
458362
responseJSON.put("errors", jsonObject);
459-
460363
}
461364
connectionRequest.onRequestFailed(responseJSON, statusCode, callBackObject);
462365

0 commit comments

Comments
 (0)