Advertisement
Guest User

Sample_Refactor_P2

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