aboutsummaryrefslogtreecommitdiff
path: root/juick-common/src/main/java/com/juick/server/util
diff options
context:
space:
mode:
authorGravatar Vitaly Takmazov2018-09-25 11:53:37 +0300
committerGravatar Vitaly Takmazov2018-09-25 11:53:37 +0300
commitae76024011a8442ae7eab953e0b97e9fe2c7c201 (patch)
tree9ec4bc077ad2db21dcfce0eefa126a484b1d0fc7 /juick-common/src/main/java/com/juick/server/util
parent85abc3e201539f81500754a7fb0e4f3a9d148fc4 (diff)
reorganize layout
Diffstat (limited to 'juick-common/src/main/java/com/juick/server/util')
-rw-r--r--juick-common/src/main/java/com/juick/server/util/HttpBadRequestException.java32
-rw-r--r--juick-common/src/main/java/com/juick/server/util/HttpForbiddenException.java33
-rw-r--r--juick-common/src/main/java/com/juick/server/util/HttpNotFoundException.java32
-rw-r--r--juick-common/src/main/java/com/juick/server/util/HttpUtils.java110
-rw-r--r--juick-common/src/main/java/com/juick/server/util/ImageUtils.java175
-rw-r--r--juick-common/src/main/java/com/juick/server/util/TagUtils.java42
-rw-r--r--juick-common/src/main/java/com/juick/server/util/UserUtils.java55
-rw-r--r--juick-common/src/main/java/com/juick/server/util/WebUtils.java62
8 files changed, 0 insertions, 541 deletions
diff --git a/juick-common/src/main/java/com/juick/server/util/HttpBadRequestException.java b/juick-common/src/main/java/com/juick/server/util/HttpBadRequestException.java
deleted file mode 100644
index 242f2b09..00000000
--- a/juick-common/src/main/java/com/juick/server/util/HttpBadRequestException.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package com.juick.server.util;
-
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.http.HttpStatus;
-import org.springframework.web.bind.annotation.ResponseStatus;
-
-/**
- * Created by vt on 24/11/2016.
- */
-@ResponseStatus(value = HttpStatus.BAD_REQUEST)
-public class HttpBadRequestException extends RuntimeException {
- public HttpBadRequestException() {
- super("the request was bad", null, false, false);
- }
-}
diff --git a/juick-common/src/main/java/com/juick/server/util/HttpForbiddenException.java b/juick-common/src/main/java/com/juick/server/util/HttpForbiddenException.java
deleted file mode 100644
index 3251ca38..00000000
--- a/juick-common/src/main/java/com/juick/server/util/HttpForbiddenException.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package com.juick.server.util;
-
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.http.HttpStatus;
-import org.springframework.web.bind.annotation.ResponseStatus;
-
-/**
- * Created by vt on 24/11/2016.
- */
-@ResponseStatus(value = HttpStatus.FORBIDDEN)
-public class HttpForbiddenException extends RuntimeException {
- public HttpForbiddenException() {
- super(StringUtils.EMPTY, null, false, false);
- }
-
-}
diff --git a/juick-common/src/main/java/com/juick/server/util/HttpNotFoundException.java b/juick-common/src/main/java/com/juick/server/util/HttpNotFoundException.java
deleted file mode 100644
index f66ece8b..00000000
--- a/juick-common/src/main/java/com/juick/server/util/HttpNotFoundException.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package com.juick.server.util;
-
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.http.HttpStatus;
-import org.springframework.web.bind.annotation.ResponseStatus;
-
-/**
- * Created by vt on 24/11/2016.
- */
-@ResponseStatus(value = HttpStatus.NOT_FOUND)
-public class HttpNotFoundException extends RuntimeException {
- public HttpNotFoundException() {
- super(StringUtils.EMPTY, null, false, false);
- }
-}
diff --git a/juick-common/src/main/java/com/juick/server/util/HttpUtils.java b/juick-common/src/main/java/com/juick/server/util/HttpUtils.java
deleted file mode 100644
index 9f356aa5..00000000
--- a/juick-common/src/main/java/com/juick/server/util/HttpUtils.java
+++ /dev/null
@@ -1,110 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package com.juick.server.util;
-
-import org.apache.commons.codec.digest.DigestUtils;
-import org.apache.commons.lang3.StringUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.web.multipart.MultipartFile;
-
-import javax.imageio.ImageIO;
-import javax.imageio.ImageReader;
-import javax.imageio.stream.ImageInputStream;
-import java.io.BufferedInputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URI;
-import java.net.URL;
-import java.net.URLConnection;
-import java.nio.file.Files;
-import java.nio.file.Paths;
-import java.util.Iterator;
-import java.util.UUID;
-
-/**
- *
- * @author Ugnich Anton
- */
-public class HttpUtils {
- private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class);
-
- public static URI receiveMultiPartFile(MultipartFile attach, String tmpDir) throws IOException {
- if (attach != null && !attach.isEmpty()) {
- ImageInputStream iis = ImageIO.createImageInputStream(attach.getInputStream());
- Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
-
- String format = StringUtils.EMPTY;
- while (readers.hasNext()) {
- ImageReader read = readers.next();
- format = read.getFormatName();
- }
- String attachmentType = attachmentTypeFromFormat(format);
- if (attachmentType.equals("jpg") || attachmentType.equals("png")) {
- String attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType;
- try {
- Files.write(Paths.get(tmpDir, attachmentFName),
- attach.getBytes());
- return URI.create(String.format("juick://%s", attachmentFName));
- } catch (IOException e) {
- logger.warn("file receive error", e);
- }
- }
- logger.warn("file type is unknown: {}", attach.getOriginalFilename());
- }
- return URI.create(StringUtils.EMPTY);
- }
-
- private static String attachmentTypeFromFormat(String format) throws IOException {
- if (format != null && format.equals("JPEG")) {
- return "jpg";
- } else if (format != null && format.equals("png")) {
- return "png";
- } else {
- throw new IOException("Wrong file type: " + format);
- }
- }
-
- public static URI downloadImage(URL url, String tmpDir) throws IOException {
- ImageInputStream iis = ImageIO.createImageInputStream(url.openStream());
- Iterator<ImageReader> readers = ImageIO.getImageReaders(iis);
-
- String format = StringUtils.EMPTY;
- while (readers.hasNext()) {
- ImageReader read = readers.next();
- format = read.getFormatName();
- }
- URLConnection urlConn;
- try {
- urlConn = url.openConnection();
- } catch (IOException e) {
- logger.error(String.format("Failed open url: %s", url.toString()));
- throw e;
- }
-
- try (InputStream is = new BufferedInputStream(urlConn.getInputStream())) {
- String attachmentType = attachmentTypeFromFormat(format);
-
- String attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType;
- Files.copy(is, Paths.get(tmpDir, attachmentFName));
- return URI.create(String.format("juick://%s", attachmentFName));
- } catch (IOException e) {
- logger.error(String.format("Failed download image by url: %s", url.toString()), e);
- throw e;
- }
- }
-}
diff --git a/juick-common/src/main/java/com/juick/server/util/ImageUtils.java b/juick-common/src/main/java/com/juick/server/util/ImageUtils.java
deleted file mode 100644
index d16faf8f..00000000
--- a/juick-common/src/main/java/com/juick/server/util/ImageUtils.java
+++ /dev/null
@@ -1,175 +0,0 @@
-
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package com.juick.server.util;
-
-import com.juick.Attachment;
-import org.apache.commons.imaging.ImageReadException;
-import org.apache.commons.imaging.Imaging;
-import org.apache.commons.imaging.common.ImageMetadata;
-import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
-import org.apache.commons.imaging.formats.tiff.TiffField;
-import org.apache.commons.imaging.formats.tiff.constants.TiffTagConstants;
-import org.apache.commons.io.FileUtils;
-import org.apache.commons.io.FilenameUtils;
-import org.imgscalr.Scalr;
-import org.imgscalr.Scalr.Rotation;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.imageio.ImageIO;
-import javax.imageio.ImageReader;
-import javax.imageio.stream.FileImageInputStream;
-import javax.imageio.stream.ImageInputStream;
-import java.awt.image.BufferedImage;
-import java.io.File;
-import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
-import java.nio.file.StandardCopyOption;
-import java.util.Iterator;
-
-public class ImageUtils {
- private static final Logger logger = LoggerFactory.getLogger(ImageUtils.class);
-
- private String imgDir;
- private String tmpDir;
-
- public ImageUtils(String imgDir, String tmpDir) {
- this.imgDir = imgDir;
- this.tmpDir = tmpDir;
- }
-/**
- * Returns <code>BufferedImage</code>, same as <code>ImageIO.read()</code> does.
- *
- * <p>JPEG images with EXIF metadata are rotated according to Orientation tag.
- *
- * @param imageFile a <code>File</code> to read from.
- */
- private static BufferedImage readImageWithOrientation(File imageFile)
- throws IOException {
-
- BufferedImage image = ImageIO.read(imageFile);
- if (!FilenameUtils.getExtension(imageFile.getName()).equals("jpg")) {
- return image;
- }
-
- try {
- ImageMetadata metadata = Imaging.getMetadata(imageFile);
-
- if (metadata instanceof JpegImageMetadata) {
- JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;
- TiffField orientationField = jpegMetadata.findEXIFValue(TiffTagConstants.TIFF_TAG_ORIENTATION);
-
- if (orientationField != null) {
- int orientation = orientationField.getIntValue();
- switch (orientation) {
- case TiffTagConstants.ORIENTATION_VALUE_ROTATE_90_CW:
- image = Scalr.rotate(image, Rotation.CW_90);
- break;
- case TiffTagConstants.ORIENTATION_VALUE_ROTATE_180:
- image = Scalr.rotate(image, Rotation.CW_180);
- break;
- case TiffTagConstants.ORIENTATION_VALUE_ROTATE_270_CW:
- image = Scalr.rotate(image, Rotation.CW_270);
- break;
- case TiffTagConstants.ORIENTATION_VALUE_MIRROR_HORIZONTAL:
- image = Scalr.rotate(image, Rotation.FLIP_HORZ);
- break;
- case TiffTagConstants.ORIENTATION_VALUE_MIRROR_VERTICAL:
- image = Scalr.rotate(image, Rotation.FLIP_VERT);
- break;
- case TiffTagConstants.ORIENTATION_VALUE_MIRROR_HORIZONTAL_AND_ROTATE_90_CW:
- image = Scalr.rotate(Scalr.rotate(image, Rotation.FLIP_HORZ), Rotation.CW_90);
- break;
- case TiffTagConstants.ORIENTATION_VALUE_MIRROR_HORIZONTAL_AND_ROTATE_270_CW:
- image = Scalr.rotate(Scalr.rotate(image, Rotation.FLIP_HORZ), Rotation.CW_270);
- break;
- case TiffTagConstants.ORIENTATION_VALUE_HORIZONTAL_NORMAL:
- default:
- // do nothing
- break;
- }
- }
- }
- } catch (ImageReadException e) {
- // failed to read metadata.
- // nothing to do here, return image as is.
- }
-
- return image;
- }
-
- public void saveImageWithPreviews(String tempFilename, String outputFilename)
- throws IOException {
- String ext = FilenameUtils.getExtension(outputFilename);
-
- Path outputImagePath = Paths.get(imgDir, "p", outputFilename);
- // this throws strange exceptions
- // Files.move(Paths.get(tmpDir, tempFilename), outputImagePath);
- FileUtils.moveFile(Paths.get(tmpDir, tempFilename).toFile(), outputImagePath.toFile());
- BufferedImage originalImage = readImageWithOrientation(outputImagePath.toFile());
-
- int width = originalImage.getWidth();
- int height = originalImage.getHeight();
- int maxDimension = (width > height) ? width : height;
- BufferedImage image1024 = (maxDimension > 1024) ? Scalr.resize(originalImage, 1024) : originalImage;
- BufferedImage image0512 = (maxDimension > 512) ? Scalr.resize(originalImage, 512) : originalImage;
- BufferedImage image0160 = (maxDimension > 160) ? Scalr.resize(originalImage, 160) : originalImage;
- ImageIO.write(image1024, ext, Paths.get(imgDir, "photos-1024", outputFilename).toFile());
- ImageIO.write(image0512, ext, Paths.get(imgDir, "photos-512", outputFilename).toFile());
- ImageIO.write(image0160, ext, Paths.get(imgDir, "ps", outputFilename).toFile());
- }
-
- public void saveAvatar(String tempFilename, int uid)
- throws IOException {
- String ext = FilenameUtils.getExtension(tempFilename);
- String originalName = String.format("%s.%s", uid, ext);
- Path originalPath = Paths.get(imgDir, "ao", originalName);
- Files.move(Paths.get(tmpDir, tempFilename), originalPath, StandardCopyOption.REPLACE_EXISTING);
- BufferedImage originalImage = ImageIO.read(originalPath.toFile());
-
- String targetExt = "png";
- String targetName = String.format("%s.%s", uid, targetExt);
- ImageIO.write(Scalr.resize(originalImage, 96), targetExt, Paths.get(imgDir, "a", targetName).toFile());
- ImageIO.write(Scalr.resize(originalImage, 32), targetExt, Paths.get(imgDir, "as", targetName).toFile());
- }
- public Attachment getAttachment(File imgFile) throws IOException {
- Attachment attachment = new Attachment();
- try (ImageInputStream stream = ImageIO.createImageInputStream(imgFile)) {
- Iterator<ImageReader> iter = ImageIO.getImageReaders(stream);
- while (iter.hasNext()) {
- ImageReader reader = iter.next();
- try {
- reader.setInput(stream);
- attachment.setWidth(reader.getWidth(reader.getMinIndex()));
- attachment.setHeight(reader.getHeight(reader.getMinIndex()));
- return attachment;
- } catch (Exception e) {
- logger.debug("Error reading {}, trying next reader", imgFile.getAbsolutePath());
- } finally {
- reader.dispose();
- }
- }
- }
-
- logger.warn("Not a known image file {}", imgFile.getAbsolutePath());
- return attachment;
- }
-} \ No newline at end of file
diff --git a/juick-common/src/main/java/com/juick/server/util/TagUtils.java b/juick-common/src/main/java/com/juick/server/util/TagUtils.java
deleted file mode 100644
index cb828933..00000000
--- a/juick-common/src/main/java/com/juick/server/util/TagUtils.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package com.juick.server.util;
-
-import com.juick.Tag;
-import org.apache.commons.collections4.CollectionUtils;
-import org.apache.commons.lang3.StringUtils;
-
-import java.util.List;
-import java.util.stream.Collectors;
-
-/**
- * Created by aalexeev on 11/13/16.
- */
-public class TagUtils {
- private TagUtils() {
- throw new IllegalStateException();
- }
-
- public static String toString(final List<Tag> tags) {
- if (CollectionUtils.isEmpty(tags))
- return StringUtils.EMPTY;
-
- return tags.stream().map(t -> "*" + t.getName())
- .collect(Collectors.joining(" "));
- }
-}
diff --git a/juick-common/src/main/java/com/juick/server/util/UserUtils.java b/juick-common/src/main/java/com/juick/server/util/UserUtils.java
deleted file mode 100644
index ab5c320b..00000000
--- a/juick-common/src/main/java/com/juick/server/util/UserUtils.java
+++ /dev/null
@@ -1,55 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package com.juick.server.util;
-
-import com.juick.User;
-import com.juick.server.helpers.AnonymousUser;
-import com.juick.service.security.entities.JuickUser;
-import javax.annotation.Nonnull;
-import org.springframework.security.core.Authentication;
-import org.springframework.security.core.context.SecurityContextHolder;
-
-/**
- * Created by aalexeev on 11/14/16.
- */
-public class UserUtils {
- private UserUtils() {
- throw new IllegalStateException();
- }
-
- public static Authentication getAuthentication() {
- return SecurityContextHolder.getContext().getAuthentication();
- }
-
- public static Object getPrincipal(final Authentication authentication) {
- return authentication == null ? null : authentication.getPrincipal();
- }
-
- @Nonnull
- public static User getCurrentUser() {
- Object principal = getPrincipal(getAuthentication());
-
- if (principal instanceof JuickUser)
- return ((JuickUser) principal).getUser();
-
- if (principal instanceof User)
- return (User) principal;
-
- return AnonymousUser.INSTANCE;
- }
-}
diff --git a/juick-common/src/main/java/com/juick/server/util/WebUtils.java b/juick-common/src/main/java/com/juick/server/util/WebUtils.java
deleted file mode 100644
index 9dd628ee..00000000
--- a/juick-common/src/main/java/com/juick/server/util/WebUtils.java
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Copyright (C) 2008-2017, Juick
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-
-package com.juick.server.util;
-
-import java.util.regex.Pattern;
-
-/**
- * Created by aalexeev on 11/28/16.
- */
-public class WebUtils {
- private WebUtils() {
- throw new IllegalStateException();
- }
-
- private static final Pattern USER_NAME_PATTERN = Pattern.compile("[a-zA-Z-_\\d]{2,16}");
-
- private static final Pattern POST_NUMBER_PATTERN = Pattern.compile("-?\\d+");
-
- private static final Pattern JID_PATTERN = Pattern.compile("^[a-zA-Z0-9\\\\-\\\\_\\\\@\\\\.]{6,64}$");
-
-
- public static boolean isPostNumber(final String aString) {
- return aString != null && POST_NUMBER_PATTERN.matcher(aString).matches();
- }
-
- public static boolean isNotPostNumber(final String aString) {
- return !isPostNumber(aString);
- }
-
- public static boolean isUserName(final String aString) {
- return aString != null && USER_NAME_PATTERN.matcher(aString).matches();
- }
-
- public static boolean isNotUserName(final String aString) {
- return !isUserName(aString);
- }
-
- public static boolean isJid(final String aString) {
- return aString != null && JID_PATTERN.matcher(aString).matches();
- }
-
- public static boolean isNotJid(final String aString) {
- return !isJid(aString);
- }
-
-
-}