/* * get a list of request to show in the fragment * */ public ArrayList<InternetUiRequest> getUiRequests() { ArrayList<InternetUiRequest> requestsUI = new ArrayList<>(); Enumeration e = InternetManager.getInstance().getRequests().keys(); while (e.hasMoreElements()) { Integer i = (Integer) e.nextElement(); InternetRequest mainReq = InternetManager.getInstance().getRequest(i); InternetUiRequest req = new InternetUiRequest(); req.setId(mainReq.getId()); req.setUrl(mainReq.getUrl()); req.setContentType(mainReq.getContentType()); req.setParams(mainReq.getParamsAsMap()); req.setHeaders(mainReq.getHeadersAsMap()); req.setStatus(mainReq.getStatus()); if (mainReq.isCancelled()) req.setCancelled(); ArrayList<Pair<String, String>> children = new ArrayList<>(); children.add(new Pair<>("URL", req.getUrl())); InternetResponse res = mainReq.getResponse(); if (mainReq.getStatus() == InternetRequest.REQUEST_STATUS.EXECUTED && res != null && res.getResponseBody() != null && res.getResponseBody().length > 0) { String response = new String(res.getResponseBody()); children.add( new Pair<>( "Response", response != null && response.length() >= 30 ? response.substring(0, 30) + "..." : response)); } children.add( new Pair<>( "Content Type", req.getContentType() != null && req.getContentType().trim().length() > 0 ? req.getContentType() : "No Content Type")); if (InternetManager.getInstance().getBasicAuth() != null) children.add( new Pair<>( "Authentication", InternetManager.getInstance().getBasicAuth().first + " : " + InternetManager.getInstance().getBasicAuth().second)); else children.add(new Pair<>("Authentication", "No Authentication")); String params = "No Parameters"; if (req.getParams() != null && req.getParamsAsMap().size() > 0) { params = ""; for (String key : req.getParamsAsMap().keySet()) { params += key + " : " + req.getParamsAsMap().get(key) + "\n"; } } children.add(new Pair<>("Parameters", params)); String headers = "No Headers"; if (req.getHeaders() != null) { headers = ""; for (Header header : req.getHeaders()) { headers += header.getName() + " : " + header.getValue() + "\n"; } } children.add(new Pair<>("Headers", headers)); req.setUiChildren(children); requestsUI.add(req); } return requestsUI; }
@Override public void onNewShieldFrameReceived(ShieldFrame frame) { if (frame.getShieldId() == UIShield.INTERNET_SHIELD.getId()) { switch (frame.getFunctionId()) { case REQUEST.NEW_REQUEST: requestID = frame.getArgumentAsInteger(0); final InternetRequest request = new InternetRequest(); request.setId(requestID); String url = frame.getArgumentAsString(1); request.setUrl(url); request.setCallback( new CallBack() { @Override public void onSuccess( int statusCode, Header[] headers, byte[] responseBody, int requestID) { ShieldFrame frame1 = new ShieldFrame(SHIELD_ID, REQUEST.ON_SUCCESS); frame1.addIntegerArgument(2, requestID); // /0=id frame1.addIntegerArgument(2, statusCode); // frame1.addIntegerArgument(4, responseBody != null ? responseBody.length : 0); InternetResponse response = InternetManager.getInstance().getRequest(requestID).getResponse(); if (response != null) { frame1.addArgument( response .getBytes(0, InternetManager.getInstance().getMaxSentBytes()) .getArray()); sendShieldFrame(frame1, true); } } @Override public void onFailure( int statusCode, Header[] headers, byte[] responseBody, Throwable error, int requestID) { ShieldFrame frame1 = new ShieldFrame(SHIELD_ID, REQUEST.ON_FAILURE); frame1.addIntegerArgument(2, requestID); frame1.addIntegerArgument(2, statusCode); // frame1.addIntegerArgument(4, responseBody != null ? responseBody.length : 0); InternetResponse response = InternetManager.getInstance().getRequest(requestID).getResponse(); if (response != null) { frame1.addArgument( response .getBytes(0, InternetManager.getInstance().getMaxSentBytes()) .getArray()); sendShieldFrame(frame1, true); } } @Override public void onFinish(final int requestID) { ShieldFrame frame1 = new ShieldFrame(SHIELD_ID, REQUEST.ON_FINISH); frame1.addIntegerArgument(2, requestID); if (request .getRegisteredCallbacks() .contains(InternetRequest.CALLBACK.ON_SUCCESS.name()) || request .getRegisteredCallbacks() .contains(InternetRequest.CALLBACK.ON_FAILURE.name())) queueShieldFrame(frame1); else sendShieldFrame(frame1, true); } @Override public void onStart(int requestID) { ShieldFrame frame1 = new ShieldFrame(SHIELD_ID, REQUEST.ON_START); frame1.addIntegerArgument(2, requestID); sendShieldFrame(frame1, true); } }); InternetManager.getInstance().putRequest(requestID, request); break; case REQUEST.SET_URL: requestID = frame.getArgumentAsInteger(0); String reqURL = frame.getArgumentAsString(1); if (!reqURL.contains(" ")) { if (InternetManager.getInstance().getRequest(requestID) != null) InternetManager.getInstance().getRequest(requestID).setUrl(reqURL); } else { ShieldFrame frame1 = new ShieldFrame(SHIELD_ID, INTERNET.ON_ERROR); frame1.addIntegerArgument(2, requestID); frame1.addIntegerArgument(1, INTERNET.URL_IS_WRONG); sendShieldFrame(frame1, true); } break; case REQUEST.ADD_HEADER: requestID = frame.getArgumentAsInteger(0); if (InternetManager.getInstance().getRequest(requestID) != null) InternetManager.getInstance() .getRequest(requestID) .addHeader(frame.getArgumentAsString(1), frame.getArgumentAsString(2)); break; case REQUEST.ADD_PARAM: requestID = frame.getArgumentAsInteger(0); if (InternetManager.getInstance().getRequest(requestID) != null) InternetManager.getInstance() .getRequest(requestID) .addParam(frame.getArgumentAsString(1), frame.getArgumentAsString(2)); break; case REQUEST.DEL_ALL_HEADERS: requestID = frame.getArgumentAsInteger(0); if (InternetManager.getInstance().getRequest(requestID) != null) InternetManager.getInstance().getRequest(requestID).removeAllHeaders(); break; case REQUEST.DEL_ALL_PARAMS: requestID = frame.getArgumentAsInteger(0); if (InternetManager.getInstance().getRequest(requestID) != null) InternetManager.getInstance().getRequest(requestID).removeAllParams(); break; case REQUEST.SET_CONTENT_TYPE: requestID = frame.getArgumentAsInteger(0); if (InternetManager.getInstance().getRequest(requestID) != null) InternetManager.getInstance() .getRequest(requestID) .setContentType(frame.getArgumentAsString(1)); break; case REQUEST.IGNORE_RESPONSE: requestID = frame.getArgumentAsInteger(0); if (InternetManager.getInstance().getRequest(requestID) != null) InternetManager.getInstance().getRequest(requestID).ignoreResponse(); break; //////// INTERNET case INTERNET.GET_REQUEST: requestID = frame.getArgumentAsInteger(0); InternetManager.EXECUTION_TYPE getExecutionType = null; try { getExecutionType = InternetManager.getInstance() .execute(requestID, InternetManager.REQUEST_TYPE.GET, frame.getArgument(1)[0]); // request excutes only in case of success if (getExecutionType != InternetManager.EXECUTION_TYPE.SUCCESSFUL) { ShieldFrame frame1 = new ShieldFrame(SHIELD_ID, INTERNET.ON_ERROR); frame1.addIntegerArgument(2, requestID); frame1.addIntegerArgument(1, getExecutionType.value); sendShieldFrame(frame1, true); } } catch (UnsupportedEncodingException e) { // dummy catch that only used for requests the accept custom entity ShieldFrame entityError = new ShieldFrame(SHIELD_ID, INTERNET.ON_ERROR); entityError.addIntegerArgument(2, requestID); entityError.addIntegerArgument(1, RESPONSE.UNSUPPORTED_ENTITY_ENCODING); sendShieldFrame(entityError, true); e.printStackTrace(); } break; case INTERNET.POST_REQUEST: requestID = frame.getArgumentAsInteger(0); try { InternetManager.EXECUTION_TYPE postExecutionType = InternetManager.getInstance() .execute(requestID, InternetManager.REQUEST_TYPE.POST, frame.getArgument(1)[0]); if (postExecutionType != InternetManager.EXECUTION_TYPE.SUCCESSFUL) { ShieldFrame frame1 = new ShieldFrame(SHIELD_ID, INTERNET.ON_ERROR); frame1.addIntegerArgument(2, requestID); frame1.addIntegerArgument(1, postExecutionType.value); // /0=id sendShieldFrame(frame1, true); } } catch (UnsupportedEncodingException e) { ShieldFrame entityError = new ShieldFrame(SHIELD_ID, INTERNET.ON_ERROR); entityError.addIntegerArgument(2, requestID); entityError.addIntegerArgument(1, RESPONSE.UNSUPPORTED_ENTITY_ENCODING); sendShieldFrame(entityError, true); e.printStackTrace(); } break; case INTERNET.PUT_REQUEST: requestID = frame.getArgumentAsInteger(0); try { InternetManager.EXECUTION_TYPE putExecutionType = InternetManager.getInstance() .execute(requestID, InternetManager.REQUEST_TYPE.PUT, frame.getArgument(1)[0]); if (putExecutionType != InternetManager.EXECUTION_TYPE.SUCCESSFUL) { ShieldFrame frame1 = new ShieldFrame(SHIELD_ID, INTERNET.ON_ERROR); frame1.addIntegerArgument(2, requestID); frame1.addIntegerArgument(1, putExecutionType.value); // /0=id sendShieldFrame(frame1, true); } } catch (UnsupportedEncodingException e) { ShieldFrame entityError = new ShieldFrame(SHIELD_ID, INTERNET.ON_ERROR); entityError.addIntegerArgument(2, requestID); entityError.addIntegerArgument(1, 6); sendShieldFrame(entityError, true); e.printStackTrace(); } break; case INTERNET.DELETE_REQUEST: requestID = frame.getArgumentAsInteger(0); try { InternetManager.EXECUTION_TYPE deleteExecutionType = InternetManager.getInstance() .execute( requestID, InternetManager.REQUEST_TYPE.DELETE, frame.getArgument(1)[0]); if (deleteExecutionType != InternetManager.EXECUTION_TYPE.SUCCESSFUL) { ShieldFrame frame1 = new ShieldFrame(SHIELD_ID, INTERNET.ON_ERROR); frame1.addIntegerArgument(2, requestID); frame1.addIntegerArgument(1, deleteExecutionType.value); // /0=id sendShieldFrame(frame1, true); } } catch (UnsupportedEncodingException e) { ShieldFrame entityError = new ShieldFrame(SHIELD_ID, INTERNET.ON_ERROR); entityError.addIntegerArgument(2, requestID); entityError.addIntegerArgument(1, 6); sendShieldFrame(entityError, true); e.printStackTrace(); } break; case INTERNET.CANCEL_ALL_REQUESTS: InternetManager.getInstance().cancelAllRequests(); break; case INTERNET.SET_BASIC_AUTH: InternetManager.getInstance() .setBasicAuth(new Pair<>(frame.getArgumentAsString(0), frame.getArgumentAsString(1))); break; case INTERNET.CLEAR_BASIC_AUTH: InternetManager.getInstance().clearBasicAuth(); break; case INTERNET.SET_DEFAULT_MAX_RESPONSE_BYTES_COUNT: InternetManager.getInstance().setMaxSentBytes(frame.getArgumentAsInteger(0)); break; /////// RESPONSE case RESPONSE.DISPOSE: // remove response from database requestID = frame.getArgumentAsInteger(0); InternetManager.getInstance().disponseResponse(requestID); break; case RESPONSE.GET_NEXT_BYTES: // get a byte array for the response from index to another requestID = frame.getArgumentAsInteger(0); if (InternetManager.getInstance().getRequest(requestID) != null) { InternetResponse response = InternetManager.getInstance().getRequest(requestID).getResponse(); if (response != null) { final InternetResponse.ResponseBodyBytes bodyBytes = response.getBytes( frame.getArgumentAsInteger(4, 1), frame.getArgumentAsInteger(2)); if (bodyBytes.getBytes_status() == InternetResponse.RESPONSE_BODY_BYTES.NOT_ENOUGH_BYTES) { if (bodyBytes.getArray() != null && bodyBytes.getArray().length > 0) { ShieldFrame frameSent = new ShieldFrame(SHIELD_ID, RESPONSE.SEND_GET_NEXT_BYTES); frameSent.addIntegerArgument(2, requestID); frameSent.addArgument(bodyBytes.getArray()); sendShieldFrame(frameSent, true); ShieldFrame frameSentNotEnough = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frameSentNotEnough.addIntegerArgument(2, requestID); frameSentNotEnough.addIntegerArgument(1, RESPONSE.NO_ENOUGH_BYTES); queueShieldFrame(frameSentNotEnough); } } else if (bodyBytes.getBytes_status() == InternetResponse.RESPONSE_BODY_BYTES.INDEX_GREATER_THAN_LENGTH || bodyBytes.getBytes_status() == InternetResponse.RESPONSE_BODY_BYTES.INDEX_LESS_THAN_0) { ShieldFrame frameSentIndexOut = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frameSentIndexOut.addIntegerArgument(2, requestID); frameSentIndexOut.addIntegerArgument(1, RESPONSE.INDEX_OUT_OF_BOUNDS); sendShieldFrame(frameSentIndexOut, true); } else if (bodyBytes.getBytes_status() == InternetResponse.RESPONSE_BODY_BYTES.COUNT_LESS_THAN_0) { ShieldFrame frameSentcountOut = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frameSentcountOut.addIntegerArgument(2, requestID); frameSentcountOut.addIntegerArgument(1, RESPONSE.SIZE_OF_REQUEST_CAN_NOT_BE_ZERO); sendShieldFrame(frameSentcountOut, true); } else { // successfully getten fully if (bodyBytes.getArray() != null && bodyBytes.getArray().length > 0) { ShieldFrame frameSent = new ShieldFrame(SHIELD_ID, RESPONSE.SEND_GET_NEXT_BYTES); frameSent.addIntegerArgument(2, requestID); frameSent.addArgument(bodyBytes.getArray()); sendShieldFrame(frameSent, true); } } } else { // no response ShieldFrame frameSentNotRes = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frameSentNotRes.addIntegerArgument(2, requestID); frameSentNotRes.addIntegerArgument(1, RESPONSE.REQUEST_HAS_NO_RESPONSE); sendShieldFrame(frameSentNotRes, true); } } else // no request { ShieldFrame frameSentNotReq = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frameSentNotReq.addIntegerArgument(2, requestID); frameSentNotReq.addIntegerArgument(1, RESPONSE.RESPONSE_CAN_NOT_BE_FOUND); sendShieldFrame(frameSentNotReq, true); } break; case RESPONSE.GET_HEADER: requestID = frame.getArgumentAsInteger(0); String key = frame.getArgumentAsString(1); ShieldFrame frame2 = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frame2.addIntegerArgument(2, requestID); if (InternetManager.getInstance().getRequest(requestID) != null) { InternetResponse response = InternetManager.getInstance().getRequest(requestID).getResponse(); if (response != null) { if (response.getHeaders() == null || response.getHeaders().get(key) == null) { frame2.addIntegerArgument(1, RESPONSE.HEADER_CAN_NOT_BE_FOUND); sendShieldFrame(frame2, true); } else { ShieldFrame frameSent = new ShieldFrame(SHIELD_ID, RESPONSE.SEND_GET_HEADER); frameSent.addIntegerArgument(2, requestID); frameSent.addStringArgument(key); frameSent.addStringArgument(response.getHeaders().get(key)); sendShieldFrame(frameSent, true); } } else { // no response frame2.addIntegerArgument(1, RESPONSE.REQUEST_HAS_NO_RESPONSE); sendShieldFrame(frame2, true); } } else // no request { frame2.addIntegerArgument(1, RESPONSE.RESPONSE_CAN_NOT_BE_FOUND); sendShieldFrame(frame2, true); } break; case RESPONSE.GET_JSON_RESPONSE: requestID = frame.getArgumentAsInteger(0); if (InternetManager.getInstance().getRequest(requestID) != null) { InternetResponse response = InternetManager.getInstance().getRequest(requestID).getResponse(); if (response != null) { final ArrayList<InternetResponse.JsonNode> jsonNodes = response.getNodes(frame); if (jsonNodes.size() > 0) { try { String result = response.getValueOf( jsonNodes.get(0).getDataType() == InternetResponse.JsonNode.NODE_DATA_TYPE.ARRAY ? new JSONArray(new String(response.getResponseBody())) : new JSONObject(new String(response.getResponseBody())), jsonNodes); ShieldFrame frameJsonSent = new ShieldFrame(SHIELD_ID, RESPONSE.RESPONSE_JSON); frameJsonSent.addIntegerArgument(2, requestID); frameJsonSent.addStringArgument(result); frameJsonSent.addArgument(frame.getArgument(1)); for (int arg = 2; arg < frame.getArguments().size(); arg++) { frameJsonSent.addArgument(frame.getArgument(arg)); } sendShieldFrame(frameJsonSent, true); } catch (JSONException e) { ShieldFrame frameJson = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frameJson.addIntegerArgument(2, requestID); frameJson.addIntegerArgument(1, RESPONSE.JSON_KEYCHAIN_IS_WRONG); sendShieldFrame(frameJson, true); } catch (ClassCastException e) { ShieldFrame frameJson = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frameJson.addIntegerArgument(2, requestID); frameJson.addIntegerArgument(1, RESPONSE.JSON_KEYCHAIN_IS_WRONG); sendShieldFrame(frameJson, true); } } else { ShieldFrame frameJson = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frameJson.addIntegerArgument(2, requestID); frameJson.addIntegerArgument(1, RESPONSE.JSON_KEYCHAIN_IS_WRONG); sendShieldFrame(frameJson, true); } } else { // no response ShieldFrame frameJson = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frameJson.addIntegerArgument(2, requestID); frameJson.addIntegerArgument(1, RESPONSE.REQUEST_HAS_NO_RESPONSE); sendShieldFrame(frameJson, true); } } else // no request { ShieldFrame frameJson = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frameJson.addIntegerArgument(2, requestID); frameJson.addIntegerArgument(1, RESPONSE.RESPONSE_CAN_NOT_BE_FOUND); sendShieldFrame(frameJson, true); } break; case RESPONSE.GET_JSON_ARRAY_LENGTH: requestID = frame.getArgumentAsInteger(0); if (InternetManager.getInstance().getRequest(requestID) != null) { InternetResponse response = InternetManager.getInstance().getRequest(requestID).getResponse(); if (response != null) { final ArrayList<InternetResponse.JsonNode> jsonNodes = response.getNodes(frame); if (jsonNodes.size() > 0) { try { int result = response.getJSONArrayLength( jsonNodes.get(0).getDataType() == InternetResponse.JsonNode.NODE_DATA_TYPE.ARRAY ? new JSONArray(new String(response.getResponseBody())) : new JSONObject(new String(response.getResponseBody())), jsonNodes); if (result == -1) { ShieldFrame frameJson = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frameJson.addIntegerArgument(2, requestID); frameJson.addIntegerArgument(1, RESPONSE.JSON_KEYCHAIN_IS_WRONG); sendShieldFrame(frameJson, true); } else { ShieldFrame frameJsonSent = new ShieldFrame(SHIELD_ID, RESPONSE.RESPONSE_JSON_ARRAY); frameJsonSent.addIntegerArgument(2, requestID); frameJsonSent.addIntegerArgument(4, result); frameJsonSent.addArgument(frame.getArgument(1)); for (int arg = 2; arg < frame.getArguments().size(); arg++) { frameJsonSent.addArgument(frame.getArgument(arg)); } sendShieldFrame(frameJsonSent, true); } } catch (JSONException e) { ShieldFrame frameJson = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frameJson.addIntegerArgument(2, requestID); frameJson.addIntegerArgument(1, RESPONSE.JSON_KEYCHAIN_IS_WRONG); sendShieldFrame(frameJson, true); } catch (ClassCastException e) { ShieldFrame frameJson = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frameJson.addIntegerArgument(2, requestID); frameJson.addIntegerArgument(1, RESPONSE.JSON_KEYCHAIN_IS_WRONG); sendShieldFrame(frameJson, true); } } else { } } else { // no response ShieldFrame frameJson = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frameJson.addIntegerArgument(2, requestID); frameJson.addIntegerArgument(1, RESPONSE.REQUEST_HAS_NO_RESPONSE); sendShieldFrame(frameJson, true); } } else // no request { ShieldFrame frameJson = new ShieldFrame(SHIELD_ID, RESPONSE.ON_ERROR); frameJson.addIntegerArgument(2, requestID); frameJson.addIntegerArgument(1, RESPONSE.RESPONSE_CAN_NOT_BE_FOUND); sendShieldFrame(frameJson, true); } break; case REQUEST.ADD_HTTP_ENTITY: requestID = frame.getArgumentAsInteger(0); if (InternetManager.getInstance().getRequest(requestID) != null) InternetManager.getInstance() .getRequest(requestID) .setEntity(frame.getArgumentAsString(1)); break; case REQUEST.SET_ENCODING: requestID = frame.getArgumentAsInteger(0); if (InternetManager.getInstance().getRequest(requestID) != null) InternetManager.getInstance() .getRequest(requestID) .setEncoding(frame.getArgumentAsString(1)); break; default: break; } if (InternetManager.getInstance().getUiCallback() != null) InternetManager.getInstance().getUiCallback().onStart(); } }