forked from Ecwid/consul-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue Ecwid#207: implemented Agent-local operations
Issue Ecwid#224: implemented Consul Intentions
- Loading branch information
Showing
47 changed files
with
2,730 additions
and
19 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
src/main/java/com/ecwid/consul/json/CertificateChainTypeAdapterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.ecwid.consul.json; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.TypeAdapterFactory; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
import java.io.IOException; | ||
import java.security.cert.Certificate; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public class CertificateChainTypeAdapterFactory implements TypeAdapterFactory { | ||
|
||
private final CertificateTypeAdapter certificateTypeAdapter = new CertificateTypeAdapter(); | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { | ||
Class<? super T> rawType = type.getRawType(); | ||
if (!Collection.class.isAssignableFrom(rawType)) { | ||
return null; | ||
} | ||
return new TypeAdapter<T>() { | ||
|
||
@Override | ||
public void write(JsonWriter out, T value) throws IOException { | ||
List<Certificate> certificates = (List<Certificate>) value; | ||
if (certificates == null) { | ||
out.nullValue(); | ||
} else { | ||
out.beginArray(); | ||
for (Certificate certificate : certificates) { | ||
certificateTypeAdapter.write(out, certificate); | ||
} | ||
out.endArray(); | ||
} | ||
} | ||
|
||
@Override | ||
public T read(JsonReader in) throws IOException { | ||
final List<Certificate> certificates = new ArrayList<>(); | ||
in.beginArray(); | ||
while (in.peek() == JsonToken.STRING) { | ||
certificates.add(certificateTypeAdapter.read(in)); | ||
} | ||
in.endArray(); | ||
return (T) certificates; | ||
} | ||
}; | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
src/main/java/com/ecwid/consul/json/CertificateTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.ecwid.consul.json; | ||
|
||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.cert.Certificate; | ||
import java.security.cert.CertificateEncodingException; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.CertificateFactory; | ||
import java.util.Base64; | ||
|
||
public class CertificateTypeAdapter extends TypeAdapter<Certificate> { | ||
|
||
private static final String BEGIN_CERT = "-----BEGIN CERTIFICATE-----"; | ||
|
||
private static final String END_CERT = "-----END CERTIFICATE-----"; | ||
|
||
private static final String LINE_SEPARATOR = System.getProperty("line.separator"); | ||
|
||
private static final int LINE_LENGTH = 64; | ||
|
||
@Override | ||
public void write(JsonWriter out, Certificate value) throws IOException { | ||
if (value == null) { | ||
out.nullValue(); | ||
} else { | ||
try { | ||
Base64.Encoder encoder = Base64.getMimeEncoder( | ||
LINE_LENGTH, LINE_SEPARATOR.getBytes(StandardCharsets.UTF_8)); | ||
final String encoded = BEGIN_CERT | ||
+ LINE_SEPARATOR | ||
+ new String(encoder.encode(value.getEncoded()), StandardCharsets.UTF_8) | ||
+ LINE_SEPARATOR | ||
+ END_CERT; | ||
out.value(encoded); | ||
} catch (CertificateEncodingException ex) { | ||
throw new IOException(ex); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Certificate read(JsonReader in) throws IOException { | ||
if (in.peek() == JsonToken.NULL) { | ||
in.nextNull(); | ||
return null; | ||
} else { | ||
String certificate = in.nextString(); | ||
try { | ||
return CertificateFactory.getInstance("X509") | ||
.generateCertificate( | ||
new ByteArrayInputStream( | ||
certificate.getBytes(StandardCharsets.UTF_8))); | ||
} catch (CertificateException ex) { | ||
throw new IOException(ex); | ||
} | ||
} | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/ecwid/consul/json/OffsetDateTimeTypeAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.ecwid.consul.json; | ||
|
||
import com.google.gson.TypeAdapter; | ||
import com.google.gson.stream.JsonReader; | ||
import com.google.gson.stream.JsonToken; | ||
import com.google.gson.stream.JsonWriter; | ||
import java.io.IOException; | ||
import java.time.OffsetDateTime; | ||
|
||
public class OffsetDateTimeTypeAdapter extends TypeAdapter<OffsetDateTime> { | ||
|
||
@Override | ||
public void write(JsonWriter out, OffsetDateTime value) throws IOException { | ||
if (value == null) { | ||
out.nullValue(); | ||
} else { | ||
out.value(value.toString()); | ||
} | ||
} | ||
|
||
@Override | ||
public OffsetDateTime read(JsonReader in) throws IOException { | ||
if (in.peek() == JsonToken.NULL) { | ||
in.nextNull(); | ||
return null; | ||
} else { | ||
String timestamp = in.nextString(); | ||
return OffsetDateTime.parse(timestamp); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
package com.ecwid.consul.transport; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author Vasily Vasilkov ([email protected]) | ||
*/ | ||
|
@@ -13,4 +11,6 @@ public interface HttpTransport { | |
|
||
public HttpResponse makeDeleteRequest(HttpRequest request); | ||
|
||
public HttpResponse makePostRequest(HttpRequest request); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.