package com.qiniu.ufop.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
/**
* @Description qiniu开发自定义数据处理程序
* @project ufop
* @author:hf
* @date:
*/
@Slf4j
@RestController
public class UfopController {
/**
* @Description: 监听handler方法
* @Return:
* @Author: hf
* @Time: 2021/3/26 8:21
*/
@PostMapping(value="handler",produces = { "application/json; charset=utf-8" })
public void handler(HttpServletRequest request, HttpServletResponse response
,String cmd,String url){
//qdoractl register doraufoptest -m private -d 'it is a test!'
Process pro =null;
try {
String command = String.format("F:/qiniuufop/qdoractl register %S -m private -d %s", cmd, url);
log.info("qdoractl执行命令command:"+command);
pro = Runtime.getRuntime().exec(command);
UfopInterceptor error = new UfopInterceptor(pro.getErrorStream());
UfopInterceptor output = new UfopInterceptor(pro.getInputStream());
error.start();
output.start();
} catch (IOException e) {
e.printStackTrace();
log.info(e.getMessage());
} finally {
if (pro != null){
pro.destroy();
}
}
}
static class UfopInterceptor extends Thread {
private InputStream is;
UfopInterceptor(InputStream is){
this.is = is;
}
@Override
public void run(){
try (BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));) {
String line;
while ((line = br.readLine()) != null) {
log.info(line);
}
}
catch (IOException e) {
log.error("ufop线程异常:" + e.getMessage());
}
}
}
}