package com.juick.www; import com.juick.Tag; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.math.NumberUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.jdbc.core.JdbcTemplate; import rocks.xmpp.addr.Jid; import rocks.xmpp.core.session.XmppSession; import rocks.xmpp.core.stanza.model.Message; import rocks.xmpp.extensions.oob.model.x.OobX; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.List; import java.util.stream.Collectors; /** * Created by vitalyster on 08.12.2016. */ public class XMPPPost { private final static Logger logger = LoggerFactory.getLogger(XMPPPost.class); public void doPostMessage(JdbcTemplate sql, HttpServletRequest request, HttpServletResponse response, XmppSession xmpp, com.juick.User visitor) throws ServletException, IOException { if (visitor.getUid() == 0) { response.sendError(HttpServletResponse.SC_FORBIDDEN); return; } String body = request.getParameter("body").replace("\r", ""); int mid = NumberUtils.toInt(request.getParameter("mid"), 0); int rid = NumberUtils.toInt(request.getParameter("rid"), 0); if (mid > 0 && rid > 0) { body = String.format("#%d/%d %s", mid, rid, body); } else if (mid > 0) { body = String.format("#%d %s", mid, body); } else { // is a post List tags = Utils.parseTags(sql, request.getParameter("tags")); body = String.format("%s %s", tags.stream() .map(t -> "*" + t.getName()).collect(Collectors.joining(" ")), body); } String attachmentFName; try { attachmentFName = Utils.receiveAttachment(request.getPart("attach"), request.getParameter("img")); } catch (Exception e) { logger.error("MULTIPART ERROR", e); response.sendError(HttpServletResponse.SC_BAD_REQUEST); return; } Message msg = new Message(); msg.setType(Message.Type.CHAT); msg.setFrom(Jid.of(String.valueOf(visitor.getUid()), "uid.juick.com", "perl")); msg.setTo(Jid.of("juick@juick.com/Juick")); msg.setBody(body); try { if (attachmentFName != null) { String attachmentUrl = String.format("juick://%s", attachmentFName); msg.addExtension(new OobX(new URI(attachmentUrl), "!!!!Juick!!")); } xmpp.sendMessage(msg); } catch (URISyntaxException e1) { logger.warn("attachment error", e1); } String referer = request.getHeader("referer"); if (StringUtils.isBlank(referer) || referer.substring(0, 21).equals("http://juick.com/post") || referer.substring(0, 22).equals("https://juick.com/post")) { response.sendRedirect("/?show=my"); return; } response.sendRedirect(referer); } }