1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
/*
* 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 <http://www.gnu.org/licenses/>.
*/
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.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Properties;
/**
*
* @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 {
Properties conf = new Properties();
conf.load(new FileInputStream("/etc/juick/api.conf"));
final String driverClassName = "com.mysql.jdbc.Driver";
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setUrl("jdbc:mysql://localhost/juick?autoReconnect=true&user=" + conf.getProperty("mysql_username", "") + "&password=" + conf.getProperty("mysql_password", ""));
dataSource.setDriverClassName(driverClassName);
jdbc = new JdbcTemplate(dataSource);
messages = new Messages(jdbc);
users = new Users(jdbc);
pm = new PM(jdbc);
others = new Others(jdbc);
setupXmppComponent(conf.getProperty("xmpp_password"));
} catch (IOException e) {
log("API initialization error", e);
}
}
public void setupXmppComponent(final String password) {
Thread thr = new Thread(() -> {
try {
Socket socket = new Socket("localhost", 5347);
xmpp = new StreamComponent(new JID("", "api.juick.com", ""), socket.getInputStream(), socket.getOutputStream(), password);
xmpp.addListener(Main.this);
xmpp.startParsing();
} catch (IOException e) {
log("XMPP exception", e);
}
});
thr.start();
}
@Override
public void onStreamFail(String msg) {
log("XMPP failed: " + msg);
}
@Override
public void onStreamReady() {
System.err.println("XMPP STREAM READY");
}
/**
* Handles the HTTP <code>GET</code> 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("/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 <code>POST</code> 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);
}
}
}
}
|