com.virjar.sekiro.api.SekiroResponse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sekiro-api Show documentation
Show all versions of sekiro-api Show documentation
ratel api,used for developer on ratel system,an extension for xposed framewrok,ratel api compatable with original xposed framework
package com.virjar.sekiro.api;
import com.virjar.sekiro.netty.protocol.SekiroNatMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Hashtable;
import external.com.alibaba.fastjson.JSON;
import io.netty.channel.Channel;
public class SekiroResponse {
private SekiroRequest request;
private Channel channel;
private boolean closed = false;
private static final Logger logger = LoggerFactory.getLogger(SekiroResponse.class);
public SekiroResponse(SekiroRequest request, Channel channel) {
this.request = request;
this.channel = channel;
}
/**
* 返回数据到服务器
*
* @param contentType MIME type,为了支持file,图片等特殊场景才支持
* @param bytes 返回内容
*/
public void send(String contentType, byte[] bytes) {
if (closed) {
logger.warn("response send already!");
return;
}
SekiroNatMessage sekiroNatMessage = new SekiroNatMessage();
sekiroNatMessage.setSerialNumber(request.getSerialNo());
sekiroNatMessage.setType(SekiroNatMessage.TYPE_INVOKE);
sekiroNatMessage.setData(bytes);
sekiroNatMessage.setExtra(contentType);
channel.writeAndFlush(sekiroNatMessage);
closed = true;
}
public void send(CommonRes commonRes) {
String stringContent = JSON.toJSONString(commonRes);
send("application/json; charset=utf-8", stringContent);
}
public void send(String contentType, String string) {
send(contentType, string.getBytes(StandardCharsets.UTF_8));
}
public void send(String string) {
send("text/html; charset=utf-8", string);
}
public void sendFile(File file) throws IOException {
FileInputStream fin = new FileInputStream(file);
sendStream(getContentType(file.getAbsolutePath()), new BufferedInputStream(fin, 64000));
}
private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
private static final int EOF = -1;
public void sendStream(String contentType, InputStream inputStream) throws IOException {
try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) {
// copy(inputStream, output);
long count = 0;
int n;
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
while (EOF != (n = inputStream.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
send(contentType, output.toByteArray());
}
}
private static Hashtable mContentTypes = new Hashtable();
static {
mContentTypes.put("js", "application/javascript");
mContentTypes.put("json", "application/json");
mContentTypes.put("png", "image/png");
mContentTypes.put("jpg", "image/jpeg");
mContentTypes.put("html", "text/html");
mContentTypes.put("css", "text/css");
mContentTypes.put("mp4", "video/mp4");
mContentTypes.put("mov", "video/quicktime");
mContentTypes.put("wmv", "video/x-ms-wmv");
}
public static String getContentType(String path) {
String type = tryGetContentType(path);
if (type != null)
return type;
return "text/plain";
}
public static String tryGetContentType(String path) {
int index = path.lastIndexOf(".");
if (index != -1) {
String e = path.substring(index + 1);
String ct = mContentTypes.get(e);
if (ct != null)
return ct;
}
return null;
}
public static String getStackTrack(Throwable throwable) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(byteArrayOutputStream));
throwable.printStackTrace(printWriter);
printWriter.close();
try {
return byteArrayOutputStream.toString(StandardCharsets.UTF_8.name());
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public void failed(int errorCode, Throwable throwable) {
failed(errorCode, getStackTrack(throwable));
}
public void failed(int errorCode, String message) {
send(CommonRes.failed(errorCode, message));
}
public void failed(String message) {
send(CommonRes.failed(message));
}
public void success(T data) {
send(CommonRes.success(data));
}
}