Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (30)
Showing
with 1056 additions and 0 deletions
*/out/
*/target/
*/.idea/workspace.xml
*/.idea/tasks.xml
*.iws
<component name="ArtifactManager">
<artifact type="war" name="IdentityService:war">
<output-path>$PROJECT_DIR$/target</output-path>
<root id="archive" name="IdentityService-1.0-SNAPSHOT.war">
<element id="artifact" artifact-name="IdentityService:war exploded" />
</root>
</artifact>
</component>
\ No newline at end of file
<component name="ArtifactManager">
<artifact type="exploded-war" name="IdentityService:war exploded">
<output-path>$PROJECT_DIR$/target/IdentityService-1.0-SNAPSHOT</output-path>
<root id="root">
<element id="directory" name="WEB-INF">
<element id="directory" name="classes">
<element id="module-output" name="IdentityService" />
</element>
<element id="directory" name="lib">
<element id="library" level="project" name="Maven: mysql:mysql-connector-java:5.1.6" />
<element id="library" level="project" name="Maven: com.google.code.gson:gson:2.8.2" />
</element>
</element>
<element id="directory" name="META-INF">
<element id="file-copy" path="$PROJECT_DIR$/target/IdentityService-1.0-SNAPSHOT/META-INF/MANIFEST.MF" />
</element>
<element id="javaee-facet-resources" facet="IdentityService/web/Web" />
</root>
</artifact>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="IdentityService" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="IdentityService" target="1.7" />
</bytecodeTargetLevel>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: com.google.code.gson:gson:2.8.2">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/com/google/code/gson/gson/2.8.2/gson-2.8.2.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/com/google/code/gson/gson/2.8.2/gson-2.8.2-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/com/google/code/gson/gson/2.8.2/gson-2.8.2-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: javax:javaee-web-api:7.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/javax/javaee-web-api/7.0/javaee-web-api-7.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/javax/javaee-web-api/7.0/javaee-web-api-7.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/javax/javaee-web-api/7.0/javaee-web-api-7.0-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<component name="libraryTable">
<library name="Maven: mysql:mysql-connector-java:5.1.6">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/mysql/mysql-connector-java/5.1.6/mysql-connector-java-5.1.6-sources.jar!/" />
</SOURCES>
</library>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" />
</component>
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/IdentityService.iml" filepath="$PROJECT_DIR$/IdentityService.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="SqlDialectMappings">
<file url="file://$PROJECT_DIR$/data/db_ojek_account.sql" dialect="MySQL" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="web" name="Web">
<configuration>
<descriptors>
<deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" />
</descriptors>
<webroots>
<root url="file://$MODULE_DIR$/src/main/webapp" relative="/" />
</webroots>
<sourceRoots>
<root url="file://$MODULE_DIR$/src/main/java" />
<root url="file://$MODULE_DIR$/src/main/resources" />
<root url="file://$MODULE_DIR$/target" />
</sourceRoots>
</configuration>
</facet>
</component>
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/target" isTestSource="false" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="PROVIDED" name="Tomcat 7.0.82" level="application_server_libraries" />
<orderEntry type="library" exported="" name="mysql:mysql-connector-java:6.0.6" level="application" />
<orderEntry type="library" exported="" name="com.google.code.gson:gson:2.8.2" level="application" />
<orderEntry type="library" scope="PROVIDED" name="Maven: javax:javaee-web-api:7.0" level="project" />
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.6" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.2" level="project" />
</component>
</module>
\ No newline at end of file
-- phpMyAdmin SQL Dump
-- version 4.5.4.1deb2ubuntu2
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: 04 Nov 2017 pada 09.26
-- Versi Server: 10.0.31-MariaDB-0ubuntu0.16.04.2
-- PHP Version: 7.0.22-0ubuntu0.16.04.1
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `db_ojek_account`
--
-- --------------------------------------------------------
--
-- Struktur dari tabel `account`
--
CREATE TABLE `account` (
`id` int(15) NOT NULL,
`name` varchar(50) NOT NULL,
`username` varchar(20) NOT NULL,
`email` varchar(40) NOT NULL,
`password` varchar(100) NOT NULL,
`phone` varchar(15) NOT NULL,
`photo` varchar(100) NOT NULL,
`is_driver` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data untuk tabel `account`
--
INSERT INTO `account` (`id`, `name`, `username`, `email`, `password`, `phone`, `photo`, `is_driver`) VALUES
(1, 'Fadhil Imam', 'fadhilimamk', '13515146@std.stei.itb.ac.id', 'ac43724f16e9241d990427ab7c8f4228', '085797490039', '/img/profile/VGdWZUZ2dlJlUkM5eWpjVDcyQXJoZz09', 1),
(2, 'Joko Susilo', 'jokosusilo', 'rahasia@gmail.com', 'ac43724f16e9241d990427ab7c8f4228', '0247624517', '/img/profile/cXNNb1RtOXg0ckxtUVpoZGpiK3NTZz09', 1),
(3, 'Abdul Majid', 'abdulmajid', 'abdul@gmail.com', 'jhasdTUAShasdbasdjask', '085741627404', '/img/profile/R2dXM01NcDVRV0lrVXdGTE5nbDZDdz09', 1),
(4, 'Abdullah Malik Ibrahim', 'malikibrahim', 'malik@gmail.com', 'ac43724f16e9241d990427ab7c8f4228', '0247657917', '/img/profile/UFBqemxTWmtkZytjSmNSVWE4S2lTQT09', 1),
(5, 'Joko Akbar', 'joko', 'jokoakbar@gmail.com', 'ac43724f16e9241d990427ab7c8f4228', '085797490039', '/img/empty_profile.png', 0),
(6, 'Lifardi Muhammad', 'lifardi', 'lifardi@gmail.com', 'ac43724f16e9241d990427ab7c8f4228', '085797490039', '/img/empty_profile.png', 1),
(7, 'Coba 3', 'coba', 'coba@gmail.com', 'ac43724f16e9241d990427ab7c8f4228', '085797490039', '/img/empty_profile.png', 0),
(8, 'Fadhil Imam', 'fadhil', 'fadhil@itb.ac.id', 'ac43724f16e9241d990427ab7c8f4228', '0872318283192', '/img/empty_profile.png', 0);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `account`
--
ALTER TABLE `account`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `username` (`username`,`email`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `account`
--
ALTER TABLE `account`
MODIFY `id` int(15) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.informatika.ojek</groupId>
<artifactId>IdentityService</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>IdentityService</name>
<properties>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<compilerArguments>
<endorseddirs>${endorsed.dir}</endorseddirs>
</compilerArguments>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.3</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.6</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<outputDirectory>${endorsed.dir}</outputDirectory>
<silent>true</silent>
<artifactItems>
<artifactItem>
<groupId>javax</groupId>
<artifactId>javaee-endorsed-api</artifactId>
<version>7.0</version>
<type>jar</type>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package org.informatika.ojek;
import com.google.gson.Gson;
import org.informatika.ojek.model.Account;
import org.informatika.ojek.model.Response;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author fadhilimamk
*/
@WebServlet(name = "Auth", urlPatterns = {"/auth"})
public class Auth extends HttpServlet {
private DAO DAO;
private Gson gson;
public void init() {
String jdbcURL = getServletContext().getInitParameter("jdbcURL");
String jdbcUsername = getServletContext().getInitParameter("jdbcUsername");
String jdbcPassword = getServletContext().getInitParameter("jdbcPassword");
DAO = new DAO(jdbcURL, jdbcUsername, jdbcPassword);
gson = new Gson();
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.setStatus(500);
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
Response data;
// Check username and password in post body
if (username == null || password == null) {
data = new Response(Response.STATUS_ERROR, null, null, "Wrong parameter!");
out.print(gson.toJson(data));
return;
}
Account account = DAO.checkAvaibility(username, password);
// Return error if user not exist
if (account == null) {
data = new Response(Response.STATUS_ERROR, null, null, "Wrong username or password!");
out.print(gson.toJson(data));
return;
}
// Return old token if user already login before
String token = DAO.checkUserAlreadyLogin(account.getId());
if (token != null) {
data = new Response(Response.STATUS_SUCCESS, token, null, "User already login before!");
out.print(gson.toJson(data));
return;
}
// Generate new token
try {
token = account.generateToken(password);
DAO.createToken(account.getId(), token);
} catch (Exception e) {
data = new Response(Response.STATUS_ERROR, null, null, e.getMessage());
out.print( gson.toJson(data));
return;
}
response.setStatus(200);
data = new Response(Response.STATUS_SUCCESS, token, null, null);
out.print(gson.toJson(data));
}
}
package org.informatika.ojek;
import org.informatika.ojek.model.Account;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;
/**
* Class DAO merupakan Data Access Object yang digunakan untuk mengakses
* basis data pada identity service
*
* @author fadhilimamk
*/
public class DAO {
private static int EXPIRE_TIME = 120; // in minutes
private String jdbcURL;
private String jdbcUsername;
private String jdbcPassword;
private Connection jdbcConnection;
public DAO(String jdbcURL, String jdbcUsername, String jdbcPassword) {
this.jdbcURL = jdbcURL;
this.jdbcUsername = jdbcUsername;
this.jdbcPassword = jdbcPassword;
}
private void connect() throws SQLException {
if (jdbcConnection == null || jdbcConnection.isClosed()) {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
throw new SQLException(e);
}
jdbcConnection = DriverManager.getConnection(
jdbcURL, jdbcUsername, jdbcPassword);
}
}
private void disconnect() throws SQLException {
if (jdbcConnection != null && !jdbcConnection.isClosed()) {
jdbcConnection.close();
}
}
/**
* Method untuk mendapatkan semua akun dalam basis dataidentity service
*/
public List<Account> listAllAccount() throws SQLException {
List<Account> listAccount = new ArrayList<>();
String sql = "SELECT * FROM account";
connect();
Statement statement = jdbcConnection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String username = resultSet.getString("username");
String email = resultSet.getString("email");
String phone = resultSet.getString("phone");
String photo = resultSet.getString("photo");
Boolean isDriver = resultSet.getBoolean("is_driver");
Account account = new Account(id, name, username, email, phone, photo, isDriver);
listAccount.add(account);
}
resultSet.close();
statement.close();
disconnect();
return listAccount;
}
/**
* Method checkAvaibility mengembalikan id user dengan username dan password
* yang diberikan. Method ini akan mengembalikan -1 jika user tidak ditemukan
*/
public int checkAvailability(String username, String password) {
String sql = "SELECT id FROM account WHERE username = ? AND password = MD5(?)";
try {
connect();
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, username);
statement.setString(2, password);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
int id = resultSet.getInt("id");
resultSet.close();
statement.close();
disconnect();
return id;
}
resultSet.close();
statement.close();
disconnect();
} catch (SQLException e) {
e.printStackTrace();
}
return -1;
}
public Account checkAvaibility(String username, String password) {
String sql = "SELECT * FROM account WHERE username = ? AND password = MD5(?)";
try {
connect();
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, username);
statement.setString(2, password);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String email = resultSet.getString("email");
String phone = resultSet.getString("phone");
String photo = resultSet.getString("photo");
Boolean isDriver = resultSet.getBoolean("is_driver");
Account account = new Account(id, name, username, email, phone, photo, isDriver);
resultSet.close();
statement.close();
disconnect();
return account;
}
resultSet.close();
statement.close();
disconnect();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* Method checkUserAlreadyLogin berfungsi untuk memeriksa apakah user sudah
* login dan memiliki token. Jika user sudah login maka akan dikembalikan tokennya,
* namun jika belum maka akan dikembalikan null.
*/
public String checkUserAlreadyLogin(int idUser) {
String sql = "SELECT * FROM token WHERE id_account = ?";
PreparedStatement statement;
try {
connect();
statement = jdbcConnection.prepareStatement(sql);
statement.setInt(1, idUser);
ResultSet resultSet = statement.executeQuery();
resultSet.getFetchSize();
if (resultSet.next()) {
String token = resultSet.getString("token");
Timestamp timeout = resultSet.getTimestamp("expire_at");
Date now = new Date();
if (timeout.getTime() > now.getTime()) {
statement.close();
resultSet.close();
disconnect();
return token;
}
// Token is expired
logoutUser(idUser);
return null;
}
statement.close();
resultSet.close();
disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* Method createToken digunakan untuk menyimpan token milik user yang menandakan bahwa
* user tersebut telah login. Waktu expire otomatis diberikan 5 menit.
*/
public void createToken(int idUser, String token) throws SQLException {
String sql = "INSERT INTO token (token, expire_at, id_account) VALUES (?, ?, ?)";
// 5 minute expire
Timestamp expire = new Timestamp(System.currentTimeMillis()+EXPIRE_TIME*60*1000);
connect();
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, token);
statement.setTimestamp(2, expire);
statement.setInt(3, idUser);
int rowCount = statement.executeUpdate();
statement.close();
disconnect();
}
public void updateTokenTimeout(int idUser) throws SQLException {
String sql = "UPDATE token SET expire_at = ? WHERE id_account = ?";
// 5 minute expire
Timestamp expire = new Timestamp(System.currentTimeMillis()+EXPIRE_TIME*60*1000);
connect();
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setTimestamp(1, expire);
statement.setInt(2, idUser);
int rowCount = statement.executeUpdate();
statement.close();
disconnect();
}
/**
* Method validateToken mengecek user pemilik suatu token, akan mengembalikan -1
* jika token tidak ditemukan dalam basis data token.
*/
public int validateToken(String token) {
String sql = "SELECT * FROM token WHERE token = ?";
PreparedStatement statement;
try {
connect();
statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, token);
ResultSet resultSet = statement.executeQuery();
resultSet.getFetchSize();
if (resultSet.next()) {
int userId = resultSet.getInt("id_account");
Timestamp timeout = resultSet.getTimestamp("expire_at");
Date now = new Date();
if (timeout.getTime() > now.getTime()) {
statement.close();
resultSet.close();
disconnect();
return userId;
}
// Token is expired
logoutUser(userId);
return -1;
}
statement.close();
resultSet.close();
disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return -1;
}
public Account userInfo(int userId){
String sql = "SELECT * FROM account WHERE id = ?";
try {
connect();
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setInt(1, userId);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
String username = resultSet.getString("username");
String email = resultSet.getString("email");
String phone = resultSet.getString("phone");
String photo = resultSet.getString("photo");
Boolean isDriver = resultSet.getBoolean("is_driver");
statement.close();
resultSet.close();
disconnect();
return new Account(id, name, username, email, phone, photo, isDriver);
}
statement.close();
resultSet.close();
disconnect();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* Method logoutUser akan menghapus data token user dengan id = idUser dari basis
* data, user yang tidak memiliki token adalah user yang tidak login
*/
public void logoutUser(int idUser) throws SQLException {
String sql = "DELETE FROM token WHERE id_account = ?";
connect();
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setInt(1, idUser);
int rowCount = statement.executeUpdate();
statement.close();
disconnect();
if (rowCount < 1) {
throw new SQLException("No row effected!");
}
}
public void updateUser(int idUser, String name, String phone, Boolean driverStatus) throws SQLException {
String sql = "UPDATE account SET name = ?, phone = ?, is_driver = ? WHERE id = ?";
connect();
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, name);
statement.setString(2, phone);
statement.setInt(3, driverStatus ? 1 : 0);
statement.setInt(4, idUser);
int rowCount = statement.executeUpdate();
statement.close();
disconnect();
if (rowCount < 1) {
throw new SQLException("No row effected!");
}
}
public void registerUser(Account account, String password) throws SQLException {
String sql = "INSERT INTO account (`name`, username, email, password, phone, is_driver) " +
"VALUES (?,?,?,MD5(?),?,0)";
connect();
PreparedStatement statement = jdbcConnection.prepareStatement(sql);
statement.setString(1, account.getName());
statement.setString(2, account.getUsername());
statement.setString(3, account.getEmail());
statement.setString(4, password);
statement.setString(5, account.getPhone());
int rowCount = statement.executeUpdate();
statement.close();
disconnect();
if (rowCount < 1) {
throw new SQLException("No row effected!");
}
disconnect();
}
}
package org.informatika.ojek;
import com.google.gson.Gson;
import org.informatika.ojek.model.Response;
import javax.servlet.ServletException;
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.sql.SQLException;
@WebServlet(name = "Edit")
public class Edit extends HttpServlet {
private DAO DAO;
private Gson gson;
public void init() {
String jdbcURL = getServletContext().getInitParameter("jdbcURL");
String jdbcUsername = getServletContext().getInitParameter("jdbcUsername");
String jdbcPassword = getServletContext().getInitParameter("jdbcPassword");
DAO = new DAO(jdbcURL, jdbcUsername, jdbcPassword);
gson = new Gson();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
Response data;
response.setStatus(500);
String token = request.getParameter("token");
String name = request.getParameter("name");
String phone = request.getParameter("phone");
String driverStatus = request.getParameter("driver_status");
// Check required parameters
if (token == null || name == null || phone == null || driverStatus == null) {
data = new Response(Response.STATUS_ERROR, null, null, "Wrong parameter!");
out.print(gson.toJson(data));
return;
}
// Check if token valid
int userId = DAO.validateToken(token);
if (userId == -1) {
data = new Response(Response.STATUS_ERROR, null, null, "Token is expired!");
out.print(gson.toJson(data));
return;
}
// Token is valid, change data
try {
DAO.updateUser(userId, name, phone, driverStatus.equals("true"));
} catch (SQLException e) {
e.printStackTrace();
data = new Response(Response.STATUS_ERROR, null, null, e.getMessage());
out.print(gson.toJson(data));
return;
}
response.setStatus(200);
data = new Response(Response.STATUS_SUCCESS, null, null, "User updated!");
out.print(gson.toJson(data));
}
}
package org.informatika.ojek;
import com.google.gson.Gson;
import org.informatika.ojek.model.Response;
import javax.servlet.ServletException;
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.sql.SQLException;
@WebServlet(name = "Logout", urlPatterns = "/logout")
public class Logout extends HttpServlet {
private DAO DAO;
private Gson gson;
public void init() {
String jdbcURL = getServletContext().getInitParameter("jdbcURL");
String jdbcUsername = getServletContext().getInitParameter("jdbcUsername");
String jdbcPassword = getServletContext().getInitParameter("jdbcPassword");
DAO = new DAO(jdbcURL, jdbcUsername, jdbcPassword);
gson = new Gson();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.setStatus(500);
PrintWriter out = response.getWriter();
Response data;
String token = request.getParameter("token");
if (token == null) {
data = new Response(Response.STATUS_ERROR, null, null, "Wrong parameter!");
out.print(gson.toJson(data));
return;
}
int userId = DAO.validateToken(token);
if (userId == -1) {
data = new Response(Response.STATUS_ERROR, null, null, "User already logout!");
out.println(gson.toJson(data));
return;
}
try {
DAO.logoutUser(userId);
} catch (SQLException e) {
e.printStackTrace();
data = new Response(Response.STATUS_ERROR, null, null, e.getMessage());
out.println(gson.toJson(data));
return;
}
response.setStatus(200);
data = new Response(Response.STATUS_ERROR, null, null, "Logout successfull");
out.println(gson.toJson(data));
}
}