Skip to content

Commit

Permalink
add new tool GotifyClientBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
ctlove0523 committed Aug 8, 2021
1 parent 6c25952 commit fe5ff49
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 84 deletions.
45 changes: 45 additions & 0 deletions src/main/java/io/github/ctlove0523/gotify/GotifyClientBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package io.github.ctlove0523.gotify;

/**
* @author chentong
*/
public class GotifyClientBuilder {
private String endpoint;

private String userName;

private String password;

public GotifyClientBuilder endpoint(String endpoint) {
this.endpoint = endpoint;
return this;
}

public GotifyClientBuilder userName(String userName) {
this.userName = userName;
return this;
}

public GotifyClientBuilder password(String password) {
this.password = password;
return this;
}

public GotifyClient build() {
GotifyClientConfig config = new GotifyClientConfig.Builder()
.credential(new Credential() {
@Override
public String getUserName() {
return userName;
}

@Override
public String getPassword() {
return password;
}
})
.endpoint(this.endpoint)
.build();
return new GotifyClientImpl(config);
}
}
43 changes: 9 additions & 34 deletions src/main/java/io/github/ctlove0523/gotify/GotifyClientConfig.java
Original file line number Diff line number Diff line change
@@ -1,62 +1,37 @@
package io.github.ctlove0523.gotify;

public class GotifyClientConfig {
private final String scheme;
private final String host;
private final int port;
private final String endpoint;
private final Credential credential;

GotifyClientConfig(Builder builder) {
this.scheme = builder.scheme;
this.host = builder.host;
this.port = builder.port;
this.credential = builder.credential;
this.endpoint = builder.endpoint;
}


public String getScheme() {
return scheme;
}

public String getHost() {
return host;
}

public int getPort() {
return port;
public String getEndpoint() {
return endpoint;
}

Credential getCredential() {
return credential;
}

public static class Builder {
private String scheme;
private String host;
private int port;
private Credential credential;
private String endpoint;

public Builder builder() {
return new Builder();
}

public Builder scheme(String scheme) {
this.scheme = scheme;
return this;
}

public Builder host(String host) {
this.host = host;
return this;
}

public Builder port(int port) {
this.port = port;
public Builder credential(Credential credential) {
this.credential = credential;
return this;
}

public Builder credential(Credential credential) {
this.credential = credential;
public Builder endpoint(String endpoint) {
this.endpoint = endpoint;
return this;
}

Expand Down
16 changes: 10 additions & 6 deletions src/main/java/io/github/ctlove0523/gotify/JacksonUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author chentong
*/
public class JacksonUtil {
private static final Logger log = LoggerFactory.getLogger(JacksonUtil.class);

private static final ObjectMapper MAPPER = new ObjectMapper();

public static <T> String list2String(List<T> input) {
try {
return MAPPER.writeValueAsString(input);
}
catch (JsonProcessingException e) {
e.printStackTrace();
log.error("list2String json processing exception ", e);
}
return null;
}
Expand All @@ -30,7 +34,7 @@ public static <T> List<T> string2List(String jsonString, Class<T> cls) {
return MAPPER.readValue(jsonString, getCollectionType(List.class, cls));
}
catch (IOException e) {
e.printStackTrace();
log.error("string2List json processing exception ", e);
}
return null;
}
Expand All @@ -44,7 +48,7 @@ public static String map2String(Map<String, Object> map) {
return MAPPER.writeValueAsString(map);
}
catch (JsonProcessingException e) {
e.printStackTrace();
log.error("map2String json processing exception ", e);
}

return null;
Expand All @@ -55,7 +59,7 @@ public static Map<String, Object> string2Map(String input) {
return MAPPER.readValue(input, Map.class);
}
catch (IOException e) {
e.printStackTrace();
log.error("string2Map json processing exception ", e);
}

return new HashMap<>();
Expand All @@ -66,7 +70,7 @@ public static <T> String object2String(T object) {
return MAPPER.writeValueAsString(object);
}
catch (JsonProcessingException e) {
e.printStackTrace();
log.error("object2String json processing exception ", e);
}
return "";
}
Expand All @@ -76,7 +80,7 @@ public static <T> T string2Object(String content, Class<T> clazz) {
return MAPPER.readValue(content, clazz);
}
catch (JsonProcessingException e) {
e.printStackTrace();
log.error("string2Object json processing exception ", e);
}
return null;
}
Expand Down
27 changes: 20 additions & 7 deletions src/main/java/io/github/ctlove0523/gotify/MessageClientImpl.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,47 @@
package io.github.ctlove0523.gotify;

import java.net.MalformedURLException;
import java.net.URI;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.sun.jndi.toolkit.url.Uri;
import io.github.ctlove0523.gotify.app.Application;
import io.github.ctlove0523.gotify.message.Message;
import io.github.ctlove0523.gotify.message.PagedMessages;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class MessageClientImpl implements MessageClient {
private static final Logger log = LoggerFactory.getLogger(MessageClientImpl.class);

private final GotifyClientConfig clientConfig;

private final GotifyClientWebSocketClient webSocketClient;
private GotifyClientWebSocketClient webSocketClient;

public MessageClientImpl(GotifyClientConfig clientConfig) {
this.clientConfig = clientConfig;

String uri = "ws://" + clientConfig.getHost() + ":" + clientConfig.getPort() + "/stream";
String authInfo = clientConfig.getCredential().getUserName() + ":" + clientConfig.getCredential().getPassword();
String authorization = Base64.getEncoder().encodeToString(authInfo.getBytes());
try {
Uri endpointUri = new Uri(clientConfig.getEndpoint());
String uri = "ws://" + endpointUri.getHost() + ":" + endpointUri.getPort() + "/stream";
String authInfo = clientConfig.getCredential().getUserName() + ":" + clientConfig.getCredential()
.getPassword();
String authorization = Base64.getEncoder().encodeToString(authInfo.getBytes());

this.webSocketClient = new GotifyClientWebSocketClient(URI.create(uri));
webSocketClient.addHeader("Authorization", "Basic " + authorization);
webSocketClient.connect();
this.webSocketClient = new GotifyClientWebSocketClient(URI.create(uri));
webSocketClient.addHeader("Authorization", "Basic " + authorization);
webSocketClient.connect();
}
catch (MalformedURLException e) {
log.error("malformed url exception ", e);
}
}


Expand Down
42 changes: 8 additions & 34 deletions src/main/java/io/github/ctlove0523/gotify/UriBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,18 @@
* @author chentong
*/
class UriBuilder {
private String scheme;
private String host;
private int port;
private String endpoint;
private String path;
private Map<String, Object> pathParams;
private Map<String, Object> queryParams;

public static UriBuilder builder() {
return new UriBuilder();
}

public UriBuilder scheme(String scheme) {
this.scheme = scheme;
return this;
}

public UriBuilder host(String host) {
this.host = host;
public UriBuilder config(GotifyClientConfig config) {
this.endpoint = config.getEndpoint();
return this;
}

public UriBuilder port(int port) {
this.port = port;
return this;
public static UriBuilder builder() {
return new UriBuilder();
}

public UriBuilder path(String path) {
Expand All @@ -49,30 +37,16 @@ public UriBuilder queryParams(Map<String, Object> queryParams) {
return this;
}

public UriBuilder config(GotifyClientConfig config) {
this.scheme = config.getScheme();
this.host = config.getHost();
this.port = config.getPort();
return this;
}

public String build() {
StringBuilder sb = new StringBuilder();
sb.append(scheme);
sb.append("://");
sb.append(host);
sb.append(":");
sb.append(port);
sb.append(endpoint);

if (pathParams == null || pathParams.isEmpty()) {
sb.append(path);
}
else {
if (pathParams != null && !pathParams.isEmpty()) {
for (Map.Entry<String, Object> entry : pathParams.entrySet()) {
path = path.replace("{" + entry.getKey() + "}", entry.getValue().toString());
}
sb.append(path);
}
sb.append(path);

if (queryParams != null && !queryParams.isEmpty()) {
List<String> queryString = new ArrayList<>(queryParams.size());
Expand Down
4 changes: 1 addition & 3 deletions src/test/java/io/github/ctlove0523/gotify/BaseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ protected GotifyClient newGotifyClient(MockerServer server) {
.build();

GotifyClientConfig config = new GotifyClientConfig.Builder()
.scheme("http")
.host("localhost")
.port(server.port())
.endpoint("http://localhost:" + server.port())
.credential(credential)
.build();

Expand Down

0 comments on commit fe5ff49

Please sign in to comment.