|
| 1 | +package com.gewia.common.scope.impl; |
| 2 | + |
| 3 | +import com.gewia.common.scope.Scope; |
| 4 | +import com.gewia.common.scope.ScopePart; |
| 5 | +import com.gewia.common.scope.impl.util.Limitation; |
| 6 | +import com.gewia.common.scope.impl.util.Mode; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.List; |
| 9 | + |
| 10 | +/** |
| 11 | + * MicroServiceScope is specified by the following schema: 'microService.topic.mode.limitation.(extra)' . |
| 12 | + * Example: user.user.write.all - lets the given application/user write/create <i>all</i> users. |
| 13 | + */ |
| 14 | +public class MicroServiceScope extends BasicScope { |
| 15 | + |
| 16 | + public MicroServiceScope(ScopePart microService, ScopePart topic, Mode mode, Limitation limitation, ScopePart... extra) { |
| 17 | + super(); |
| 18 | + |
| 19 | + this.addScopePart(microService); |
| 20 | + this.addScopePart(topic); |
| 21 | + this.addScopePart(mode.getScopePart()); |
| 22 | + this.addScopePart(limitation.getScopePart()); |
| 23 | + |
| 24 | + for (ScopePart scopePart : extra) this.addScopePart(scopePart); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public Scope removeScopePart(int index) { |
| 29 | + if (index <= 3) throw new UnsupportedOperationException("MicroServiceScopes have to fit the schema."); |
| 30 | + return super.removeScopePart(index); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Gets the currently available scope parts. |
| 35 | + * |
| 36 | + * <p> |
| 37 | + * The returned list is an unmodifiable list, because of the restrictions of {@link MicroServiceScope}. |
| 38 | + * </p> |
| 39 | + * |
| 40 | + * @return all scope parts |
| 41 | + * |
| 42 | + * @since 1.0 |
| 43 | + */ |
| 44 | + @Override |
| 45 | + public List<ScopePart> getScopeParts() { |
| 46 | + return Collections.unmodifiableList(super.getScopeParts()); |
| 47 | + } |
| 48 | + |
| 49 | + |
| 50 | + /** |
| 51 | + * Adds the given <i>scopePart</i> at the given <i>index</i>. |
| 52 | + * |
| 53 | + * @param scopePart the scope part to add |
| 54 | + * @param index the index to add the scope part at |
| 55 | + * |
| 56 | + * @return this |
| 57 | + * |
| 58 | + * @throws IllegalArgumentException if the schema isn't fulfilled (e.g. invalid mode or limitation) |
| 59 | + * |
| 60 | + * @since 1.0 |
| 61 | + */ |
| 62 | + @Override |
| 63 | + public Scope setScopePart(ScopePart scopePart, int index) { |
| 64 | + if (index == 2 && !Mode.isMode(scopePart)) throw new IllegalArgumentException("Third index has to be a valid mode."); |
| 65 | + if (index == 3 && !Limitation.isLimitation(scopePart)) throw new IllegalArgumentException("Fourth index has to be a valid limitation"); |
| 66 | + |
| 67 | + return this.setScopePart(scopePart, index); |
| 68 | + } |
| 69 | + |
| 70 | + public ScopePart getMicroService() { |
| 71 | + return this.getScopePart(0); |
| 72 | + } |
| 73 | + |
| 74 | + public ScopePart getTopic() { |
| 75 | + return this.getScopePart(1); |
| 76 | + } |
| 77 | + |
| 78 | + public Mode getMode() { |
| 79 | + return Mode.find(this.getScopePart(2).getContent()); |
| 80 | + } |
| 81 | + |
| 82 | + public Limitation getLimitation() { |
| 83 | + return Limitation.find(this.getScopePart(3).getContent()); |
| 84 | + } |
| 85 | + |
| 86 | + public MicroServiceScope setMicroService(ScopePart microService) { |
| 87 | + this.setScopePart(microService, 0); |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + public MicroServiceScope setTopic(ScopePart topic) { |
| 92 | + this.setScopePart(topic, 1); |
| 93 | + return this; |
| 94 | + } |
| 95 | + |
| 96 | + public MicroServiceScope setMode(Mode mode) { |
| 97 | + this.setScopePart(mode.getScopePart(), 2); |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + public MicroServiceScope setLimitation(Limitation limitation) { |
| 102 | + this.setScopePart(limitation.getScopePart(), 3); |
| 103 | + return this; |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments