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 @ 2590

History | View | Annotate | Download (2.7 KB)

1
package org.gvsig.tools.identitymanagement;
2

    
3
import org.apache.commons.lang3.StringUtils;
4
import org.gvsig.tools.ToolsLocator;
5
import org.gvsig.tools.identitymanagement.spi.AbstractSimpleIdentity;
6

    
7
public class UnauthorizedException extends RuntimeException {
8

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

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

    
25
    public UnauthorizedException(String identityId, String actionName, Object resource, String resourceName) {
26
        this(new AbstractSimpleIdentity(null) {
27
            @Override
28
            public String getID() {
29
                return identityId;
30
            }
31
        }, actionName, resource, resourceName);
32
    }
33

    
34
    public UnauthorizedException(String actionName, Object resource, String resourceName) {
35
        this((SimpleIdentity)null, actionName, resource, resourceName);
36
    }
37

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

    
67
    public SimpleIdentity getIdentity() {
68
        return identity;
69
    }
70

    
71
    public String getActionName() {
72
        return actionName;
73
    }
74

    
75
    public Object getResource() {
76
        return resource;
77
    }
78

    
79
    public String getResourceAsString() {
80
        String s = null;
81
        try {
82
            s = resource.toString();
83
        } catch (Throwable th) {
84
            // Do nothing.
85
        }
86
        return s;
87
    }
88

    
89
    public String getResourceName() {
90
        return resourceName;
91
    }
92

    
93
}