/*
* Juick
* Copyright (C) 2008-2013, 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.api;
import com.juick.xmpp.JID;
import com.juick.xmpp.Stream;
import com.juick.xmpp.StreamComponent;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.LogManager;
/**
*
* @author Ugnich Anton
*/
@WebServlet(name = "Main", urlPatterns = {"/"})
@MultipartConfig
public class Main extends HttpServlet implements Stream.StreamListener {
JdbcTemplate jdbc;
Stream xmpp;
Messages messages;
Users users;
PM pm;
Others others;
@Override
public void init() throws ServletException {
super.init();
try {
LogManager.getLogManager().readConfiguration(getServletContext().getResourceAsStream("WEB-INF/logging.properties"));
Properties conf = new Properties();
conf.load(getServletContext().getResourceAsStream("WEB-INF/juick.conf"));
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(conf.getProperty("datasource_driver"));
dataSource.setUrl(conf.getProperty("datasource_url"));
jdbc = new JdbcTemplate(dataSource);
messages = new Messages(jdbc);
users = new Users(jdbc);
pm = new PM(jdbc);
others = new Others(jdbc);
setupXmppComponent(conf.getProperty("xmpp_host", "localhost"), Integer.parseInt(conf.getProperty("xmpp_port", "5347")),
conf.getProperty("xmpp_jid", "api.localhost"), conf.getProperty("xmpp_password"));
} catch (IOException e) {
log("API initialization error", e);
}
}
public void setupXmppComponent(final String host, final int port, final String jid, final String password) {
ExecutorService executorService = Executors.newSingleThreadExecutor();
executorService.submit(() -> {
try {
Socket socket = new Socket(host, port);
xmpp = new StreamComponent(new JID(jid), socket.getInputStream(), socket.getOutputStream(), password);
xmpp.addListener(Main.this);
xmpp.startParsing();
} catch (IOException e) {
log("XMPP exception", e);
}
});
}
@Override
public void onStreamFail(String msg) {
log("XMPP failed: " + msg);
}
@Override
public void onStreamReady() {
log("XMPP STREAM READY");
}
/**
* Handles the HTTP GET
method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (request.getCharacterEncoding() == null) {
request.setCharacterEncoding("UTF-8");
}
int vuid = Utils.getHttpAuthUID(jdbc, request);
if (vuid == 0) {
vuid = Utils.getVisitorQueryStringUID(jdbc, request);
}
String uri = request.getRequestURI();
if (uri.equals("/home")) {
if (vuid > 0) {
messages.doGetHome(request, response, vuid);
} else {
response.sendError(401);
}
} else if (uri.equals("/messages")) {
messages.doGet(request, response, vuid);
} else if (uri.equals("/thread")) {
messages.doThreadGet(request, response, vuid);
} else if (uri.equals("/users")) {
users.doGetUsers(request, response, vuid);
} else if (uri.equals("/users/read")) {
users.doGetUserRead(request, response, vuid);
} else if (uri.equals("/users/readers")) {
users.doGetUserReaders(request, response, vuid);
} else if (uri.equals("/pm")) {
if (vuid > 0) {
pm.doGetPM(request, response, vuid);
} else {
response.sendError(401);
}
} else if (uri.equals("/groups_pms")) {
if (vuid > 0) {
others.doGetGroupsPMs(request, response, vuid);
} else {
response.sendError(401);
}
} else if (uri.equals("/messages/recommended")) {
if (vuid > 0) {
messages.doGetRecommended(request, response, vuid);
} else {
response.sendError(401);
}
} else if (uri.equals("/messages/set_popular") && vuid == 3694) {
messages.doSetPopular(request, response, xmpp);
} else if (uri.equals("/messages/set_privacy") && vuid > 0) {
messages.doSetPrivacy(request, response, xmpp, vuid);
} else {
response.sendError(404);
}
}
/**
* Handles the HTTP POST
method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (request.getCharacterEncoding() == null) {
request.setCharacterEncoding("UTF-8");
}
int vuid = Utils.getHttpAuthUID(jdbc, request);
if (vuid == 0) {
vuid = Utils.getVisitorQueryStringUID(jdbc, request);
}
if (vuid == 0) {
response.sendError(401);
return;
}
String uri = request.getRequestURI();
switch (uri) {
case "/post":
break;
case "/pm":
pm.doPostPM(request, response, xmpp, vuid);
break;
default:
response.sendError(405);
break;
}
}
public static void replyJSON(HttpServletRequest request, HttpServletResponse response, String json) throws IOException {
response.setContentType("application/json; charset=UTF-8");
response.setHeader("Access-Control-Allow-Origin", "*");
String callback = request.getParameter("callback");
if (callback != null && (callback.length() > 64 || !callback.matches("[a-zA-Z0-9\\-\\_]+"))) {
callback = null;
}
try (PrintWriter out = response.getWriter()) {
if (callback != null) {
out.print(callback + "(");
out.print(json);
out.print(")");
} else {
out.print(json);
}
}
}
}