package com.perforce.spark; import java.util.Map; import spark.Request; import spark.Response; import com.perforce.p4java.exception.P4JavaException; import com.perforce.p4java.server.IOptionsServer; public class ConnectionSession { static String TICKET = "ticket"; static String USER = "user"; static String PASS = "pass"; public static IOptionsServer get(Request request, Response response) throws P4JavaException { // use Perforce ticket from session String user = request.session().attribute(ConnectionSession.USER); String ticket = request.session().attribute(ConnectionSession.TICKET); try { IOptionsServer p4 = getP4(user, ticket); return p4; } catch (P4JavaException e) { // wipe out cookie and session logout(request, response); response.redirect("/"); throw e; } } private static IOptionsServer getP4(String user, String ticket) throws P4JavaException { String uri = ConnectionConfig.toUri(); IOptionsServer p4 = ConnectionFactory.getConnection(uri); p4.connect(); p4.setUserName(user); p4.setAuthTicket(ticket); // verify connection try { isLogin(p4); } catch (P4JavaException e) { throw e; } return p4; } public static Map<String, Object> login(Request request, Response response) { Map<String, Object> attributes; attributes = NavBar.attributes("cover", request); String uri = ConnectionConfig.toUri(); IOptionsServer p4 = ConnectionFactory.getConnection(uri); String user = request.queryParams(USER); String pass = request.queryParams(PASS); // fail if server is down. try { p4.connect(); } catch (Exception e) { String status = e.getMessage(); attributes.put("error_show", "inherit"); attributes.put("error_message", status); return attributes; } // login and use Perforce ticket for session p4.setUserName(user); // fetch ticket String ticket = null; try { p4.login(pass); } catch (Exception e) { String status = e.getMessage(); attributes.put("error_show", "inherit"); attributes.put("error_message", status); return attributes; } // check for null ticket ticket = p4.getAuthTicket(); if (ticket == null) { String status = "Ticket value is null."; attributes.put("error_show", "inherit"); attributes.put("error_message", status); return attributes; } // store ticket in session request.session().attribute(USER, user); request.session().attribute(TICKET, ticket); // save cookie String store = request.queryParams("store"); if (store != null) { response.cookie(USER, user); response.cookie(TICKET, ticket); } response.redirect("/"); return attributes; } public static Object logout(Request request, Response response) { request.session().removeAttribute(USER); request.session().removeAttribute(TICKET); response.removeCookie(USER); response.removeCookie(TICKET); return null; } public static void cookie(Request request, Response response) { String user = request.cookie(USER); String ticket = request.cookie(TICKET); try { getP4(user, ticket); } catch (P4JavaException e) { logout(request, response); return; } if (user != null) { // store ticket in session request.session().attribute(USER, user); request.session().attribute(TICKET, ticket); } } private static void isLogin(IOptionsServer p4) throws P4JavaException { String status = p4.getLoginStatus(); if (status.contains("not necessary")) { return; } if (status.contains("ticket expires in")) { return; } // If there is a broker or something else that swallows the message if (status.isEmpty()) { return; } throw new P4JavaException(status); } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#10 | 14228 | Paul Allen |
Moved View over to the new model. Retactor connection to package. |
||
#9 | 14181 | Paul Allen | Lots of updates and refactoring. | ||
#8 | 14017 | Paul Allen | Proxy caching to Perforce and SLF4J logging. | ||
#7 | 14013 | Paul Allen | Basic proxy | ||
#6 | 13869 | Paul Allen |
- Fix logout when testing cookie. - Refactor ftl->html - Add local bootstrap |
||
#5 | 13863 | Paul Allen | Minor fix to session vs cookie for login check. | ||
#4 | 13798 | Paul Allen | Basic file upload and login/logout cookie tidyup | ||
#3 | 13746 | Paul Allen | Basic File/Dir browsing and Cookie management. | ||
#2 | 13728 | Paul Allen | Error support | ||
#1 | 13722 | Paul Allen | Login UX |