package com.perforce.jbrown.utils; import java.util.Map; /* * This class provides helper methods for checking the results from a server.execMapCmdList() call * and other helper method useful when using execMapCmdList. * * It steals code from P4Java 2015.2. Future P4Java versions may require updates to this classes * authorization failed error strings in array accessErrMsgs. */ import com.perforce.p4java.exception.AccessException; import com.perforce.p4java.exception.RequestException; import com.perforce.p4java.impl.mapbased.MapKeys; import com.perforce.p4java.impl.mapbased.rpc.func.helper.MapUnmapper; import com.perforce.p4java.server.IOptionsServer; /*** * The list of accessErrMsgs are stolen from * com.perforce.p4java.impl.mapbased.rpc.RpcServer.java and * com.p4java.impl.mapbased.server.Server.java. * * @author jbrown */ public class MapCmdUtils { public static boolean handleErrorStr(IOptionsServer server, Map<String, Object> map) throws RequestException, AccessException { String errStr = server.getErrorStr(map); if (errStr != null) { if (isAuthFail(errStr)) { throw new AccessException(errStr); } else { throw new RequestException(errStr, (String) map.get("code0")); } } return false; } public static boolean isAuthFail(String errStr) { if (errStr != null) { for (String str : accessErrMsgs) { if (errStr.contains(str)) { return true; } } } return false; } /** * Build a string that can be appended to the value of another line/word * field. This allows using execMapCmdList() to send not-yet-supported form * fields. * <p * Sample usage where the form has a required existing field "Email". * P4Java doesn't support the AuthMethod form field. so we replace the * Email value in the Map with a new value containing the extra field. * <pre><code> * inputMap.put("Email", map.get("Email") + buildField("AuthMethod","ldap",false)); * </code></pre> * * @param fieldName name of field * @param value field's value * @param isTextField * true if multi-line capable field (like a changelist's * description) * @return */ public static String buildField(String fieldName, String value, boolean isTextField) { StringBuilder hack = new StringBuilder(); hack.append(MapKeys.DOUBLE_LF); // terminate previous field's value. hack.append(fieldName); if (isTextField) { hack.append(MapKeys.COLON_LF); value = MapUnmapper.replaceNewlines(value); } else { hack.append(MapKeys.COLON_SPACE); } hack.append(value); return hack.toString(); } /** * Signals access (login) needed */ protected static final String CORE_AUTH_FAIL_STRING_1 = "Perforce password (P4PASSWD)"; /** * Signals access (login) needed */ protected static final String CORE_AUTH_FAIL_STRING_2 = "Access for user"; /** * Signals ticket has expired */ protected static final String CORE_AUTH_FAIL_STRING_3 = "Your session has expired"; /** * Signals ticket has expired */ protected static final String CORE_AUTH_FAIL_STRING_4 = "Your session was logged out"; private static final String AUTH_FAIL_STRING_1 = "Single sign-on on client failed"; // SSO // failure private static final String AUTH_FAIL_STRING_2 = "Password invalid"; private static final String[] accessErrMsgs = { CORE_AUTH_FAIL_STRING_1, CORE_AUTH_FAIL_STRING_2, CORE_AUTH_FAIL_STRING_3, CORE_AUTH_FAIL_STRING_4, AUTH_FAIL_STRING_1, AUTH_FAIL_STRING_2 }; }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 19914 | Joel Brown |
Example for updating a user's AuthMethod in p4java 2015.2. P4Java 2015.2 doesn't know squat about field AuthMethod. |
||
#1 | 19893 | Joel Brown | getting perforce Groups sample, including workaround for job087545 |