Advertisement
Guest User

Sample_Refactor_P1

a guest
Aug 15th, 2019
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 5.24 KB | None | 0 0
  1. import java.util.Optional;
  2.  
  3. @Component
  4. @Qualifier(API.LOAD_IMAGE)
  5. public class LoadImageApi implements IApiAdapter {
  6.  
  7.     private static final String SPLIT_TOKEN_CHARACTER = "ANGEL";
  8.    
  9.     @Autowired
  10.     private UserImageDAO userImageDAO;
  11.  
  12.     @Override
  13.     public Respond execute(Request request, Date time) {
  14.        
  15.         final String imageId = request.getImageId();
  16.         final String imageKind = request.getImageKind();
  17.        
  18.         if (StringUtils.isNullOrEmpty(imageId) || StringUtils.isNullOrEmpty(imageKind)) {
  19.             return new Respond(ErrorCode.WRONG_DATA_FORMAT);
  20.         }
  21.        
  22.         final String iasActive = request.getIasActive();
  23.         final String token = request.getToken();
  24.         if(iasActive == null && !checkToken(token)) {
  25.             return new Respond(ErrorCode.INVALID_TOKEN);
  26.         }
  27.        
  28.         String userId = request.getUserId();
  29.         final String imageKindUpdated = imageKind.equals(Constant.IMAGE_KIND_VALUE.DEMO_IMAGE) ? Constant.IMAGE_KIND_VALUE.NEWS_BANNER : imageKind;
  30.         final String imageIdUpdated = Optional.ofNullable(JsonUtils.parser(imageId))
  31.                 .map(json -> JsonUtils.getProperty(json, ParamKey.IMAGE_ID))
  32.                 .orElse(imageId);
  33.         try {
  34.             int iasInt = Integer.parseInt(iasActive);
  35.             final boolean supportImageKind = (imageKindUpdated.equals(Constant.IMAGE_KIND_VALUE.THUMBNAIL) || imageKindUpdated.equals(Constant.IMAGE_KIND_VALUE.ORIGINAL_IMAGE));
  36.             if(iasInt == Constant.FLAG.ON && supportImageKind) {
  37.                 if(userImageDAO.imageExist(imageIdUpdated, userId)) {
  38.                     return null;
  39.                 }
  40.             }
  41.  
  42.             final String imageUrl = Helper.getImageUrl(imageId, imageKind);
  43.            
  44.             if (imageUrl != null) {
  45.                 if (iasInt == Constant.FLAG.ON ||
  46.                     (iasInt != Constant.FLAG.ON && !userImageDAO.deniedImageChat(imageId, userId))) {
  47.                     ByteRespond respond = new ByteRespond();
  48.                     byte[] resultImageByte = Helper.getFile(imageUrl);
  49.                     if(resultImageByte == null) {
  50.                         String imageUrlS3;
  51.                         if(Constant.IMAGE_KIND_VALUE.THUMBNAIL.equals(imageKind)) {
  52.                             imageUrlS3 = FilesAndFolders.FOLDERS.THUMBNAIL_IMAGE_FOLDER_S3 + imageUrl.replace(FilesAndFolders.FOLDERS.THUMBNAIL_IMAGE_FOLDER, "");
  53.                         } else if(Constant.IMAGE_KIND_VALUE.ORIGINAL_IMAGE.equals(imageKind)) {
  54.                             imageUrlS3 = FilesAndFolders.FOLDERS.ORIGINAL_IMAGE_FOLDER_S3 + imageUrl.replace(FilesAndFolders.FOLDERS.ORIGINAL_IMAGE_FOLDER, "");    
  55.                         }
  56.                         HelperAWSS3.getInstance();
  57.                         resultImageByte = HelperAWSS3.getByteData(imageUrlS3);
  58.                     }
  59.                     if (resultImageByte == null) {
  60.                         return new EntityRespond(ErrorCode.FILE_NOT_FOUND);
  61.                     }
  62.                     respond.data = resultImageByte;
  63.                     return respond;
  64.                 } else {
  65.                     return new EntityRespond(ErrorCode.FILE_NOT_FOUND);
  66.                 }
  67.             } else {
  68.                 return new EntityRespond(ErrorCode.FILE_NOT_FOUND);
  69.             }
  70.  
  71.         } catch (ApplicationException ex) {
  72.             return new ByteRespond(ex.getErrorCode());
  73.         } catch (Exception ex) {
  74.             Util.addErrorLog(ex);
  75.         }
  76.         return new ByteRespond();
  77.     }
  78.  
  79.     public static boolean checkToken(String token) {
  80.         TokenElement session = null;
  81.         try {
  82.             String[] tokenArr = token.split(Pattern.quote(SPLIT_TOKEN_CHARACTER));
  83.             if (tokenArr[0] != null) {
  84.                 session = JWTCreator.getInstance().parseJWT(tokenArr[0]);
  85.             }
  86.             return session != null;
  87.         } catch (Exception e) {
  88.             return false;
  89.         }
  90.     }
  91.    
  92.     public static final class StringUtils {
  93.         private StringUtils() {throw new UnsupportedOperationException("Not allow creating instance");}
  94.        
  95.        
  96.         public static boolean isNullOrEmpty(String input) {
  97.             return input == null || input.length() == 0 ;
  98.         }
  99.     }
  100.    
  101.     public static final class JsonUtils {
  102.         private JsonUtils() {throw new UnsupportedOperationException("Not allow creating instance");}
  103.        
  104.         /**
  105.          * @return null if can not parse source
  106.          */
  107.         public static JSONObject parser(String source) {
  108.             try {
  109.                 JSONParser parser = new JSONParser();
  110.                 return (JSONObject) parser.parse(source);
  111.             } catch (ParseException e) {
  112.                 e.printStackTrace();
  113.             }
  114.             return null;
  115.         }
  116.        
  117.         /**
  118.          * @return null if {@code property} not found
  119.          */
  120.         public static String getProperty(JSONObject object, String property) {
  121.             return (String) object.get(property);
  122.         }
  123.     }
  124.    
  125.     public static final class JSONObject {
  126.        
  127.     }
  128.    
  129.     public static final class ParamKey {
  130.         public static final String IMAGE_ID = "IMAGE_ID";
  131.     }
  132.    
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement