aboutsummaryrefslogtreecommitdiff
path: root/src/org
diff options
context:
space:
mode:
Diffstat (limited to 'src/org')
-rw-r--r--src/org/traccar/ServerManager.java2
-rw-r--r--src/org/traccar/database/DataManager.java21
-rw-r--r--src/org/traccar/http/MainServlet.java67
-rw-r--r--src/org/traccar/http/WebServer.java15
4 files changed, 94 insertions, 11 deletions
diff --git a/src/org/traccar/ServerManager.java b/src/org/traccar/ServerManager.java
index abda51787..4d480749d 100644
--- a/src/org/traccar/ServerManager.java
+++ b/src/org/traccar/ServerManager.java
@@ -183,7 +183,7 @@ public class ServerManager {
// Initialize web server
if (Boolean.valueOf(properties.getProperty("http.enable"))) {
- webServer = new WebServer(properties, dataManager.getDataSource());
+ webServer = new WebServer(properties, dataManager);
}
}
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index 602345df9..2c78a53c4 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -267,4 +267,25 @@ public class DataManager {
}
}
+ public long login(String name, String password) throws SQLException {
+
+ Connection connection = dataSource.getConnection();
+ try {
+ PreparedStatement statement = connection.prepareStatement(
+ "SELECT id FROM users WHERE name = ? AND password = ?;");
+ try {
+ statement.setString(1, name);
+ statement.setString(2, password);
+
+ ResultSet result = statement.executeQuery();
+ result.next();
+ return result.getLong("id");
+ } finally {
+ statement.close();
+ }
+ } finally {
+ connection.close();
+ }
+ }
+
}
diff --git a/src/org/traccar/http/MainServlet.java b/src/org/traccar/http/MainServlet.java
new file mode 100644
index 000000000..ca5bcfc17
--- /dev/null
+++ b/src/org/traccar/http/MainServlet.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.traccar.http;
+
+import org.traccar.database.DataManager;
+
+import javax.servlet.ServletException;
+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.sql.SQLException;
+
+public class MainServlet extends HttpServlet {
+
+ private static final String USER_ID = "userId";
+
+ private DataManager dataManager;
+
+ public MainServlet(DataManager dataManager) {
+ this.dataManager = dataManager;
+ }
+
+ @Override
+ protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+
+ String command = req.getPathInfo();
+
+ if (command.equals("/login")) {
+ login(req, resp);
+ } else if (command.equals("/logout")) {
+ logout(req, resp);
+ } else {
+ resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
+ }
+ }
+
+ private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ try {
+ req.getSession().setAttribute(USER_ID,
+ dataManager.login(req.getParameter("name"), req.getParameter("password")));
+ resp.getWriter().println("{ success: true }");
+ } catch (SQLException e) {
+ throw new ServletException(e);
+ }
+ }
+
+ private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
+ req.getSession().removeAttribute(USER_ID);
+ resp.getWriter().println("{ success: true }");
+ }
+
+}
diff --git a/src/org/traccar/http/WebServer.java b/src/org/traccar/http/WebServer.java
index dac252be1..e90ddfe9b 100644
--- a/src/org/traccar/http/WebServer.java
+++ b/src/org/traccar/http/WebServer.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2012 - 2014 Anton Tananaev (anton.tananaev@gmail.com)
+ * Copyright 2012 - 2015 Anton Tananaev (anton.tananaev@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@ import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.webapp.WebAppContext;
+import org.traccar.database.DataManager;
import org.traccar.helper.Log;
/**
@@ -44,7 +45,7 @@ public class WebServer {
private Server server;
- public WebServer(Properties properties, DataSource dataSource) {
+ public WebServer(Properties properties, DataManager dataManager) {
String address = properties.getProperty("http.address");
Integer port = Integer.valueOf(properties.getProperty("http.port", "8082"));
if (address == null) {
@@ -57,13 +58,7 @@ public class WebServer {
ServletContextHandler servletHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
servletHandler.setContextPath("/api");
- servletHandler.addServlet(new ServletHolder(new HttpServlet() {
- @Override
- protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- PrintWriter out = resp.getWriter();
- out.println("<html><body>api</body></html>");
- }
- }), "/*");
+ servletHandler.addServlet(new ServletHolder(new MainServlet(dataManager)), "/*");
ResourceHandler resourceHandler = new ResourceHandler();
resourceHandler.setResourceBase(properties.getProperty("http.path"));
@@ -78,7 +73,7 @@ public class WebServer {
try {
Context context = new InitialContext();
- context.bind("java:/DefaultDS", dataSource);
+ context.bind("java:/DefaultDS", dataManager.getDataSource());
} catch (Exception error) {
Log.warning(error);
}