Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / identitymanagement / UnauthorizedException.java @ 1199

History | View | Annotate | Download (2.3 KB)

1
package org.gvsig.tools.identitymanagement;
2

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

    
6
public class UnauthorizedException extends RuntimeException {
7

    
8
    private SimpleIdentity identity;
9
    private String actionName;
10
    private Object resource;
11
    private String resourceName;
12

    
13
    public UnauthorizedException(SimpleIdentity identity, String actionName, Object resource, String resourceName) {
14
        this.actionName = actionName;
15
        this.identity = identity;
16
        this.resource = resource;
17
        this.resourceName = resourceName;
18
        if (this.identity == null) {
19
            SimpleIdentityManager identityManager = ToolsLocator.getIdentityManager();
20
            this.identity = identityManager.getCurrentIdentity();
21
        }
22
    }
23

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

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

    
57
    public SimpleIdentity getIdentity() {
58
        return identity;
59
    }
60

    
61
    public String getActionName() {
62
        return actionName;
63
    }
64

    
65
    public Object getResource() {
66
        return resource;
67
    }
68

    
69
    public String getResourceAsString() {
70
        String s = null;
71
        try {
72
            s = resource.toString();
73
        } catch (Throwable th) {
74
            // Do nothing.
75
        }
76
        return s;
77
    }
78

    
79
    public String getResourceName() {
80
        return resourceName;
81
    }
82

    
83
}