@Override public void onNewShieldFrameReceived(ShieldFrame frame) { if (frame.getShieldId() == UIShield.TERMINAL_SHIELD.getId()) { String outputTxt = null; if (frame.getArguments() != null && frame.getArguments().size() > 0) outputTxt = frame.getArgumentAsString(0); if (outputTxt != null) { String date = terminalPrintedLines.size() == 0 || terminalPrintedLines.get(terminalPrintedLines.size() - 1).isEndedWithNewLine ? TerminalFragment.getTimeAsString() + " [RX] : " : ""; boolean isEndedWithNewLine = outputTxt.length() > 0 && outputTxt.charAt(outputTxt.length() - 1) == '\n'; if (lastItemEndedWithNewLine) { terminalPrintedLines.add( new TerminalPrintedLine( date, outputTxt.substring( 0, isEndedWithNewLine ? outputTxt.length() - 1 : outputTxt.length()), isEndedWithNewLine, true)); tempLines.add( new TerminalPrintedLine( date, getEncodedString( outputTxt.substring( 0, isEndedWithNewLine ? outputTxt.length() - 1 : outputTxt.length())), isEndedWithNewLine, true)); } else if (terminalPrintedLines.size() > 0 && tempLines.size() > 0) { terminalPrintedLines.get(terminalPrintedLines.size() - 1).print = terminalPrintedLines.get(terminalPrintedLines.size() - 1).print + outputTxt.substring( 0, isEndedWithNewLine ? outputTxt.length() - 1 : outputTxt.length()); tempLines.get(tempLines.size() - 1).print = getEncodedString(terminalPrintedLines.get(terminalPrintedLines.size() - 1).print); if (isEndedWithNewLine) terminalPrintedLines.get(terminalPrintedLines.size() - 1).isEndedWithNewLine = true; } lastItemEndedWithNewLine = isEndedWithNewLine; greaterThanThousand = terminalPrintedLines.size() > 1000; if (greaterThanThousand) { // for (int i = 0; i < 1; i++) { terminalPrintedLines.remove(0); tempLines.remove(0); // } } switch (frame.getFunctionId()) { case WRITE: if (eventHandler != null) { eventHandler.onPrint(outputTxt, greaterThanThousand); } break; case PRINT: if (eventHandler != null) { eventHandler.onPrint(outputTxt, greaterThanThousand); } break; default: break; } } // Log.d("internetLog", "Terminal " + outputTxt); } }
@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(); } }
@Override public void onNewShieldFrameReceived(ShieldFrame frame) { if (frame.getShieldId() == UIShield.DATA_LOGGER.getId()) { switch (frame.getFunctionId()) { case START_LOGGING: if (frame.getArguments().size() > 0) fileName = frame.getArgumentAsString(0); else fileName = null; headerList = new CopyOnWriteArrayList<String>(); dataSet = new ArrayList<Map<String, String>>(); rowData = new HashMap<String, String>(); currentStatus = LOGGING; isStarted = true; if (eventHandler != null) { eventHandler.onStartLogging(); } break; case STOP_LOGGING: saveData(); break; case ADD_STRING: if (isStarted) { currentStatus = LOGGING; String key = frame.getArgumentAsString(0); String value = frame.getArgumentAsString(1); if (!headerList.contains(key)) headerList.add(key); rowData.put(key, value); if (eventHandler != null) { eventHandler.onAdd(key, value); } } break; case ADD_FLOAT: if (isStarted) { currentStatus = LOGGING; String keyFloat = frame.getArgumentAsString(0); String valueFloat = frame.getArgumentAsFloat(1) + ""; if (!headerList.contains(keyFloat)) headerList.add(keyFloat); rowData.put(keyFloat, valueFloat); if (eventHandler != null) { eventHandler.onAdd(keyFloat, valueFloat); } } break; case LOG: if (isStarted) { currentStatus = LOGGING; if (eventHandler != null) { eventHandler.onLog(new HashMap<>(rowData)); } if (!headerList.contains("Time")) headerList.add("Time"); rowData.put( "Time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US) .format(new Date()) .toString()); // rowData.remove("Time"); dataSet.add(new HashMap<>(rowData)); rowData = new HashMap<>(); } break; default: break; } } }