Last active
August 29, 2015 14:04
-
-
Save peel/8b123037572f3df8e034 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoanApplication { | |
private long applicationNo; | |
private long amount; | |
private String email; | |
private String contact; | |
private boolean approved; | |
public LoanApplication() { | |
applicationNo = LoanHandler.getNextId(); | |
} | |
public long getAmount() { | |
return amount; | |
} | |
public void setAmount(long amount) { | |
this.amount = amount; | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
public long getApplicationNo() { | |
return applicationNo; | |
} | |
public void setApplicationNo(long applicationNo) { | |
this.applicationNo = applicationNo; | |
} | |
public void approve() { | |
setApproved(true); | |
} | |
public boolean isApproved() { | |
return approved; | |
} | |
public void setApproved(boolean approved) { | |
this.approved = approved; | |
} | |
public void setContact(String who) { | |
this.contact = who; | |
} | |
public String getContact() { | |
return contact; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileFilter; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import org.eclipse.jetty.server.Request; | |
import org.eclipse.jetty.server.handler.AbstractHandler; | |
import com.google.gson.Gson; | |
public class LoanHandler extends AbstractHandler { | |
public static final String APPLICATION = "apply"; | |
public static final String FETCH = "fetch"; | |
public static final String TICKET_ID = "ticketId"; | |
public static final String APPROVE = "approve"; | |
@Override | |
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) | |
throws IOException, ServletException { | |
response.setContentType("application/json;charset=utf-8"); | |
response.setStatus(HttpServletResponse.SC_OK); | |
baseRequest.setHandled(true); | |
PrintWriter writer = response.getWriter(); | |
try { | |
if (isApplication(request)) { | |
LoanApplication application = new LoanApplication(); | |
application.setAmount(amountFrom(request)); | |
application.setContact(contactFrom(request)); | |
Ticket ticket = LoanRepository.store(application); | |
writer.println(new Gson().toJson(ticket)); | |
} else if (isStatusRequest(request) && idSpecified(request)) { | |
writer.println(fetchLoanInfo(request.getParameter(TICKET_ID))); | |
} else if (isApproval(request) && idSpecified(request)) { | |
writer.println(approveLoan(request.getParameter(TICKET_ID))); | |
} else { | |
writer.println("Incorrect parameters provided"); | |
} | |
} catch (ApplicationException e) { | |
writer.println("Uh oh! Problem occured: " + e.getMessage()); | |
} | |
} | |
private String contactFrom(HttpServletRequest request) { | |
return request.getParameter("contact"); | |
} | |
private long amountFrom(HttpServletRequest request) { | |
return Long.parseLong(request.getParameter("amount")); | |
} | |
private String approveLoan(String parameter) { | |
return new Gson().toJson(LoanRepository.approve(parameter)); | |
} | |
private boolean isApproval(HttpServletRequest request) { | |
return APPROVE.equals(request.getParameter("action")); | |
} | |
private boolean idSpecified(HttpServletRequest request) { | |
return request.getParameter(TICKET_ID) != null && validId(request) >= 0; | |
} | |
private long validId(HttpServletRequest request) { | |
String ticketId = request.getParameter(TICKET_ID); | |
try { | |
return Long.parseLong(ticketId); | |
} catch (NumberFormatException e) { | |
return -1L; | |
} | |
} | |
private boolean isStatusRequest(HttpServletRequest request) { | |
return FETCH.equals(request.getParameter("action")); | |
} | |
private boolean isApplication(HttpServletRequest request) { | |
return APPLICATION.equals(request.getParameter("action")); | |
} | |
private String fetchLoanInfo(String ticketId) { | |
LoanApplication formerApplication = LoanRepository.fetch(ticketId); | |
return new Gson().toJson(formerApplication); | |
} | |
public static long getNextId() { | |
File file = new File(LoanRepository.REPOSITORY_ROOT); | |
File[] files = file.listFiles(new FileFilter() { | |
@Override | |
public boolean accept(File pathname) { | |
return pathname.getName().endsWith(LoanRepository.FILE_EXTENSION); | |
} | |
}); | |
return files == null ? 0 : files.length + 1; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.Scanner; | |
import com.google.gson.Gson; | |
public class LoanRepository { | |
public final static String FILE_EXTENSION = ".loan"; | |
public final static String REPOSITORY_ROOT = System.getProperty("user.home") + "/loan"; | |
public static LoanApplication fetch(String ticketId) { | |
return fetch(Long.parseLong(ticketId)); | |
} | |
public static LoanApplication fetch(long ticketId) { | |
File file = fileFromApplication(ticketId); | |
try { | |
String output = new Scanner(file).useDelimiter("\\Z").next(); | |
return new Gson().fromJson(output, LoanApplication.class); | |
} catch (FileNotFoundException e) { | |
throw new ApplicationException("Ticket not found", e); | |
} | |
} | |
public static Ticket store(LoanApplication application) { | |
try { | |
new File(REPOSITORY_ROOT).mkdirs(); | |
FileOutputStream fileOutputStream = new FileOutputStream( | |
fileFromApplication(application.getApplicationNo())); | |
fileOutputStream.write(new Gson().toJson(application).getBytes()); | |
fileOutputStream.close(); | |
return new Ticket(application.getApplicationNo()); | |
} catch (FileNotFoundException e) { | |
throw new ApplicationException("Could not store application", e); | |
} catch (IOException e) { | |
throw new ApplicationException("Could not store application", e); | |
} | |
} | |
private static File fileFromApplication(long applicationNo) { | |
return new File(REPOSITORY_ROOT + "/" + applicationNo + FILE_EXTENSION); | |
} | |
public static Ticket approve(String ticketId) { | |
LoanApplication application = fetch(ticketId); | |
application.approve(); | |
store(application); | |
return new Ticket(application.getApplicationNo()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.eclipse.jetty.server.Server; | |
public class LoanServer { | |
public static void main(String[] args) throws Exception { | |
Server server = new Server(8080); | |
server.setHandler(new LoanHandler()); | |
server.start(); | |
server.join(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Ticket { | |
private long id; | |
public Ticket(long applicationNo) { | |
id = applicationNo; | |
} | |
public long getId() { | |
return id; | |
} | |
public void setId(long id) { | |
this.id = id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment