postLoad()   F
last analyzed

Complexity

Conditions 11

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
cc 11
rs 3.1764

1 Method

Rating   Name   Duplication   Size   Complexity  
newInstance 0 ? ?

How to fix   Complexity   

Complexity

Complex classes like org.apereo.cas.services.AbstractRegisteredService.postLoad() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
package org.apereo.cas.services;
2
3
import com.fasterxml.jackson.annotation.JsonTypeInfo;
4
import org.apache.commons.lang3.StringUtils;
5
import org.apache.commons.lang3.builder.CompareToBuilder;
6
import org.apache.commons.lang3.builder.EqualsBuilder;
7
import org.apache.commons.lang3.builder.HashCodeBuilder;
8
import org.apache.commons.lang3.builder.ToStringBuilder;
9
import org.apache.commons.lang3.builder.ToStringStyle;
10
import org.hibernate.annotations.GenericGenerator;
11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13
14
import javax.persistence.CascadeType;
15
import javax.persistence.Column;
16
import javax.persistence.DiscriminatorColumn;
17
import javax.persistence.DiscriminatorType;
18
import javax.persistence.Entity;
19
import javax.persistence.FetchType;
20
import javax.persistence.GeneratedValue;
21
import javax.persistence.GenerationType;
22
import javax.persistence.Id;
23
import javax.persistence.Inheritance;
24
import javax.persistence.JoinTable;
25
import javax.persistence.Lob;
26
import javax.persistence.OneToMany;
27
import javax.persistence.OrderColumn;
28
import javax.persistence.PostLoad;
29
import javax.persistence.Table;
30
import java.net.URL;
31
import java.util.ArrayList;
32
import java.util.HashMap;
33
import java.util.HashSet;
34
import java.util.LinkedHashMap;
35
import java.util.List;
36
import java.util.Map;
37
import java.util.Set;
38
39
/**
40
 * Base class for mutable, persistable registered services.
41
 *
42
 * @author Marvin S. Addison
43
 * @author Scott Battaglia
44
 * @author Misagh Moayyed
45
 * @since 3.0.0
46
 */
47
@Entity
48
@Inheritance
49
@DiscriminatorColumn(name = "expression_type", length = 50, discriminatorType = DiscriminatorType.STRING,
50
        columnDefinition = "VARCHAR(50) DEFAULT 'regex'")
51
@Table(name = "RegexRegisteredService")
52
@JsonTypeInfo(use = JsonTypeInfo.Id.CLASS, include = JsonTypeInfo.As.PROPERTY)
53
public abstract class AbstractRegisteredService implements RegisteredService {
54
    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractRegisteredService.class);
55
56
    private static final long serialVersionUID = 7645279151115635245L;
57
58
    /**
59
     * The unique identifier for this service.
60
     */
61
    @Column(length = 255, updatable = true, insertable = true, nullable = false)
62
    protected String serviceId;
63
64
    @Column(length = 255, updatable = true, insertable = true, nullable = false)
65
    private String name;
66
67
    @Column(length = 255, updatable = true, insertable = true, nullable = true)
68
    private String theme;
69
70
    @Column(length = 255, updatable = true, insertable = true, nullable = true)
71
    private String informationUrl;
72
73
    @Column(length = 255, updatable = true, insertable = true, nullable = true)
74
    private String privacyUrl;
75
76
    @org.springframework.data.annotation.Id
77
    @Id
78
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "native")
79
    @GenericGenerator(name = "native", strategy = "native")
80
    private long id = RegisteredService.INITIAL_IDENTIFIER_VALUE;
81
82
    @Column(length = 255, updatable = true, insertable = true, nullable = true)
83
    private String description;
84
85
    @Lob
86
    @Column(name = "expiration_policy", nullable = true, length = Integer.MAX_VALUE)
87
    private RegisteredServiceExpirationPolicy expirationPolicy = new DefaultRegisteredServiceExpirationPolicy();
88
89
    @Lob
90
    @Column(name = "proxy_policy", nullable = true, length = Integer.MAX_VALUE)
91
    private RegisteredServiceProxyPolicy proxyPolicy = new RefuseRegisteredServiceProxyPolicy();
92
93
    @Column(name = "evaluation_order", nullable = false)
94
    private int evaluationOrder;
95
96
    @Lob
97
    @Column(name = "username_attr", nullable = true, length = Integer.MAX_VALUE)
98
    private RegisteredServiceUsernameAttributeProvider usernameAttributeProvider = new DefaultRegisteredServiceUsernameProvider();
99
100
    @Column(name = "logout_type", nullable = true)
101
    private LogoutType logoutType = LogoutType.BACK_CHANNEL;
102
103
    @Lob
104
    @Column(name = "required_handlers", length = Integer.MAX_VALUE)
105
    private HashSet<String> requiredHandlers = new HashSet<>();
106
107
    @Lob
108
    @Column(name = "attribute_release", nullable = true, length = Integer.MAX_VALUE)
109
    private RegisteredServiceAttributeReleasePolicy attributeReleasePolicy = new ReturnAllowedAttributeReleasePolicy();
110
111
    @Lob
112
    @Column(name = "mfa_policy", nullable = true, length = Integer.MAX_VALUE)
113
    private RegisteredServiceMultifactorPolicy multifactorPolicy = new DefaultRegisteredServiceMultifactorPolicy();
114
115
    @Column(length = 255, updatable = true, insertable = true, nullable = true)
116
    private String logo;
117
118
    @Column(name = "logout_url")
119
    private URL logoutUrl;
120
121
    @Lob
122
    @Column(name = "access_strategy", nullable = true, length = Integer.MAX_VALUE)
123
    private RegisteredServiceAccessStrategy accessStrategy = new DefaultRegisteredServiceAccessStrategy();
124
125
    @Lob
126
    @Column(name = "public_key", nullable = true, length = Integer.MAX_VALUE)
127
    private RegisteredServicePublicKey publicKey;
128
129
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
130
    @JoinTable(name = "RegisteredServiceImpl_Props")
131
    private Map<String, DefaultRegisteredServiceProperty> properties = new HashMap<>();
132
133
    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
134
    @JoinTable(name = "RegisteredService_Contacts")
135
    @OrderColumn
136
    private List<DefaultRegisteredServiceContact> contacts = new ArrayList<>();
137
138
139
    @Override
140
    public long getId() {
141
        return this.id;
142
    }
143
144
    @Override
145
    public String getInformationUrl() {
146
        return this.informationUrl;
147
    }
148
149
    @Override
150
    public String getPrivacyUrl() {
151
        return this.privacyUrl;
152
    }
153
154
    @Override
155
    public String getDescription() {
156
        return this.description;
157
    }
158
159
    @Override
160
    public String getServiceId() {
161
        return this.serviceId;
162
    }
163
164
    @Override
165
    public String getName() {
166
        return this.name;
167
    }
168
169
    @Override
170
    public String getTheme() {
171
        return this.theme;
172
    }
173
174
    @Override
175
    public RegisteredServiceProxyPolicy getProxyPolicy() {
176
        return this.proxyPolicy;
177
    }
178
179
    @Override
180
    public RegisteredServiceAccessStrategy getAccessStrategy() {
181
        return this.accessStrategy;
182
    }
183
184
    @Override
185
    public URL getLogoutUrl() {
186
        return this.logoutUrl;
187
    }
188
189
    /**
190
     * Initializes the registered service with default values
191
     * for fields that are unspecified. Only triggered by JPA.
192
     *
193
     * @since 4.1
194
     */
195
    @PostLoad
196
    public void postLoad() {
197
        if (this.proxyPolicy == null) {
198
            this.proxyPolicy = new RefuseRegisteredServiceProxyPolicy();
199
        }
200
        if (this.usernameAttributeProvider == null) {
201
            this.usernameAttributeProvider = new DefaultRegisteredServiceUsernameProvider();
202
        }
203
        if (this.logoutType == null) {
204
            this.logoutType = LogoutType.BACK_CHANNEL;
205
        }
206
        if (this.requiredHandlers == null) {
207
            this.requiredHandlers = new HashSet<>();
208
        }
209
        if (this.accessStrategy == null) {
210
            this.accessStrategy = new DefaultRegisteredServiceAccessStrategy();
211
        }
212
        if (this.multifactorPolicy == null) {
213
            this.multifactorPolicy = new DefaultRegisteredServiceMultifactorPolicy();
214
        }
215
        if (this.properties == null) {
216
            this.properties = new LinkedHashMap<>();
217
        }
218
        if (this.attributeReleasePolicy == null) {
219
            this.attributeReleasePolicy = new ReturnAllowedAttributeReleasePolicy();
220
        }
221
        if (this.contacts == null) {
222
            this.contacts = new ArrayList<>();
223
        }
224
        if (this.expirationPolicy == null) {
225
            this.expirationPolicy = new DefaultRegisteredServiceExpirationPolicy();
226
        }
227
    }
228
229
    @Override
230
    public boolean equals(final Object o) {
231
        if (o == null) {
232
            return false;
233
        }
234
235
        if (this == o) {
236
            return true;
237
        }
238
239
        if (!(o instanceof AbstractRegisteredService)) {
240
            return false;
241
        }
242
243
        final AbstractRegisteredService that = (AbstractRegisteredService) o;
244
245
        final EqualsBuilder builder = new EqualsBuilder();
246
        return builder
247
                .append(this.proxyPolicy, that.proxyPolicy)
248
                .append(this.evaluationOrder, that.evaluationOrder)
249
                .append(this.description, that.description)
250
                .append(this.name, that.name)
251
                .append(this.serviceId, that.serviceId)
252
                .append(this.theme, that.theme)
253
                .append(this.usernameAttributeProvider, that.usernameAttributeProvider)
254
                .append(this.logoutType, that.logoutType)
255
                .append(this.attributeReleasePolicy, that.attributeReleasePolicy)
256
                .append(this.accessStrategy, that.accessStrategy)
257
                .append(this.logo, that.logo)
258
                .append(this.publicKey, that.publicKey)
259
                .append(this.logoutUrl, that.logoutUrl)
260
                .append(this.requiredHandlers, that.requiredHandlers)
261
                .append(this.proxyPolicy, that.proxyPolicy)
262
                .append(this.properties, that.properties)
263
                .append(this.multifactorPolicy, that.multifactorPolicy)
264
                .append(this.informationUrl, that.informationUrl)
265
                .append(this.privacyUrl, that.privacyUrl)
266
                .append(this.contacts, that.contacts)
267
                .append(this.expirationPolicy, that.expirationPolicy)
268
                .isEquals();
269
    }
270
271
    @Override
272
    public int hashCode() {
273
        return new HashCodeBuilder(7, 31)
274
                .append(this.description)
275
                .append(this.serviceId)
276
                .append(this.name)
277
                .append(this.theme)
278
                .append(this.evaluationOrder)
279
                .append(this.usernameAttributeProvider)
280
                .append(this.accessStrategy)
281
                .append(this.logoutType)
282
                .append(this.attributeReleasePolicy)
283
                .append(this.accessStrategy)
284
                .append(this.logo)
285
                .append(this.publicKey)
286
                .append(this.logoutUrl)
287
                .append(this.requiredHandlers)
288
                .append(this.proxyPolicy)
289
                .append(this.properties)
290
                .append(this.multifactorPolicy)
291
                .append(this.informationUrl)
292
                .append(this.privacyUrl)
293
                .append(this.contacts)
294
                .append(this.expirationPolicy)
295
                .toHashCode();
296
    }
297
298
    public void setProxyPolicy(final RegisteredServiceProxyPolicy policy) {
299
        this.proxyPolicy = policy;
300
    }
301
302
    public void setDescription(final String description) {
303
        this.description = description;
304
    }
305
306
    /**
307
     * Sets the service identifier. Extensions are to define the format.
308
     *
309
     * @param id the new service id
310
     */
311
    public abstract void setServiceId(String id);
312
313
    public void setId(final long id) {
314
        this.id = id;
315
    }
316
317
    public void setName(final String name) {
318
        this.name = name;
319
    }
320
321
    public void setTheme(final String theme) {
322
        this.theme = theme;
323
    }
324
325
    @Override
326
    public void setEvaluationOrder(final int evaluationOrder) {
327
        this.evaluationOrder = evaluationOrder;
328
    }
329
330
    @Override
331
    public int getEvaluationOrder() {
332
        return this.evaluationOrder;
333
    }
334
335
    @Override
336
    public RegisteredServiceUsernameAttributeProvider getUsernameAttributeProvider() {
337
        return this.usernameAttributeProvider;
338
    }
339
340
    public void setAccessStrategy(final RegisteredServiceAccessStrategy accessStrategy) {
341
        this.accessStrategy = accessStrategy;
342
    }
343
344
    public void setLogoutUrl(final URL logoutUrl) {
345
        this.logoutUrl = logoutUrl;
346
    }
347
348
    public void setInformationUrl(final String informationUrl) {
349
        this.informationUrl = informationUrl;
350
    }
351
352
    public void setPrivacyUrl(final String privacyUrl) {
353
        this.privacyUrl = privacyUrl;
354
    }
355
356
    /**
357
     * Sets the user attribute provider instance
358
     * when providing usernames to this registered service.
359
     *
360
     * @param usernameProvider the new username attribute
361
     */
362
    public void setUsernameAttributeProvider(final RegisteredServiceUsernameAttributeProvider usernameProvider) {
363
        this.usernameAttributeProvider = usernameProvider;
364
    }
365
366
    @Override
367
    public LogoutType getLogoutType() {
368
        return this.logoutType;
369
    }
370
371
    /**
372
     * Set the logout type of the service.
373
     *
374
     * @param logoutType the logout type of the service.
375
     */
376
    public void setLogoutType(final LogoutType logoutType) {
377
        this.logoutType = logoutType;
378
    }
379
380
    @Override
381
    public AbstractRegisteredService clone() {
382
        final AbstractRegisteredService clone = newInstance();
383
        clone.copyFrom(this);
384
        return clone;
385
    }
386
387
    /**
388
     * Copies the properties of the source service into this instance.
389
     *
390
     * @param source Source service from which to copy properties.
391
     */
392
    public void copyFrom(final RegisteredService source) {
393
        setId(source.getId());
394
        setProxyPolicy(source.getProxyPolicy());
395
        setDescription(source.getDescription());
396
        setName(source.getName());
397
        setServiceId(source.getServiceId());
398
        setTheme(source.getTheme());
399
        setEvaluationOrder(source.getEvaluationOrder());
400
        setUsernameAttributeProvider(source.getUsernameAttributeProvider());
401
        setLogoutType(source.getLogoutType());
402
        setAttributeReleasePolicy(source.getAttributeReleasePolicy());
403
        setAccessStrategy(source.getAccessStrategy());
404
        setLogo(source.getLogo());
405
        setLogoutUrl(source.getLogoutUrl());
406
        setPublicKey(source.getPublicKey());
407
        setRequiredHandlers(source.getRequiredHandlers());
408
        setProperties(source.getProperties());
409
        setMultifactorPolicy(source.getMultifactorPolicy());
410
        setInformationUrl(source.getInformationUrl());
411
        setPrivacyUrl(source.getPrivacyUrl());
412
        setContacts(source.getContacts());
413
        setExpirationPolicy(source.getExpirationPolicy());
414
    }
415
416
    /**
417
     * {@inheritDoc}
418
     * Compares this instance with the {@code other} registered service based on
419
     * evaluation order, name. The name comparison is case insensitive.
420
     *
421
     * @see #getEvaluationOrder()
422
     */
423
    @Override
424
    public int compareTo(final RegisteredService other) {
425
        return new CompareToBuilder()
426
                .append(getEvaluationOrder(), other.getEvaluationOrder())
427
                .append(StringUtils.defaultIfBlank(getName(), StringUtils.EMPTY).toLowerCase(),
428
                        StringUtils.defaultIfBlank(other.getName(), StringUtils.EMPTY).toLowerCase())
429
                .append(getServiceId(), other.getServiceId())
430
                .append(getId(), other.getId())
431
                .toComparison();
432
    }
433
434
    @Override
435
    public String toString() {
436
        final ToStringBuilder builder = new ToStringBuilder(null, ToStringStyle.SHORT_PREFIX_STYLE);
437
        builder.append("id", this.id);
438
        builder.append("name", this.name);
439
        builder.append("description", this.description);
440
        builder.append("serviceId", this.serviceId);
441
        builder.append("usernameAttributeProvider", this.usernameAttributeProvider);
442
        builder.append("theme", this.theme);
443
        builder.append("evaluationOrder", this.evaluationOrder);
444
        builder.append("logoutType", this.logoutType);
445
        builder.append("attributeReleasePolicy", this.attributeReleasePolicy);
446
        builder.append("accessStrategy", this.accessStrategy);
447
        builder.append("publicKey", this.publicKey);
448
        builder.append("proxyPolicy", this.proxyPolicy);
449
        builder.append("logo", this.logo);
450
        builder.append("logoutUrl", this.logoutUrl);
451
        builder.append("requiredHandlers", this.requiredHandlers);
452
        builder.append("properties", this.properties);
453
        builder.append("multifactorPolicy", this.multifactorPolicy);
454
        builder.append("informationUrl", this.informationUrl);
455
        builder.append("privacyUrl", this.privacyUrl);
456
        builder.append("contacts", this.contacts);
457
        builder.append("expirationPolicy", this.expirationPolicy);
458
        return builder.toString();
459
    }
460
461
    /**
462
     * Create a new service instance.
463
     *
464
     * @return the registered service
465
     */
466
    protected abstract AbstractRegisteredService newInstance();
467
468
    @Override
469
    public Set<String> getRequiredHandlers() {
470
        if (this.requiredHandlers == null) {
471
            this.requiredHandlers = new HashSet<>();
472
        }
473
        return this.requiredHandlers;
474
    }
475
476
    /**
477
     * Sets the required handlers for this service.
478
     *
479
     * @param handlers the new required handlers
480
     */
481
    public void setRequiredHandlers(final Set<String> handlers) {
482
        getRequiredHandlers().clear();
483
        if (handlers == null) {
484
            return;
485
        }
486
        getRequiredHandlers().addAll(handlers);
487
    }
488
489
    /**
490
     * Sets the attribute filtering policy.
491
     *
492
     * @param policy the new attribute filtering policy
493
     */
494
    public void setAttributeReleasePolicy(final RegisteredServiceAttributeReleasePolicy policy) {
495
        this.attributeReleasePolicy = policy;
496
    }
497
498
    @Override
499
    public RegisteredServiceAttributeReleasePolicy getAttributeReleasePolicy() {
500
        return this.attributeReleasePolicy;
501
    }
502
503
    @Override
504
    public String getLogo() {
505
        return this.logo;
506
    }
507
508
    public void setLogo(final String logo) {
509
        this.logo = logo;
510
    }
511
512
    @Override
513
    public RegisteredServicePublicKey getPublicKey() {
514
        return this.publicKey;
515
    }
516
517
    public void setPublicKey(final RegisteredServicePublicKey publicKey) {
518
        this.publicKey = publicKey;
519
    }
520
521
    @Override
522
    public Map<String, RegisteredServiceProperty> getProperties() {
523
        return (Map) this.properties;
524
    }
525
526
    public void setProperties(final Map<String, RegisteredServiceProperty> properties) {
527
        this.properties = (Map) properties;
528
    }
529
530
    public RegisteredServiceMultifactorPolicy getMultifactorPolicy() {
531
        return this.multifactorPolicy;
532
    }
533
534
    public void setMultifactorPolicy(final RegisteredServiceMultifactorPolicy multifactorPolicy) {
535
        this.multifactorPolicy = multifactorPolicy;
536
    }
537
538
    @Override
539
    public List<RegisteredServiceContact> getContacts() {
540
        return (List) this.contacts;
541
    }
542
543
    public void setContacts(final List<RegisteredServiceContact> contacts) {
544
        this.contacts = (List) contacts;
545
    }
546
547
    @Override
548
    public RegisteredServiceExpirationPolicy getExpirationPolicy() {
549
        return expirationPolicy;
550
    }
551
552
    public void setExpirationPolicy(final RegisteredServiceExpirationPolicy expirationPolicy) {
553
        this.expirationPolicy = expirationPolicy;
554
    }
555
}
556