From 559ae343aa72db04e12b94acfa0bec30ed9a11f1 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Fri, 9 Dec 2016 16:14:27 +0300 Subject: juick-spring-www: merge some work from develop branch, port some templates to Thymeleaf --- .../juick/server/util/HttpBadRequestException.java | 12 +++ .../juick/server/util/HttpForbiddenException.java | 12 +++ .../juick/server/util/HttpNotFoundException.java | 12 +++ .../main/java/com/juick/server/util/HttpUtils.java | 111 +++++++++++++++++++++ .../main/java/com/juick/service/EmailService.java | 8 ++ .../java/com/juick/service/EmailServiceImpl.java | 33 ++++++ 6 files changed, 188 insertions(+) create mode 100644 juick-server/src/main/java/com/juick/server/util/HttpBadRequestException.java create mode 100644 juick-server/src/main/java/com/juick/server/util/HttpForbiddenException.java create mode 100644 juick-server/src/main/java/com/juick/server/util/HttpNotFoundException.java create mode 100644 juick-server/src/main/java/com/juick/server/util/HttpUtils.java create mode 100644 juick-server/src/main/java/com/juick/service/EmailService.java create mode 100644 juick-server/src/main/java/com/juick/service/EmailServiceImpl.java (limited to 'juick-server/src/main/java') diff --git a/juick-server/src/main/java/com/juick/server/util/HttpBadRequestException.java b/juick-server/src/main/java/com/juick/server/util/HttpBadRequestException.java new file mode 100644 index 00000000..cd862830 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/util/HttpBadRequestException.java @@ -0,0 +1,12 @@ +package com.juick.server.util; + +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 { + +} diff --git a/juick-server/src/main/java/com/juick/server/util/HttpForbiddenException.java b/juick-server/src/main/java/com/juick/server/util/HttpForbiddenException.java new file mode 100644 index 00000000..7e85816e --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/util/HttpForbiddenException.java @@ -0,0 +1,12 @@ +package com.juick.server.util; + +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 { + +} diff --git a/juick-server/src/main/java/com/juick/server/util/HttpNotFoundException.java b/juick-server/src/main/java/com/juick/server/util/HttpNotFoundException.java new file mode 100644 index 00000000..378eadfc --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/util/HttpNotFoundException.java @@ -0,0 +1,12 @@ +package com.juick.server.util; + +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 { + +} diff --git a/juick-server/src/main/java/com/juick/server/util/HttpUtils.java b/juick-server/src/main/java/com/juick/server/util/HttpUtils.java new file mode 100644 index 00000000..fa0b2ec2 --- /dev/null +++ b/juick-server/src/main/java/com/juick/server/util/HttpUtils.java @@ -0,0 +1,111 @@ +/* + * Juick + * Copyright (C) 2008-2011, Ugnich Anton + * + * 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 . + */ +package com.juick.server.util; + +import org.apache.commons.codec.digest.DigestUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.multipart.MultipartFile; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.net.URLConnection; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.UUID; + +/** + * + * @author Ugnich Anton + */ +public class HttpUtils { + private static final Logger logger = LoggerFactory.getLogger(HttpUtils.class); + + public static String receiveMultiPartFile(MultipartFile attach, String tmpDir) { + if (attach !=null && !attach.isEmpty()) { + String partname = attach.getOriginalFilename(); + if (partname != null && partname.length() > 0) { + String attachmentType = partname.substring(partname.length() - 3).toLowerCase(); + if (attachmentType.equals("jpg") || attachmentType.equals("peg") || attachmentType.equals("png")) { + if (attachmentType.equals("peg")) { + attachmentType = "jpg"; + } + String attachmentFName = DigestUtils.md5Hex(UUID.randomUUID().toString()) + "." + attachmentType; + try { + Files.write(Paths.get(tmpDir, attachmentFName), + attach.getBytes()); + return attachmentFName; + } catch (IOException e) { + logger.warn("file receive error", e); + } + } + } + } + return ""; + } + public static String downloadImage(URL url) throws Exception { + String attachmentFName = null; + Exception ex = null; + + InputStream is = null; + FileOutputStream fos = null; + try { + URLConnection urlConn = url.openConnection(); + is = urlConn.getInputStream(); + String mime = urlConn.getContentType(); + + String attachmentType; + if (mime != null && mime.equals("image/jpeg")) { + attachmentType = "jpg"; + } else if (mime != null && mime.equals("image/png")) { + attachmentType = "png"; + } else { + throw new Exception("Wrong file type"); + } + + attachmentFName = UUID.randomUUID().toString() + "." + attachmentType; + fos = new FileOutputStream("/var/www/juick.com/i/tmp/" + attachmentFName); + byte[] buffer = new byte[10240]; + int len; + while ((len = is.read(buffer)) > 0) { + fos.write(buffer, 0, len); + } + } catch (Exception e) { + ex = e; + attachmentFName = null; + } finally { + try { + if (is != null) { + is.close(); + } + } finally { + if (fos != null) { + fos.close(); + } + } + } + + if (ex != null) { + throw ex; + } else { + return attachmentFName; + } + } +} diff --git a/juick-server/src/main/java/com/juick/service/EmailService.java b/juick-server/src/main/java/com/juick/service/EmailService.java new file mode 100644 index 00000000..f62f4b32 --- /dev/null +++ b/juick-server/src/main/java/com/juick/service/EmailService.java @@ -0,0 +1,8 @@ +package com.juick.service; + +/** + * Created by vitalyster on 09.12.2016. + */ +public interface EmailService { + boolean verifyAddressByCode(Integer userId, String code); +} diff --git a/juick-server/src/main/java/com/juick/service/EmailServiceImpl.java b/juick-server/src/main/java/com/juick/service/EmailServiceImpl.java new file mode 100644 index 00000000..bdd78609 --- /dev/null +++ b/juick-server/src/main/java/com/juick/service/EmailServiceImpl.java @@ -0,0 +1,33 @@ +package com.juick.service; + +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +import javax.inject.Inject; + +/** + * Created by vitalyster on 09.12.2016. + */ +@Repository +@Transactional +public class EmailServiceImpl extends BaseJdbcService implements EmailService { + @Inject + public EmailServiceImpl(JdbcTemplate jdbcTemplate) { + super(jdbcTemplate, null); + } + + @Override + public boolean verifyAddressByCode(Integer userId, String code) { + try { + String address = getJdbcTemplate().queryForObject("SELECT account FROM auth WHERE user_id=? AND protocol='email' AND authcode=?", + String.class, userId, code); + getJdbcTemplate().update("INSERT INTO emails(user_id,email) VALUES (?,?)", userId, address); + getJdbcTemplate().update("DELETE FROM auth WHERE user_id=? AND authcode=?", userId, code); + } catch (EmptyResultDataAccessException e) { + return false; + } + return true; + } +} -- cgit v1.2.3