Skip to content
This repository was archived by the owner on Dec 4, 2017. It is now read-only.

Commit 81edd87

Browse files
committed
add interface Msg and implement it
1 parent baa3a82 commit 81edd87

File tree

51 files changed

+713
-318
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+713
-318
lines changed

elasta-authorization/src/main/java/elasta/authorization/Authorizer.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package elasta.authorization;
22

3+
import elasta.core.promise.intfs.Promise;
34
import lombok.Builder;
45
import lombok.Value;
56

@@ -11,7 +12,7 @@
1112
@FunctionalInterface
1213
public interface Authorizer {
1314

14-
boolean authorize(AuthorizeParams params);
15+
Promise<Boolean> authorize(AuthorizeParams params);
1516

1617
@Value
1718
@Builder

elasta-composer/src/main/java/elasta/composer/ComposerUtils.java

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.google.common.collect.ImmutableList;
44
import com.google.common.collect.ImmutableMap;
5+
import com.google.common.collect.ListMultimap;
6+
import io.vertx.core.MultiMap;
57
import io.vertx.core.json.JsonArray;
68
import io.vertx.core.json.JsonObject;
79

@@ -19,4 +21,8 @@ static JsonObject emptyJsonObject() {
1921
static JsonArray emptyJsonArray() {
2022
return JSON_ARRAY;
2123
}
24+
25+
static MultiMap toMultimap(ListMultimap<String, String> headers) {
26+
return new VertxMultiMap(headers);
27+
}
2228
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package elasta.composer;
2+
3+
import com.google.common.collect.ImmutableMap;
4+
5+
import java.util.Map;
6+
import java.util.Optional;
7+
8+
/**
9+
* Created by sohan on 5/15/2017.
10+
*/
11+
public interface Context {
12+
13+
boolean containsKey(String key);
14+
15+
<T> Optional<T> get(String key);
16+
17+
Map<String, Object> getMap();
18+
19+
Context addAll(Map<String, Object> map);
20+
}

elasta-composer/src/main/java/elasta/composer/ContextHolder.java

-14
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package elasta.composer;
2+
3+
import com.google.common.collect.ImmutableMap;
4+
5+
import java.util.Map;
6+
import java.util.Objects;
7+
import java.util.Optional;
8+
9+
/**
10+
* Created by sohan on 5/15/2017.
11+
*/
12+
final public class ContextImpl implements Context {
13+
final Map<String, Object> map;
14+
15+
public ContextImpl(Map<String, Object> map) {
16+
Objects.requireNonNull(map);
17+
this.map = map;
18+
}
19+
20+
@Override
21+
public boolean containsKey(String key) {
22+
return map.containsKey(key);
23+
}
24+
25+
@Override
26+
public <T> Optional<T> get(String key) {
27+
return Optional.ofNullable(cast(map.get(key)));
28+
}
29+
30+
@Override
31+
public Map<String, Object> getMap() {
32+
return map;
33+
}
34+
35+
@Override
36+
public Context addAll(Map<String, Object> map) {
37+
return new ContextImpl(
38+
ImmutableMap.<String, Object>builder().putAll(map).build()
39+
);
40+
}
41+
42+
private <T> T cast(Object value) {
43+
return (T) value;
44+
}
45+
}

elasta-composer/src/main/java/elasta/composer/RequestContext.java renamed to elasta-composer/src/main/java/elasta/composer/Headers.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,12 @@
33
import com.google.common.collect.ListMultimap;
44
import com.google.common.collect.Multimap;
55

6-
import java.util.Date;
7-
import java.util.List;
8-
import java.util.Optional;
9-
import java.util.Set;
6+
import java.util.*;
107

118
/**
129
* Created by sohan on 5/12/2017.
1310
*/
14-
public interface RequestContext {
11+
public interface Headers {
1512

1613
boolean containsKey(String key);
1714

@@ -32,4 +29,8 @@ public interface RequestContext {
3229
List<String> getAll(String key);
3330

3431
ListMultimap<String, String> getMultimap();
32+
33+
Headers addAll(ListMultimap<String, String> multimap);
34+
35+
Headers addAll(Map<String, String> multimap);
3536
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package elasta.composer;
2+
3+
import com.google.common.collect.ListMultimap;
4+
import elasta.composer.ex.NoUserInContextException;
5+
import elasta.composer.impl.MsgImpl;
6+
7+
import java.util.Map;
8+
9+
/**
10+
* Created by sohan on 5/15/2017.
11+
*/
12+
public interface Msg<T> {
13+
14+
Headers headers();
15+
16+
Context context();
17+
18+
T body();
19+
20+
default <R> R userId() {
21+
throw new NoUserInContextException("No user exists in the Msg context");
22+
}
23+
24+
Msg<T> addHeaders(ListMultimap<String, String> multimap);
25+
26+
Msg<T> addHeaders(Map<String, String> map);
27+
28+
Msg<T> addContext(Map<String, Object> map);
29+
30+
Msg<T> withBody(T body);
31+
32+
static <TT> RequestBuilder<TT> builder() {
33+
return new MsgImpl.RequestImplBuilder<>();
34+
}
35+
36+
static <TT> RequestBuilder<TT> builder(Msg<TT> msg) {
37+
return new MsgImpl.RequestImplBuilder<>(msg);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package elasta.composer;
2+
3+
import elasta.core.flow.EnterEventHandlerP;
4+
import elasta.core.flow.StateTrigger;
5+
import elasta.core.promise.intfs.Promise;
6+
7+
/**
8+
* Created by sohan on 5/15/2017.
9+
*/
10+
public interface MsgEnterEventHandlerP<T, R> extends EnterEventHandlerP<Msg<T>, Msg<R>> {
11+
@Override
12+
Promise<StateTrigger<Msg<R>>> handle(Msg<T> tMsg) throws Throwable;
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package elasta.composer;
2+
3+
/**
4+
* Created by sohan on 5/15/2017.
5+
*/
6+
public interface RequestBuilder<T> {
7+
8+
RequestBuilder<T> headers(Headers headers);
9+
10+
RequestBuilder<T> context(Context context);
11+
12+
RequestBuilder<T> body(T body);
13+
14+
RequestBuilder<T> userId(Object userId);
15+
16+
Msg<T> build();
17+
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package elasta.composer;
2+
3+
import com.google.common.collect.ImmutableList;
4+
import com.google.common.collect.ListMultimap;
5+
import io.vertx.core.MultiMap;
6+
7+
import java.util.*;
8+
9+
/**
10+
* Created by sohan on 5/15/2017.
11+
*/
12+
final public class VertxMultiMap implements MultiMap {
13+
final ListMultimap<String, String> multimap;
14+
15+
public VertxMultiMap(ListMultimap<String, String> multimap) {
16+
Objects.requireNonNull(multimap);
17+
this.multimap = multimap;
18+
}
19+
20+
@Override
21+
public String get(CharSequence name) {
22+
return getOne(String.valueOf(name));
23+
}
24+
25+
private String getOne(String key) {
26+
List<String> list = multimap.get(String.valueOf(key));
27+
if (list.size() <= 0) {
28+
return null;
29+
}
30+
return list.get(0);
31+
}
32+
33+
@Override
34+
public String get(String name) {
35+
return getOne(name);
36+
}
37+
38+
@Override
39+
public List<String> getAll(String name) {
40+
return multimap.get(name);
41+
}
42+
43+
@Override
44+
public List<String> getAll(CharSequence name) {
45+
return multimap.get(String.valueOf(name));
46+
}
47+
48+
@Override
49+
public List<Map.Entry<String, String>> entries() {
50+
return ImmutableList.copyOf(multimap.entries());
51+
}
52+
53+
@Override
54+
public boolean contains(String name) {
55+
return multimap.containsKey(name);
56+
}
57+
58+
@Override
59+
public boolean contains(CharSequence name) {
60+
return multimap.containsKey(String.valueOf(name));
61+
}
62+
63+
@Override
64+
public boolean isEmpty() {
65+
return multimap.isEmpty();
66+
}
67+
68+
@Override
69+
public Set<String> names() {
70+
return multimap.keySet();
71+
}
72+
73+
@Override
74+
public MultiMap add(String name, String value) {
75+
throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported");
76+
}
77+
78+
@Override
79+
public MultiMap add(CharSequence name, CharSequence value) {
80+
throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported");
81+
}
82+
83+
@Override
84+
public MultiMap add(String name, Iterable<String> values) {
85+
throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported");
86+
}
87+
88+
@Override
89+
public MultiMap add(CharSequence name, Iterable<CharSequence> values) {
90+
throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported");
91+
}
92+
93+
@Override
94+
public MultiMap addAll(MultiMap map) {
95+
throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported");
96+
}
97+
98+
@Override
99+
public MultiMap addAll(Map<String, String> headers) {
100+
throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported");
101+
}
102+
103+
@Override
104+
public MultiMap set(String name, String value) {
105+
throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported");
106+
}
107+
108+
@Override
109+
public MultiMap set(CharSequence name, CharSequence value) {
110+
throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported");
111+
}
112+
113+
@Override
114+
public MultiMap set(String name, Iterable<String> values) {
115+
throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported");
116+
}
117+
118+
@Override
119+
public MultiMap set(CharSequence name, Iterable<CharSequence> values) {
120+
throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported");
121+
}
122+
123+
@Override
124+
public MultiMap setAll(MultiMap map) {
125+
throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported");
126+
}
127+
128+
@Override
129+
public MultiMap setAll(Map<String, String> headers) {
130+
throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported");
131+
}
132+
133+
@Override
134+
public MultiMap remove(String name) {
135+
throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported");
136+
}
137+
138+
@Override
139+
public MultiMap remove(CharSequence name) {
140+
throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported");
141+
}
142+
143+
@Override
144+
public MultiMap clear() {
145+
throw new UnsupportedOperationException("ImmutableListMultimap: operation not supported");
146+
}
147+
148+
@Override
149+
public int size() {
150+
return multimap.size();
151+
}
152+
153+
@Override
154+
public Iterator<Map.Entry<String, String>> iterator() {
155+
return multimap.entries().iterator();
156+
}
157+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package elasta.composer.ex;
2+
3+
/**
4+
* Created by sohan on 5/15/2017.
5+
*/
6+
final public class NoUserInContextException extends RuntimeException {
7+
public NoUserInContextException(String msg) {
8+
super(msg);
9+
}
10+
}

0 commit comments

Comments
 (0)