浏览代码

Merge branch 'master' of http://192.168.1.220:10080/esb/esb

zhenglingfeng 8 年之前
父节点
当前提交
ff5ad946c6

+ 20 - 8
hos-broker/src/main/java/com/yihu/hos/broker/services/camel/ESBCamelService.java

@ -13,7 +13,6 @@ import com.yihu.hos.core.encrypt.DES;
import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.model.bo.ServiceFlow;
import com.yihu.hos.web.framework.util.GridFSUtil;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.bson.Document;
@ -21,6 +20,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
@ -236,14 +236,19 @@ public class ESBCamelService {
        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);
//        MongoDatabase db = mongoConfig.mongoClient().getDatabase(dbName);
//        boolean read = GridFSUtil.readFile(db, outputStream, fileName);
        //从中心下载
        String downUrl = centerUrl + "/tenant/down/" + handleFile.getFilePath();
        String sourcePath =  ClassFileUtil.downFile(downUrl, resource, handleFile.getPackageName(), handleFile.getClassName(), ClassFileUtil.CLASS_FILE);
        boolean read = GridFSUtil.readFile(db, outputStream, fileName);
        if (!read) {
        if (sourcePath==null) {
            logger.error("not mongo file, fileName:" + fileName);
            return false;
        }
        return read;
        return true;
    }
    private boolean generateClassFile(ServiceFlow.HandleFile handleFile) throws Exception {
@ -253,7 +258,7 @@ 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);
//        String fileName = DES.decrypt(handleFile.getFilePath(), DES.COMMON_PASSWORD);
        String fileName = DES.decrypt(handleFile.getFilePath(), DES.COMMON_PASSWORD);
//        MongoDatabase db = mongoConfig.mongoClient().getDatabase(dbName);
//
//        boolean read = GridFSUtil.readFileContent(db, outputStream, fileName);
@ -276,8 +281,11 @@ public class ESBCamelService {
            //编译成功后将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
//            GridFSUtil.uploadFile(classPath, handleFile.getClassName() + handleFile.getRouteCode() + ClassFileUtil.CLASS_FILE, null);
//            TODO 上传到本地mongodb和中心mongodb
            String enFileName = DES.encrypt(handleFile.getClassName() + handleFile.getRouteCode() + ClassFileUtil.CLASS_FILE, DES.COMMON_PASSWORD);
            String uploadUrl = centerUrl + "/tenant/upload/" + enFileName;
            ClassFileUtil.uploadFile(uploadUrl,new File(classPath),handleFile.getClassName() + handleFile.getRouteCode() + ClassFileUtil.CLASS_FILE);
        }
        return succ;
    }
@ -294,6 +302,10 @@ public class ESBCamelService {
        ClassFileUtil.deleteClassfile(classPath);
        ClassFileUtil.deleteClassfile(javaPath);
        //TODO 从中心删除
        String uploadUrl = centerUrl + "/tenant/delFile/" + javaPath;
        ClassFileUtil.deleteFile(uploadUrl);
        // 完成
        logger.info("===================" + handleFile.getPackageName() + CoreConstant.DOT + handleFile.getClassName() + ".class 删除过程结束");
    }

+ 83 - 0
hos-core/src/main/java/com/yihu/hos/core/datatype/ClassFileUtil.java

@ -8,6 +8,9 @@ import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
@ -17,6 +20,7 @@ import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
public class ClassFileUtil {
    public static final String CLASS_FILE = ".class";
@ -209,6 +213,20 @@ public class ClassFileUtil {
        }
    }
    public static String uploadFile(String uploadUrl, File file,String fileName) {
        try {
            byte[] bytes = upload(uploadUrl, file,fileName);//文件内容
            if (bytes!=null){
                return fileName;
            }else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }
    /**
     *  http接口下载文件
     * @param url  请求地址
@ -241,4 +259,69 @@ public class ClassFileUtil {
        return null;
    }
    /**
     *  http 上传文件请求
     * @param url  请求地址
     * @param file  文件名
     * @return
     */
    public static byte[] 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();//设置请求和传输超时时间
            httpPost.setConfig(requestConfig);
            MultipartEntityBuilder reqEntity1 = MultipartEntityBuilder.create();
            reqEntity1.addBinaryBody("pack", 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/octet-stream;charset=UTF-8");
            entity = response.getEntity();
            byte[] bytes= FileUtil.getBytesByStream(entity.getContent());
//            EntityUtils.toString(response.getEntity(), "UTF-8");
            return bytes;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                EntityUtils.consume(entity);
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    public static String 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();//设置请求和传输超时时间
            httpPost.setConfig(requestConfig);
            response = httpClient.execute(httpPost);
            response.setHeader( "Content-Type", "application/octet-stream;charset=UTF-8");
            entity = response.getEntity();
            return entity.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                EntityUtils.consume(entity);
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

+ 12 - 0
hos-web-framework/src/main/java/com/yihu/hos/web/framework/util/GridFSUtil.java

@ -370,4 +370,16 @@ public class GridFSUtil {
        }
        return null;
    }
    public static boolean deleteFile(String fileName){
        try {
            gridFsOperations.delete(Query.query(GridFsCriteria.where("filename").is(fileName)));//删除原来的文件,保证唯一
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}

+ 2 - 0
src/main/java/com/yihu/hos/filter/SessionOutTimeFilter.java

@ -34,6 +34,8 @@ public class SessionOutTimeFilter extends OncePerRequestFilter {
                || path.indexOf("swagger") != -1
                || path.indexOf(httpServletRequest.getContextPath() + "/v2/api-docs") != -1
                || path.indexOf("/tenant/down") != -1
                || path.indexOf("/tenant/upload") != -1
                || path.indexOf("/tenant/delFile") != -1
                || path.indexOf(httpServletRequest.getContextPath() + "/mobile") != -1) {
            filterChain.doFilter(httpServletRequest, httpServletResponse);
            return;

+ 29 - 9
src/main/java/com/yihu/hos/tenant/controller/TenantController.java

@ -4,14 +4,13 @@ import com.yihu.hos.tenant.model.TenantModel;
import com.yihu.hos.tenant.service.TenantService;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import io.swagger.annotations.ApiParam;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
@ -182,6 +181,28 @@ public class TenantController extends BaseController{
    }
    /**
     *  //TODO 后 独立到对应的rest接口中
     * 上传文件
     * @param response
     * @param fileName
     * @return
     */
    @RequestMapping(value = "/upload/{fileName}", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public Object upload(
            HttpServletResponse response, @PathVariable("fileName")  String fileName,
                         @ApiParam(name = "pack", value = "文件", allowMultiple = true)
                         @RequestPart() MultipartFile pack
                         ) {
        try {
            return tenantService.uploadFile(pack.getInputStream(), fileName);
        } catch (Exception e) {
            return Result.error("上传文件出错!");
        }
    }
    /**
     *  //TODO 后 独立到对应的rest接口中
     * 文件下载
@ -192,9 +213,9 @@ public class TenantController extends BaseController{
    public Object down(HttpServletResponse response, @PathVariable("fileName")  String fileName) {
        try {
            OutputStream os = response.getOutputStream();
            return tenantService.readFile(os, fileName);
            return tenantService.dowFile(os, fileName);
        } catch (Exception e) {
            return Result.error("下载文件失败!");
            return Result.error("下载文件出错!");
        }
    }
@ -203,12 +224,11 @@ public class TenantController extends BaseController{
     * 删除文件
     * @return
     */
    @RequestMapping(value = "/delFile/{fileName}", produces = "application/json;charset=UTF-8", method = RequestMethod.GET)
    @RequestMapping(value = "/delFile/{fileName}", produces = "application/json;charset=UTF-8", method = RequestMethod.POST)
    @ResponseBody
    public Object delFile(HttpServletResponse response, @PathVariable("fileName")  String fileName) {
        try {
            OutputStream os = response.getOutputStream();
            return tenantService.readFile(os, fileName);
            return tenantService.delFile(fileName);
        } catch (Exception e) {
            return Result.error("下载文件失败!");
        }

+ 40 - 4
src/main/java/com/yihu/hos/tenant/service/TenantService.java

@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
@ -85,15 +86,50 @@ public class TenantService {
        return Result.success("删除成功");
    }
    public Result readFile(OutputStream os, String fileName) {
    public Result dowFile(OutputStream os, String fileName) {
        try {
            fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
            GridFSUtil.readFile(mongoConfig.mongoClient().getDatabase(dbName), os, fileName);
            return Result.success("读取成功");
            boolean succ = GridFSUtil.readFile(mongoConfig.mongoClient().getDatabase(dbName), os, fileName);
            if (succ){
                return Result.success("读取文件成功");
            }else {
                return Result.success("读取文件失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("读取文件异常");
        }
        return Result.error("读取失败");
    }
    public Result uploadFile(InputStream inputStream, String fileName) {
        try {
            fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
            String saveFileName = GridFSUtil.uploadFile(inputStream,fileName,null);
            if (saveFileName != null){
                return Result.success("上传文件成功");
            }else {
                return Result.error("上传文件失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("上传文件异常");
        }
    }
    public Result delFile( String fileName) {
        try {
            fileName = DES.decrypt(fileName, DES.COMMON_PASSWORD);
            boolean succ = GridFSUtil.deleteFile( fileName);
            if (succ){
                return Result.success("删除文件成功");
            }else {
                return Result.success("删除文件失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除文件异常");
        }
    }
}