Android Open Source - spydroid-ipcamera Mod Asset Server






From Project

Back to project page spydroid-ipcamera.

License

The source code is released under:

GNU General Public License

If you think the Android project spydroid-ipcamera listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2011-2012 GUIGUI Simon, fyhertz@gmail.com
 * /*from  w  w  w. j  ava 2 s . c  om*/
 * This file is part of Spydroid (http://code.google.com/p/spydroid-ipcamera/)
 * 
 * Spydroid is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This source code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this source code; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


package net.majorkernelpanic.http;

import static net.majorkernelpanic.http.TinyHttpServer.TAG;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URLDecoder;
import java.util.Date;
import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.MethodNotSupportedException;
import org.apache.http.entity.AbstractHttpEntity;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.impl.cookie.DateParseException;
import org.apache.http.impl.cookie.DateUtils;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;
import org.apache.http.util.EntityUtils;

import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.util.Log;

/**
 * 
 * Serves the content of assets/www 
 *
 */
public class ModAssetServer implements HttpRequestHandler {

  public static final String PATTERN = "*";
  
  /** The list of MIME Media Types supported by the server. */
  public static String[] mimeMediaTypes = new String[] {
    "htm",  "text/html", 
    "html",  "text/html", 
    "gif",  "image/gif",
    "jpg",  "image/jpeg",
    "png",  "image/png", 
    "js",  "text/javascript",
    "json",  "text/json",
    "css",  "text/css"
  };

  private final TinyHttpServer mServer;
  private final AssetManager mAssetManager;

  public ModAssetServer(TinyHttpServer server) {
    super();
    mServer = server;
    mAssetManager = mServer.mContext.getAssets();
  }

  public void handle(
      final HttpRequest request, 
      final HttpResponse response,
      final HttpContext context) throws HttpException, IOException {
    AbstractHttpEntity body = null;

    final String method = request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
    if (!method.equals("GET") && !method.equals("HEAD") && !method.equals("POST")) {
      throw new MethodNotSupportedException(method + " method not supported"); 
    }

    final String url = URLDecoder.decode(request.getRequestLine().getUri());
    if (request instanceof HttpEntityEnclosingRequest) {
      HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
      byte[] entityContent = EntityUtils.toByteArray(entity);
      Log.d(TAG,"Incoming entity content (bytes): " + entityContent.length);
    }

    final String location = "www"+(url.equals("/")?"/index.htm":url);
    response.setStatusCode(HttpStatus.SC_OK);

    try {
      Log.i(TAG,"Requested: \""+url+"\"");

      // Compares the Last-Modified date header (if present) with the If-Modified-Since date
      if (request.containsHeader("If-Modified-Since")) {
        try {
          Date date = DateUtils.parseDate(request.getHeaders("If-Modified-Since")[0].getValue());
          if (date.compareTo(mServer.mLastModified)<=0) {
            // The file has not been modified
            response.setStatusCode(HttpStatus.SC_NOT_MODIFIED);
            return;
          }
        } catch (DateParseException e) {
          e.printStackTrace();
        }
      }

      // We determine if the asset is compressed
      try {
        AssetFileDescriptor afd = mAssetManager.openFd(location);

        // The asset is not compressed
        FileInputStream fis = new FileInputStream(afd.getFileDescriptor());
        fis.skip(afd.getStartOffset());
        body = new InputStreamEntity(fis, afd.getDeclaredLength());

        Log.d(TAG,"Serving uncompressed file " + "www" + url);

      } catch (FileNotFoundException e) {

        // The asset may be compressed
        // AAPT compresses assets so first we need to uncompress them to determine their length
        InputStream stream =  mAssetManager.open(location,AssetManager.ACCESS_STREAMING);
        ByteArrayOutputStream buffer = new ByteArrayOutputStream(64000);
        byte[] tmp = new byte[4096]; int length = 0;
        while ((length = stream.read(tmp)) != -1) buffer.write(tmp, 0, length);
        body = new InputStreamEntity(new ByteArrayInputStream(buffer.toByteArray()), buffer.size());
        stream.close();

        Log.d(TAG,"Serving compressed file " + "www" + url);

      }

      body.setContentType(getMimeMediaType(url)+"; charset=UTF-8");
      response.addHeader("Last-Modified", DateUtils.formatDate(mServer.mLastModified));

    } catch (IOException e) {
      // File does not exist
      response.setStatusCode(HttpStatus.SC_NOT_FOUND);
      body = new EntityTemplate(new ContentProducer() {
        public void writeTo(final OutputStream outstream) throws IOException {
          OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8"); 
          writer.write("<html><body><h1>");
          writer.write("File ");
          writer.write("www"+url);
          writer.write(" not found");
          writer.write("</h1></body></html>");
          writer.flush();
        }
      });
      Log.d(TAG,"File " + "www" + url + " not found");
      body.setContentType("text/html; charset=UTF-8");
    }

    response.setEntity(body);

  }

  private String getMimeMediaType(String fileName) {
    String extension = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
    for (int i=0;i<mimeMediaTypes.length;i+=2) {
      if (mimeMediaTypes[i].equals(extension)) 
        return mimeMediaTypes[i+1];
    }
    return mimeMediaTypes[0];
  }

}




Java Source Code List

net.majorkernelpanic.http.ModAssetServer.java
net.majorkernelpanic.http.ModInternationalization.java
net.majorkernelpanic.http.ModSSL.java
net.majorkernelpanic.http.TinyHttpServer.java
net.majorkernelpanic.spydroid.SpydroidApplication.java
net.majorkernelpanic.spydroid.Utilities.java
net.majorkernelpanic.spydroid.api.CustomHttpServer.java
net.majorkernelpanic.spydroid.api.CustomRtspServer.java
net.majorkernelpanic.spydroid.api.RequestHandler.java
net.majorkernelpanic.spydroid.ui.AboutFragment.java
net.majorkernelpanic.spydroid.ui.HandsetFragment.java
net.majorkernelpanic.spydroid.ui.OptionsActivity.java
net.majorkernelpanic.spydroid.ui.PreviewFragment.java
net.majorkernelpanic.spydroid.ui.SpydroidActivity.java
net.majorkernelpanic.spydroid.ui.TabletFragment.java
net.majorkernelpanic.streaming.MediaStream.java
net.majorkernelpanic.streaming.SessionBuilder.java
net.majorkernelpanic.streaming.Session.java
net.majorkernelpanic.streaming.Stream.java
net.majorkernelpanic.streaming.audio.AACStream.java
net.majorkernelpanic.streaming.audio.AMRNBStream.java
net.majorkernelpanic.streaming.audio.AudioQuality.java
net.majorkernelpanic.streaming.audio.AudioStream.java
net.majorkernelpanic.streaming.exceptions.CameraInUseException.java
net.majorkernelpanic.streaming.exceptions.ConfNotSupportedException.java
net.majorkernelpanic.streaming.exceptions.InvalidSurfaceException.java
net.majorkernelpanic.streaming.exceptions.StorageUnavailableException.java
net.majorkernelpanic.streaming.gl.SurfaceManager.java
net.majorkernelpanic.streaming.gl.SurfaceView.java
net.majorkernelpanic.streaming.gl.TextureManager.java
net.majorkernelpanic.streaming.hw.CodecManager.java
net.majorkernelpanic.streaming.hw.EncoderDebugger.java
net.majorkernelpanic.streaming.hw.NV21Convertor.java
net.majorkernelpanic.streaming.mp4.MP4Config.java
net.majorkernelpanic.streaming.mp4.MP4Parser.java
net.majorkernelpanic.streaming.rtcp.SenderReport.java
net.majorkernelpanic.streaming.rtp.AACADTSPacketizer.java
net.majorkernelpanic.streaming.rtp.AACLATMPacketizer.java
net.majorkernelpanic.streaming.rtp.AMRNBPacketizer.java
net.majorkernelpanic.streaming.rtp.AbstractPacketizer.java
net.majorkernelpanic.streaming.rtp.H263Packetizer.java
net.majorkernelpanic.streaming.rtp.H264Packetizer.java
net.majorkernelpanic.streaming.rtp.MediaCodecInputStream.java
net.majorkernelpanic.streaming.rtp.RtpSocket.java
net.majorkernelpanic.streaming.rtsp.RtspClient.java
net.majorkernelpanic.streaming.rtsp.RtspServer.java
net.majorkernelpanic.streaming.rtsp.UriParser.java
net.majorkernelpanic.streaming.video.CodecManager.java
net.majorkernelpanic.streaming.video.H263Stream.java
net.majorkernelpanic.streaming.video.H264Stream.java
net.majorkernelpanic.streaming.video.VideoQuality.java
net.majorkernelpanic.streaming.video.VideoStream.java