AssetHandler

来源:互联网 发布:韩磊花房姑娘知乎 编辑:程序博客网 时间:2024/06/04 08:24
package com.streambus.setting.remoteserver;


import java.io.FileNotFoundException;


import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Locale;


import org.apache.http.Header;
import org.apache.http.HttpEntity;
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.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.protocol.HttpContext;
import org.apache.http.protocol.HttpRequestHandler;




import com.streambus.setting.HXSettingApp;
import com.streambus.setting.R;
import com.streambus.setting.util.Logger;




public class AssetHandler implements HttpRequestHandler {



@Override
public void handle(HttpRequest request, HttpResponse response,
HttpContext context) throws HttpException, IOException {

// 通过http访问才能调用此方法。
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");
}
// 处理响应
response(request, response);
}


private void response(HttpRequest request, HttpResponse response) throws UnsupportedEncodingException {
String target = request.getRequestLine().getUri();
try {
String url = sanitizeUri(target);
if (url.equals("/") || url.equals("/index.html")) {
url = "/index.html";
}
if(url.contains("index.html")){
Logger.d("test", "  builder index.html  ");
EntityTemplate body = new EntityTemplate(new ContentProducer() {
public void writeTo(final OutputStream outstream) throws IOException {
OutputStreamWriter writer = new OutputStreamWriter(outstream, "UTF-8");
buildIndexHtml(writer,HXSettingApp.getContext().getString(R.string.remote_bt_txt));
}
});

body.setContentType("text/html; charset=UTF-8");
response.setEntity(body);
}else{
if(url.contains("?"))
url = url.substring(0,url.indexOf("?"));
url = url.substring(1);
Logger.d("test", "  url  "+url);
InputStream in = HXSettingApp.getContext().getAssets().open(url);
response.setStatusCode(HttpStatus.SC_OK);
InputStreamEntity body = new InputStreamEntity(in, -1);
response.setEntity(body);
}

} catch (FileNotFoundException e) {
response.setStatusCode(HttpStatus.SC_NOT_FOUND);
final String path = URLDecoder.decode(target, "UTF-8");
EntityTemplate 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(URLDecoder.decode(path, "UTF-8"));
writer.write(" not found");
writer.write("</h1></body></html>");
writer.flush();
}


});
body.setContentType("text/html; charset=UTF-8");
response.setEntity(body);
}catch (IOException e) {
response.setStatusCode(HttpStatus.SC_FORBIDDEN);
EntityTemplate 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("Access denied");
writer.write("</h1></body></html>");
writer.flush();
}


});
body.setContentType("text/html; charset=UTF-8");
response.setEntity(body);
}
}

private void buildIndexHtml(OutputStreamWriter writer,String s) throws IOException{
writer.write("<!DOCTYPE HTML>");
writer.write("<html>");
writer.write("<head>");
writer.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
writer.write("<title>Remote Installation</title>");

writer.write("<script src=\"./js/jquery.min.js\" type=\"text/javascript\"></script>");
writer.write("<script src=\"./js/jquery.uploadify.js\" type=\"text/javascript\"></script>");
writer.write("<script src=\"./js/common.js\" type=\"text/javascript\"></script>");
writer.write("<script src=\"./js/index.js\" type=\"text/javascript\"></script>");
writer.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"./css/uploadify.css\">");
writer.write("</head>");

writer.write("<body>");
writer.write("<div style=\"width: 100%;height: auto;min-width: 1024px;margin-top: 10%;\">");
writer.write("<div id=\"filedrag\" ></div>");
writer.write("<div class=\"btTxt\" id=\"btTxt\">"+s+"</div>");
writer.write("<form id=\"upload\"  enctype=\"multipart/form-data\" method=\"post\" >");
writer.write("<div id=\"queue\"></div>");
writer.write("<input multiple type=\"file\" id=\"fileselect\"  />");
writer.write("</form>");
writer.write("<div id=\"bg2\">");
writer.write("<div id=\"okImg\" onclick=\"okAction()\" style=\"width: 42px;height: 42px;position: absolute;margin-left:125px;margin-top: -15px;display:none;cursor: pointer;\">");
writer.write("<img id=\"dImg\" src=\"./images/yuanchengcaokPC-dui.png\" style=\"width: 42px;height: 42px;\" />");
writer.write("</div>");
writer.write("<div id=\"progressBg\" style=\"width: 100%;height: 10px;\">");
writer.write("</div>");
writer.write("</div>");
writer.write("</div>");
writer.write("</body>");
writer.write("</html>");

writer.flush();
}

/**
* 解码

* @param uri
* @return
*/
private String sanitizeUri(String uri) {
try {
uri = URLDecoder.decode(uri, "UTF-8");
} catch (UnsupportedEncodingException e) {
try {
uri = URLDecoder.decode(uri, "ISO-8859-1");
} catch (UnsupportedEncodingException e1) {
throw new Error();
}
}
return uri;
}





private SimpleDateFormat sdf = new SimpleDateFormat("yy-MM-dd ahh:mm");








/** 获得文件大小表示 */
private String formatFileSize(long len) {
if (len < 1024)
return len + " B";
else if (len < 1024 * 1024)
return len / 1024 + "." + (len % 1024 / 10 % 100) + " KB";
else if (len < 1024 * 1024 * 1024)
return len / (1024 * 1024) + "." + len % (1024 * 1024) / 10 % 100
+ " MB";
else
return len / (1024 * 1024 * 1024) + "." + len
% (1024 * 1024 * 1024) / 10 % 100 + " MB";
}


}
0 0
原创粉丝点击