Revision 1195

View differences:

org.gvsig.tools/library/trunk/org.gvsig.tools/org.gvsig.tools.lib/src/main/java/org/gvsig/tools/identitymanagement/UnauthorizedException.java
1

  
2
package org.gvsig.tools.identitymanagement;
3

  
4
import org.apache.commons.lang3.StringUtils;
5
import org.gvsig.tools.ToolsLocator;
6

  
7

  
8
public class UnauthorizedException extends RuntimeException {
9
    
10
    private SimpleIdentity identity;
11
    private String actionName;
12
    private Object resource;
13
    private String resourceName;
14
    
15
    public UnauthorizedException(SimpleIdentity identity, String actionName, Object resource, String resourceName) {
16
        this.actionName = actionName;
17
        this.identity = identity;
18
        this.resource = resource;
19
        this.resourceName = resourceName;
20
        if( this.identity == null ) {
21
            SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager();
22
            this.identity = identityManager.getCurrentIdentity();
23
        }
24
    }
25

  
26
    public UnauthorizedException(String actionName, Object resource, String resourceName) {
27
        this(null,actionName,resource,resourceName);
28
    }
29

  
30
    @Override
31
    public String getMessage() {
32
        StringBuilder builder = new StringBuilder();
33
        builder.append("User '");
34
        builder.append(this.identity.getID());
35
        builder.append("' is not authorized to '");
36
        builder.append(this.actionName);
37
        builder.append("'");
38
        if( !StringUtils.isBlank(this.resourceName) ) {
39
            builder.append(" to resource '");
40
            builder.append(this.resourceName);
41
            builder.append("'");
42
        }
43
        if( this.resource!=null ) {
44
            String s = null;
45
            try {
46
                s = resource.toString();
47
            } catch(Throwable th) {
48
                // Do nothing.
49
            }
50
            if( s != null ) {
51
                builder.append(" (");
52
                builder.append(s);
53
                builder.append(" )");
54
            }
55
        }
56
        return builder.toString();
57
    }
58
    
59
}

Also available in: Unified diff