Browse Source

hos-broker清理文件名加密,统一使用文件id

airhead 8 years ago
parent
commit
c594066db0

+ 15 - 41
hos-broker/src/main/java/com/yihu/hos/broker/services/camel/ESBCamelService.java

@ -166,7 +166,7 @@ public class ESBCamelService {
                    String routeCode = serviceFlow.getRouteCode();
                    SystemCamelContext.getContext().stopRoute(routeCode);
                    SystemCamelContext.getContext().removeRoute(routeCode);
                    this.deleteClassFile(handleFile);  //清理文件?
                    this.deleteClassFile(handleFile);
                    this.deleteServiceFlow(handleFile.getRouteCode());//删除mongo中serviceFlow信息
                    //TODO 删除mongodb中文件
                }
@ -236,15 +236,13 @@ public class ESBCamelService {
        URL resource = SystemCamelContext.getResource(this);
        FileOutputStream outputStream = ClassFileUtil.createFile(resource, handleFile.getPackageName(), handleFile.getClassName(), ClassFileUtil.CLASS_FILE);
        String fileName = DES.decrypt(handleFile.getFilePath(), DES.COMMON_PASSWORD);
//        MongoDatabase db = mongoConfig.mongoClient().getDatabase(dbName);
        String fileName = handleFile.getClassName() + ClassFileUtil.CLASS_FILE;
//        boolean read = GridFSUtil.readFile(db, outputStream, fileName);
        //从中心下载
        String downUrl = fsUrl + "/tenant/down/" + handleFile.getFilePath();
        String sourcePath =  ClassFileUtil.downFile(downUrl, resource, handleFile.getPackageName(), handleFile.getClassName(), ClassFileUtil.CLASS_FILE);
        String downUrl = fsUrl + "/" + handleFile.getFilePath();
        String sourcePath = ClassFileUtil.downFile(downUrl, resource, handleFile.getPackageName(), handleFile.getClassName(), ClassFileUtil.CLASS_FILE);
        if (sourcePath==null) {
        if (sourcePath == null) {
            logger.error("not mongo file, fileName:" + fileName);
            return false;
        }
@ -258,18 +256,10 @@ public class ESBCamelService {
        }
        SystemCamelContext.putClassMapping(handleFile.getRouteCode(), handleFile.getPackageName(), handleFile.getUsage(), handleFile.getClassName());
        URL resource = SystemCamelContext.getResource(this);
//        FileOutputStream outputStream = ClassFileUtil.createFile(resource, handleFile.getPackageName(), handleFile.getClassName(), ClassFileUtil.JAVA_FILE);
//        MongoDatabase db = mongoConfig.mongoClient().getDatabase(dbName);
//
//        boolean read = GridFSUtil.readFileContent(db, outputStream, fileName);
//        if (!read) {
//            logger.error("not mongo file, fileName:" + fileName);
//            return false;
//        }
        //从中心下载java文件
        String downUrl = fsUrl + "/tenant/down/" + handleFile.getFilePath();
        String sourcePath =  ClassFileUtil.downFile(downUrl, resource, handleFile.getPackageName(), handleFile.getClassName(), ClassFileUtil.JAVA_FILE);
        String downUrl = fsUrl + "/" + handleFile.getFilePath();
        String sourcePath = ClassFileUtil.downFile(downUrl, resource, handleFile.getPackageName(), handleFile.getClassName(), ClassFileUtil.JAVA_FILE);
        if (sourcePath == null) {
            return false;
@ -280,40 +270,24 @@ public class ESBCamelService {
        if (succ) {
            //编译成功后将class文件上传至mongodb;文件名为类名+ routeCode
            String packagePath = StringUtil.replaceStrAll(handleFile.getPackageName(), ".", "/");
            String classPath =  resource.getPath()+ packagePath + "/" + handleFile.getClassName() + ClassFileUtil.CLASS_FILE;
//            GridFSUtil.uploadFile(classPath, handleFile.getClassName() + handleFile.getRouteCode() + ClassFileUtil.CLASS_FILE, null);
//            TODO 上传到本地mongodb和中心mongodb
            String fileName = DES.decrypt(handleFile.getFilePath(), DES.COMMON_PASSWORD).replaceAll(ClassFileUtil.JAVA_FILE,ClassFileUtil.CLASS_FILE);
            String enFileName = DES.encrypt(fileName, DES.COMMON_PASSWORD);
            String uploadUrl = fsUrl + "/tenant/upload/" + enFileName;
            ClassFileUtil.uploadFile(uploadUrl,new File(classPath),fileName);
            String classPath = resource.getPath() + packagePath + "/" + handleFile.getClassName() + ClassFileUtil.CLASS_FILE;
            //上传文件
            String fileName = handleFile.getClassName() + ClassFileUtil.CLASS_FILE;
            String uploadUrl = fsUrl + "/" + handleFile.getFilePath();
            ClassFileUtil.uploadFile(uploadUrl, new File(classPath), fileName);
        }
        return succ;
    }
    private void deleteClassFile(ServiceFlow.HandleFile handleFile) throws Exception {
//        String className = SystemCamelContext.getClassMapping(handleFile.getRouteCode(), handleFile.getClassName(), handleFile.getUsage());
//        if (StringUtil.isEmpty(className)) {
//            return;
//        }
        String packagePath = StringUtil.replaceStrAll(handleFile.getPackageName(), ".", "/");
        String classPath = ClassLoader.getSystemResource("").getPath() + "/" + packagePath + "/" + handleFile.getClassName() + ".class";
        String javaPath = ClassLoader.getSystemResource("").getPath() + "/" + packagePath + "/" + handleFile.getClassName() + ".java";
        ClassFileUtil.deleteClassfile(classPath);
        ClassFileUtil.deleteClassfile(javaPath);
        //TODO 从中心删除
        //删除class文件
        String delJavaUrl = fsUrl + "/tenant/delFile/" + handleFile.getFilePath();
        String delJavaUrl = fsUrl + "/" + handleFile.getFilePath();
        ClassFileUtil.deleteFile(delJavaUrl);
        //删除java文件
        String classFilePath = DES.decrypt( handleFile.getFilePath(),DES.COMMON_PASSWORD);
        String javaFilePath = classFilePath.replace(ClassFileUtil.CLASS_FILE,ClassFileUtil.JAVA_FILE);
        String enJavaPath = DES.encrypt(javaFilePath,DES.COMMON_PASSWORD);
        String delClassUrl = fsUrl + "/tenant/delFile/" + enJavaPath;
         ClassFileUtil.deleteFile(delClassUrl);
        // 完成
        logger.info("===================" + handleFile.getPackageName() + CoreConstant.DOT + handleFile.getClassName() + ".class 删除过程结束");
    }

+ 59 - 43
hos-core/src/main/java/com/yihu/hos/core/datatype/ClassFileUtil.java

@ -3,6 +3,8 @@ package com.yihu.hos.core.datatype;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yihu.hos.core.file.FileUtil;
import com.yihu.hos.core.http.HTTPResponse;
import com.yihu.hos.core.http.HttpClientKit;
import com.yihu.hos.core.log.Logger;
import com.yihu.hos.core.log.LoggerFactory;
import org.apache.http.HttpEntity;
@ -182,14 +184,15 @@ public class ClassFileUtil {
    /**
     * 下载文件,返回下载后的文件地址
     * @param downUrl        http请求地址
     * @param url            本地保存根目录
     * @param packageName   包名
     * @param className     类名
     * @param fileType      文件类型
     *
     * @param downUrl     http请求地址
     * @param url         本地保存根目录
     * @param packageName 包名
     * @param className   类名
     * @param fileType    文件类型
     * @return
     */
    public static String downFile(String downUrl,URL url, String packageName, String className, String fileType) {
    public static String downFile(String downUrl, URL url, String packageName, String className, String fileType) {
        try {
            String packagePath = StringUtil.replaceStrAll(packageName, ".", "/");
            File packageFile = new File(url.getPath() + "/" + packagePath);
@ -202,10 +205,10 @@ public class ClassFileUtil {
            String classPath = packageFile.getPath() + "/" + className + fileType;
            byte[] bytes = down(downUrl);//文件内容
            boolean succ = FileUtil.writeFile(classPath,bytes,"UTF-8");//写入文件
            if (succ){
            boolean succ = FileUtil.writeFile(classPath, bytes, "UTF-8");//写入文件
            if (succ) {
                return classPath;
            }else {
            } else {
                return null;
            }
        } catch (Exception e) {
@ -214,12 +217,12 @@ public class ClassFileUtil {
    }
    public static String uploadFile(String uploadUrl, File file,String fileName) {
    public static String uploadFile(String uploadUrl, File file, String fileName) {
        try {
            boolean succ = upload(uploadUrl, file,fileName);//文件内容
            if (succ){
            boolean succ = upload(uploadUrl, file, fileName);//文件内容
            if (succ) {
                return fileName;
            }else {
            } else {
                return null;
            }
        } catch (Exception e) {
@ -228,22 +231,23 @@ public class ClassFileUtil {
    }
    /**
     *  http接口下载文件
     * @param url  请求地址
     * http接口下载文件
     *
     * @param url 请求地址
     * @return
     */
    public static byte[] down( String url) {
    public static byte[] down(String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        try {
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(1000*60*5).setConnectTimeout(1000*60*5).build();//设置请求和传输超时时间
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(1000 * 60 * 5).setConnectTimeout(1000 * 60 * 5).build();//设置请求和传输超时时间
            httpget.setConfig(requestConfig);
            response = httpClient.execute(httpget);
            response.setHeader( "Content-Type", "application/octet-stream;charset=UTF-8");
            response.setHeader("Content-Type", "application/octet-stream;charset=UTF-8");
            entity = response.getEntity();
            byte[] bytes= FileUtil.getBytesByStream(entity.getContent());
            byte[] bytes = FileUtil.getBytesByStream(entity.getContent());
            return bytes;
        } catch (IOException e) {
            e.printStackTrace();
@ -259,38 +263,48 @@ public class ClassFileUtil {
        return null;
    }
    public static String upload(String url, File file) {
        HTTPResponse response = HttpClientKit.postFile(url, "text/plain", "file", file.toString());
        if (response.getStatusCode() != 200) {
            return null;
        }
        return response.getBody();
    }
    /**
     *  http 上传文件到中心请求
     * http 上传文件到中心请求
     *
     * @param url  请求地址
     * @param file  文件名
     * @param file 文件名
     * @return
     */
    public static boolean upload( String url,File file,String fileName) {
    public static boolean upload(String url, File file, String fileName) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        try {
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(1000*60*5).setConnectTimeout(1000*60*5).build();//设置请求和传输超时时间
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(1000 * 60 * 5).setConnectTimeout(1000 * 60 * 5).build();//设置请求和传输超时时间
            httpPost.setConfig(requestConfig);
            MultipartEntityBuilder reqEntity1 = MultipartEntityBuilder.create();
            reqEntity1.addBinaryBody("pack", file);
            reqEntity1.addTextBody("fileName",fileName, ContentType.create("text/plain", Charset.forName("UTF-8")));
            reqEntity1.addBinaryBody("file", file);
            reqEntity1.addTextBody("fileName", fileName, ContentType.create("text/plain", Charset.forName("UTF-8")));
            httpPost.setEntity(reqEntity1.build());
            response = httpClient.execute(httpPost);
            response.setHeader( "Content-Type", "application/json;charset=UTF-8");
            response.setHeader("Content-Type", "application/json;charset=UTF-8");
            entity = response.getEntity();
            int code = response.getStatusLine().getStatusCode();
            Map<String,Object> responMap = parseRespon(entity);
            if (code == 200){
            Map<String, Object> responMap = parseRespon(entity);
            if (code == 200) {
                boolean succ = (boolean) responMap.get("successFlg");
                if (!succ){
                    System.out.println("请求成功,上传失败!message:"+responMap.get("message").toString());;
                if (!succ) {
                    System.out.println("请求成功,上传失败!message:" + responMap.get("message").toString());
                    ;
                }
                return succ;
            }else {
                System.out.println("上传请求失败,status:"+code);
            } else {
                System.out.println("上传请求失败,status:" + code);
                return false;
            }
        } catch (IOException e) {
@ -309,30 +323,32 @@ public class ClassFileUtil {
    /**
     * http请求 从管理中心删除文件
     *
     * @param url
     * @return
     */
    public static boolean deleteFile( String url) {
    public static boolean deleteFile(String url) {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        CloseableHttpResponse response = null;
        HttpEntity entity = null;
        try {
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(1000*60*5).setConnectTimeout(1000*60*5).build();//设置请求和传输超时时间
            RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(1000 * 60 * 5).setConnectTimeout(1000 * 60 * 5).build();//设置请求和传输超时时间
            httpPost.setConfig(requestConfig);
            response = httpClient.execute(httpPost);
            response.setHeader( "Content-Type", "application/json;charset=UTF-8");
            response.setHeader("Content-Type", "application/json;charset=UTF-8");
            entity = response.getEntity();
            int code = response.getStatusLine().getStatusCode();
            Map<String,Object> responMap = parseRespon(entity);
            if (code == 200){
            Map<String, Object> responMap = parseRespon(entity);
            if (code == 200) {
                boolean succ = (boolean) responMap.get("successFlg");
                if (!succ){
                    System.out.println("请求成功,删除失败!message:"+responMap.get("message").toString());;
                if (!succ) {
                    System.out.println("请求成功,删除失败!message:" + responMap.get("message").toString());
                    ;
                }
                return succ;
            }else {
                System.out.println("删除请求失败,status:"+code);
            } else {
                System.out.println("删除请求失败,status:" + code);
                return false;
            }
        } catch (IOException e) {
@ -350,7 +366,7 @@ public class ClassFileUtil {
    }
    public static  Map<String,Object> parseRespon(HttpEntity entity ) throws IOException {
    public static Map<String, Object> parseRespon(HttpEntity entity) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8"));
        String line = null;
@ -359,7 +375,7 @@ public class ClassFileUtil {
        }
        String responseText = stringBuilder.toString();
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String,Object> map = objectMapper.readValue(responseText,Map.class);
        Map<String, Object> map = objectMapper.readValue(responseText, Map.class);
        return map;
    }

+ 25 - 0
hos-core/src/main/java/com/yihu/hos/core/http/DefaultClientImpl.java

@ -151,6 +151,31 @@ class DefaultClientImpl implements HTTPClient {
        return new HTTPResponse(417, "");
    }
    @Override
    public HTTPResponse postFile(String url, String contentType, String key, String path) {
        try {
            File file = new File(path);
            final MediaType type = MediaType.parse(contentType);
            Request.Builder requestBuilder = new Request.Builder();
            MultipartBody.Builder multipartBuilder = new MultipartBody.Builder();
            multipartBuilder.setType(MultipartBody.FORM)
                    .addFormDataPart(key, file.getName(), RequestBody.create(type, file));
            Request request = requestBuilder
                    .url(url)
                    .post(multipartBuilder.build())
                    .build();
            Response response = httpClient.newCall(request).execute();
            return new HTTPResponse(response.code(), response.body().string());
        } catch (IOException ex) {
            log.error(ex.getMessage());
        }
        return new HTTPResponse(417, "");
    }
    public HTTPResponse put(String url) {
        return put(url, (Map<String, String>) null);
    }

+ 2 - 0
hos-core/src/main/java/com/yihu/hos/core/http/HTTPClient.java

@ -32,6 +32,8 @@ public interface HTTPClient {
    HTTPResponse postFile(String url, String path, Map<String, String> params, Map<String, String> headers);
    HTTPResponse postFile(String url, String contentType, String key, String path);
    HTTPResponse put(String url);
    HTTPResponse put(String url, Map<String, String> params);

+ 9 - 1
hos-core/src/main/java/com/yihu/hos/core/http/HttpClientKit.java

@ -76,7 +76,7 @@ public class HttpClientKit {
        return use().post(url, json, null);
    }
    public static HTTPResponse post(String url, String json, Map<String, String> headers){
    public static HTTPResponse post(String url, String json, Map<String, String> headers) {
        return use().post(url, json, headers);
    }
@ -96,6 +96,14 @@ public class HttpClientKit {
        }
    }
    public static HTTPResponse postFile(String url, String contentType, String key, String path) {
        if (url.startsWith("https")) {
            return custom(url).postFile(url, contentType, key, path);
        } else {
            return use().postFile(url, contentType, key, path);
        }
    }
    public static HTTPResponse postFile(String url, String path, Map<String, String> params, Map<String, String> headers) {
        if (url.startsWith("https")) {
            return custom(url).postFile(url, path, params, headers);