/*******************************************************************************
* Copyright (c) 2013, Perforce Software
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
******************************************************************************/
package com.perforce.p4java.extension.command;
import java.util.Map;
import com.perforce.p4java.impl.mapbased.rpc.msg.RpcMessage;
public class ServerMessages {
public static String getServerMessage(Map<String, Object> map,
int minimumCode) {
if (map == null)
return null;
int index = 0;
String code = (String) map.get(RpcMessage.CODE + index);
// Return if no code0 key found
if (code == null)
return null;
boolean foundCode = false;
StringBuilder codeString = new StringBuilder();
while (code != null) {
int severity = RpcMessage.getSeverity(code);
if (severity >= minimumCode) {
foundCode = true;
String fmtStr = (String) map.get(RpcMessage.FMT + index);
if (fmtStr != null) {
if (fmtStr.indexOf('%') != -1) {
fmtStr = RpcMessage.interpolateArgs(fmtStr, map);
}
// Insert latest message at beginning of error string
// since server structures them this way
codeString.insert(0, fmtStr);
codeString.insert(fmtStr.length(), '\n');
}
}
index++;
code = (String) map.get(RpcMessage.CODE + index);
}
// Only return a string if at least one severity code was found
if (foundCode) {
return codeString.toString();
}
return null;
}
public static boolean isAuthFail(String message) {
if (message == null)
return false;
for (String str : ACCESS_ERROR_MESSAGES) {
if (message.contains(str))
return true;
}
return false;
}
/**
* 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";
/**
* SSO Failure.
*/
protected static final String CORE_AUTH_FAIL_STRING_5 = "Single sign-on on client failed";
protected static final String[] ACCESS_ERROR_MESSAGES = {
CORE_AUTH_FAIL_STRING_1, CORE_AUTH_FAIL_STRING_2,
CORE_AUTH_FAIL_STRING_3, CORE_AUTH_FAIL_STRING_4,
CORE_AUTH_FAIL_STRING_5 };
}