25
25
import io .kamax .matrix ._MatrixUser ;
26
26
import io .kamax .matrix .client .*;
27
27
import io .kamax .matrix .hs ._MatrixRoom ;
28
+ import io .kamax .matrix .json .LoginPostBody ;
29
+ import io .kamax .matrix .json .LoginResponse ;
28
30
import io .kamax .matrix .json .UserDisplaynameSetBody ;
29
31
32
+ import org .apache .http .client .methods .HttpPost ;
30
33
import org .apache .http .client .methods .HttpPut ;
31
- import org .slf4j .Logger ;
32
- import org .slf4j .LoggerFactory ;
34
+ import org .apache .http .client .utils .URIBuilder ;
33
35
34
36
import java .net .URI ;
37
+ import java .util .Optional ;
35
38
36
39
public class MatrixHttpClient extends AMatrixHttpClient implements _MatrixClient {
37
40
38
- private Logger log = LoggerFactory .getLogger (MatrixHttpClient .class );
39
-
40
41
public MatrixHttpClient (MatrixClientContext context ) {
41
42
super (context );
42
43
}
@@ -45,9 +46,16 @@ protected _MatrixID getMatrixId(String localpart) {
45
46
return new MatrixID (localpart , getHomeserver ().getDomain ());
46
47
}
47
48
49
+ @ Override
50
+ protected URIBuilder getClientPathBuilder (String action ) {
51
+ URIBuilder builder = super .getClientPathBuilder (action );
52
+ context .getUser ().ifPresent (user -> builder .setPath (builder .getPath ().replace ("{userId}" , user .getId ())));
53
+ return builder ;
54
+ }
55
+
48
56
@ Override
49
57
public void setDisplayName (String name ) {
50
- URI path = getClientPath ("/profile/{userId}/displayname" );
58
+ URI path = getClientPathWithAccessToken ("/profile/{userId}/displayname" );
51
59
HttpPut req = new HttpPut (path );
52
60
req .setEntity (getJsonEntity (new UserDisplaynameSetBody (name )));
53
61
execute (req );
@@ -63,4 +71,39 @@ public _MatrixUser getUser(_MatrixID mxId) {
63
71
return new MatrixHttpUser (getContext (), mxId );
64
72
}
65
73
74
+ @ Override
75
+ public Optional <String > getDeviceId () {
76
+ return context .getDeviceId ();
77
+ }
78
+
79
+ @ Override
80
+ public void login (MatrixPasswordLoginCredentials credentials ) {
81
+ HttpPost request = new HttpPost (getClientPath ("/login" ));
82
+ if (context .getDeviceId ().isPresent ()) {
83
+ request .setEntity (getJsonEntity (new LoginPostBody (credentials .getLocalPart (), credentials .getPassword (),
84
+ context .getDeviceId ().get ())));
85
+ } else {
86
+ request .setEntity (getJsonEntity (new LoginPostBody (credentials .getLocalPart (), credentials .getPassword ())));
87
+ }
88
+
89
+ String body = execute (request );
90
+ LoginResponse response = gson .fromJson (body , LoginResponse .class );
91
+ context .setToken (response .getAccessToken ());
92
+ context .setDeviceId (response .getDeviceId ());
93
+ context .setUser (new MatrixID (response .getUserId ()));
94
+
95
+ // FIXME spec returns hostname which we might not be the same as what has been used in baseUrl to login. Must
96
+ // update internals accordingly
97
+ }
98
+
99
+ @ Override
100
+ public void logout () {
101
+ URI path = getClientPathWithAccessToken ("/logout" );
102
+ HttpPost req = new HttpPost (path );
103
+ execute (req );
104
+ context .setToken (null );
105
+ context .setUser (null );
106
+ context .setDeviceId (null );
107
+ }
108
+
66
109
}
0 commit comments