public void handle(HttpRequest request, HttpResponse response, HttpContext arg2) throws HttpException, IOException { EntityTemplate body = new EntityTemplate( new ContentProducer() { public void writeTo(final OutputStream outstream) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); writer.write("var sounds = ["); for (int i = 0; i < raws.length - 1; i++) { writer.write("'" + raws[i].getName() + "',"); } writer.write("'" + raws[raws.length - 1].getName() + "'];"); writer.write("var screenState = " + (screenState ? "1" : "0") + ";"); writer.flush(); } }); response.setStatusCode(HttpStatus.SC_OK); body.setContentType("application/json; charset=UTF-8"); response.setEntity(body); // Bring SpydrdoiActivity to the foreground if (!screenState) { Intent i = new Intent(context, ServerActivity.class); i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(i); } }
public void handle(HttpRequest request, HttpResponse response, HttpContext arg2) throws HttpException, IOException { final String uri = URLDecoder.decode(request.getRequestLine().getUri()); final List<NameValuePair> params = URLEncodedUtils.parse(URI.create(uri), "UTF-8"); final String[] content = {"Error"}; int soundID; response.setStatusCode(HttpStatus.SC_NOT_FOUND); if (params.size() > 0) { try { for (Iterator<NameValuePair> it = params.iterator(); it.hasNext(); ) { NameValuePair param = it.next(); // Load sound with appropriate name if (param.getName().equals("name")) { for (int i = 0; i < raws.length; i++) { if (raws[i].getName().equals(param.getValue())) { soundID = soundPool.load(context, raws[i].getInt(null), 0); response.setStatusCode(HttpStatus.SC_OK); content[0] = "OK"; } } } } } catch (Exception e) { Log.e(TAG, "Error !"); e.printStackTrace(); } } EntityTemplate body = new EntityTemplate( new ContentProducer() { public void writeTo(final OutputStream outstream) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); writer.write(content[0]); writer.flush(); } }); body.setContentType("text/plain; charset=UTF-8"); response.setEntity(body); }
public synchronized void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { Socket socket = ((ModifiedHttpContext) context).getSocket(); // Parse URI and configure the Session accordingly final String uri = URLDecoder.decode(request.getRequestLine().getUri()); final String sessionDescriptor = "v=0\r\n" + "o=- 15143872582342435176 15143872582342435176 IN IP4 " + socket.getLocalAddress().getHostName() + "\r\n" + "s=Unnamed\r\n" + "i=N/A\r\n" + "c=IN IP4 " + socket.getLocalAddress().getHostAddress() + "\r\n" + "t=0 0\r\n" + "a=tool:spydroid\r\n" + "a=recvonly\r\n" + "a=type:broadcast\r\n" + "a=charset:UTF-8\r\n"; response.setStatusCode(HttpStatus.SC_OK); EntityTemplate body = new EntityTemplate( new ContentProducer() { public void writeTo(final OutputStream outstream) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); writer.write(sessionDescriptor); writer.flush(); } }); body.setContentType("text/plain; charset=UTF-8"); response.setEntity(body); }
public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException { SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); final String uri = URLDecoder.decode(request.getRequestLine().getUri()); final List<NameValuePair> params = URLEncodedUtils.parse(URI.create(uri), "UTF-8"); String result = "Error"; if (params.size() > 0) { try { // Set the configuration if (params.get(0).getName().equals("set")) { Editor editor = settings.edit(); editor.putBoolean("stream_audio", false); editor.putBoolean("stream_video", false); for (Iterator<NameValuePair> it = params.iterator(); it.hasNext(); ) { NameValuePair param = it.next(); if (param.getName().equals("h263") || param.getName().equals("h264")) { editor.putBoolean("stream_video", true); Session.defaultVideoQuality = VideoQuality.parseQuality(param.getValue()); editor.putInt("video_resX", Session.defaultVideoQuality.resX); editor.putInt("video_resY", Session.defaultVideoQuality.resY); editor.putString( "video_framerate", String.valueOf(Session.defaultVideoQuality.frameRate)); editor.putString( "video_bitrate", String.valueOf(Session.defaultVideoQuality.bitRate / 1000)); editor.putString("video_encoder", param.getName().equals("h263") ? "2" : "1"); } if (param.getName().equals("amr") || param.getName().equals("aac")) { editor.putBoolean("stream_audio", true); Session.defaultVideoQuality = VideoQuality.parseQuality(param.getValue()); editor.putString("audio_encoder", param.getName().equals("amr") ? "3" : "5"); } } editor.commit(); result = "[]"; } // Send the current streaming configuration to the client else if (params.get(0).getName().equals("get")) { result = "{\"" + HX_DETECt_TAG + "\": true," + "\"streamAudio\":" + settings.getBoolean("stream_audio", false) + "," + "\"audioEncoder\":\"" + (Integer.parseInt(settings.getString("audio_encoder", "3")) == 3 ? "AMR-NB" : "AAC") + "\"," + "\"streamVideo\":" + settings.getBoolean("stream_video", true) + "," + "\"videoEncoder\":\"" + (Integer.parseInt(settings.getString("video_encoder", "2")) == 2 ? "H.263" : "H.264") + "\"," + "\"videoResolution\":\"" + settings.getInt("video_resX", Session.defaultVideoQuality.resX) + "x" + settings.getInt("video_resY", Session.defaultVideoQuality.resY) + "\"," + "\"videoFramerate\":\"" + settings.getString( "video_framerate", String.valueOf(Session.defaultVideoQuality.frameRate)) + " fps\"," + "\"videoBitrate\":\"" + settings.getString( "video_bitrate", String.valueOf(Session.defaultVideoQuality.bitRate / 1000)) + " kbps\"}"; } } catch (Exception e) { Log.e(TAG, "Error !"); e.printStackTrace(); } } final String finalResult = result; EntityTemplate body = new EntityTemplate( new ContentProducer() { public void writeTo(final OutputStream outstream) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); writer.write(finalResult); writer.flush(); } }); response.setStatusCode(HttpStatus.SC_OK); body.setContentType("application/json; charset=UTF-8"); response.setEntity(body); }
public void postJsonToPipeline(String endpoint, List docs, int requestId) throws Exception { FusionSession fusionSession = null; long currTime = System.nanoTime(); synchronized (this) { fusionSession = sessions.get(endpoint); // ensure last request within the session timeout period, else reset the session if (fusionSession == null || (currTime - fusionSession.sessionEstablishedAt) > maxNanosOfInactivity) { log.info( "Fusion session is likely expired (or soon will be) for endpoint " + endpoint + ", " + "pre-emptively re-setting this session before processing request " + requestId); fusionSession = resetSession(endpoint); if (fusionSession == null) throw new IllegalStateException( "Failed to re-connect to " + endpoint + " after session loss when processing request " + requestId); } } HttpEntity entity = null; try { HttpPost postRequest = new HttpPost(endpoint); // stream the json directly to the HTTP output EntityTemplate et = new EntityTemplate(new JacksonContentProducer(jsonObjectMapper, docs)); et.setContentType("application/json; charset=utf-8"); et.setContentEncoding("gzip"); // StandardCharsets.UTF_8.name()); postRequest.setEntity(et); // new BufferedHttpEntity(et)); HttpClientContext context = HttpClientContext.create(); context.setCookieStore(cookieStore); HttpResponse response = httpClient.execute(postRequest, context); entity = response.getEntity(); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 401) { // unauth'd - session probably expired? retry to establish log.warn( "Unauthorized error (401) when trying to send request " + requestId + " to Fusion at " + endpoint + ", will re-try to establish session"); // re-establish the session and re-try the request try { EntityUtils.consume(entity); } catch (Exception ignore) { log.warn("Failed to consume entity due to: " + ignore); } finally { entity = null; } synchronized (this) { fusionSession = resetSession(endpoint); if (fusionSession == null) throw new IllegalStateException( "After re-establishing session when processing request " + requestId + ", endpoint " + endpoint + " is no longer active! Try another endpoint."); } log.info( "Going to re-try request " + requestId + " after session re-established with " + endpoint); response = httpClient.execute(postRequest, context); entity = response.getEntity(); statusCode = response.getStatusLine().getStatusCode(); if (statusCode == 200 || statusCode == 204) { log.info( "Re-try request " + requestId + " after session timeout succeeded for: " + endpoint); } else { raiseFusionServerException(endpoint, entity, statusCode, response, requestId); } } else if (statusCode != 200 && statusCode != 204) { raiseFusionServerException(endpoint, entity, statusCode, response, requestId); } else { // OK! } } finally { if (entity != null) { try { EntityUtils.consume(entity); } catch (Exception ignore) { log.warn("Failed to consume entity due to: " + ignore); } finally { entity = null; } } } }
public void handle(HttpRequest request, HttpResponse response, HttpContext arg2) throws HttpException, IOException { final String uri = URLDecoder.decode(request.getRequestLine().getUri()); final List<NameValuePair> params = URLEncodedUtils.parse(URI.create(uri), "UTF-8"); final String[] content = {"Error"}; int soundID; response.setStatusCode(HttpStatus.SC_NOT_FOUND); if (params.size() > 0) { try { for (Iterator<NameValuePair> it = params.iterator(); it.hasNext(); ) { NameValuePair param = it.next(); // Load sound with appropriate name if (param.getName().equals("name")) { for (int i = 0; i < raws.length; i++) { if (raws[i].getName().equals(param.getValue())) { soundID = soundPool.load(context, raws[i].getInt(null), 0); response.setStatusCode(HttpStatus.SC_OK); content[0] = "OK"; } } if (param.getValue().equals("forward")) { handler.obtainMessage(MOVE_FORWARD).sendToTarget(); // soundID = soundPool.load(context, raws[0].getInt(null), 0); response.setStatusCode(HttpStatus.SC_OK); content[0] = "OK"; } if (param.getValue().equals("backward")) { handler.obtainMessage(MOVE_BACKWARD).sendToTarget(); // soundID = soundPool.load(context, raws[0].getInt(null), 0); response.setStatusCode(HttpStatus.SC_OK); content[0] = "OK"; } if (param.getValue().equals("left")) { handler.obtainMessage(MOVE_LEFT).sendToTarget(); // soundID = soundPool.load(context, raws[0].getInt(null), 0); response.setStatusCode(HttpStatus.SC_OK); content[0] = "OK"; } if (param.getValue().equals("right")) { handler.obtainMessage(MOVE_RIGHT).sendToTarget(); // soundID = soundPool.load(context, raws[0].getInt(null), 0); response.setStatusCode(HttpStatus.SC_OK); content[0] = "OK"; } if (param.getValue().equals("stop")) { handler.obtainMessage(STOP).sendToTarget(); // soundID = soundPool.load(context, raws[0].getInt(null), 0); response.setStatusCode(HttpStatus.SC_OK); content[0] = "OK"; } if (param.getValue().equals("obstacleOn")) { handler.obtainMessage(ENABLE_OBSTACLE_AVOIDANCE).sendToTarget(); // soundID = soundPool.load(context, raws[0].getInt(null), 0); response.setStatusCode(HttpStatus.SC_OK); content[0] = "OK"; } if (param.getValue().equals("obstacleOff")) { handler.obtainMessage(DISABLE_OBSTACLE_AVOIDANCE).sendToTarget(); // soundID = soundPool.load(context, raws[1].getInt(null), 0); response.setStatusCode(HttpStatus.SC_OK); content[0] = "OK"; } if (param.getValue().equals("cliffOn")) { handler.obtainMessage(ENABLE_CLIFF_AVOIDANCE).sendToTarget(); // soundID = soundPool.load(context, raws[2].getInt(null), 0); response.setStatusCode(HttpStatus.SC_OK); content[0] = "OK"; } if (param.getValue().equals("cliffOff")) { handler.obtainMessage(DISABLE_CLIFF_AVOIDANCE).sendToTarget(); // soundID = soundPool.load(context, raws[3].getInt(null), 0); response.setStatusCode(HttpStatus.SC_OK); content[0] = "OK"; } } } } catch (Exception e) { Log.e(TAG, "Error !"); e.printStackTrace(); } } EntityTemplate body = new EntityTemplate( new ContentProducer() { public void writeTo(final OutputStream outstream) throws IOException { OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); writer.write(content[0]); writer.flush(); } }); body.setContentType("text/plain; charset=UTF-8"); response.setEntity(body); }