Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • clients/cesium-grp/cesium-plus-pod
  • clients/java/duniter4j
  • ji_emme/duniter4j
  • dvermd/cesium-plus-pod
  • okayotanoka/cesium-plus-pod
  • pokapow/cesium-plus-pod
  • pini-gh/cesium-plus-pod
7 results
Show changes
Showing
with 759 additions and 734 deletions
...@@ -2,7 +2,7 @@ package org.duniter.core.client.model.bma; ...@@ -2,7 +2,7 @@ package org.duniter.core.client.model.bma;
/* /*
* #%L * #%L
* UCoin Java Client :: Core API * Duniter4j :: Core API
* %% * %%
* Copyright (C) 2014 - 2015 EIS * Copyright (C) 2014 - 2015 EIS
* %% * %%
...@@ -23,28 +23,18 @@ package org.duniter.core.client.model.bma; ...@@ -23,28 +23,18 @@ package org.duniter.core.client.model.bma;
*/ */
import java.io.Serializable; import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;
import lombok.Data;
import lombok.experimental.FieldNameConstants;
import java.io.Serializable;
@Data
@FieldNameConstants
public class WotLookup { public class WotLookup {
public boolean partial; private boolean partial;
public Result[] results; private Result[] results;
public boolean isPartial() {
return partial;
}
public void setPartial(boolean partial) {
this.partial = partial;
}
public Result[] getResults() {
return results;
}
public void setResults(Result[] results) {
this.results = results;
}
public String toString() { public String toString() {
String s = ""; String s = "";
...@@ -59,225 +49,147 @@ public class WotLookup { ...@@ -59,225 +49,147 @@ public class WotLookup {
return s; return s;
} }
@Data
@FieldNameConstants
public static class Result implements Serializable { public static class Result implements Serializable {
private static final long serialVersionUID = -39452685440482106L; private static final long serialVersionUID = -39452685440482106L;
public String pubkey; private String pubkey;
public Uid[] uids; private Uid[] uids;
public SignedSignature[] signed; private SignedSignature[] signed;
public String getPubkey() {
return pubkey;
}
public void setPubkey(String pubkey) {
this.pubkey = pubkey;
}
public Uid[] getUids() {
return uids;
}
public void setUids(Uid[] uids) {
this.uids = uids;
}
public SignedSignature[] getSigned() {
return signed;
}
public void setSigned(SignedSignature[] signed) {
this.signed = signed;
}
} }
public class Uid { @Data
@FieldNameConstants
public String uid; public static class Uid {
public Meta meta;
public String self;
public Boolean revoked;
public String revocation_sig;
public OtherSignature[] others;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public Meta getMeta() {
return meta;
}
public void setMeta(Meta meta) {
this.meta = meta;
}
public String getSelf() {
return self;
}
public void setSelf(String self) {
this.self = self;
}
public OtherSignature[] getOthers() {
return others;
}
public void setOthers(OtherSignature[] others) { public interface JsonFields {
this.others = others; String REVOCATION_SIG="revocation_sig";
String REVOKED_ON="revoked_on";
} }
private String uid;
private Meta meta;
private String self;
private Boolean revoked;
private Long revokedOn;
private String revocationSig;
private OtherSignature[] others;
public Boolean getRevoked() {
return revoked;
}
public void setRevoked(Boolean revoked) {
this.revoked = revoked;
}
@JsonGetter(JsonFields.REVOCATION_SIG)
public String getRevocationSig() { public String getRevocationSig() {
return revocation_sig; return revocationSig;
} }
@JsonSetter(JsonFields.REVOCATION_SIG)
public void setRevocationSig(String revocationSig) { public void setRevocationSig(String revocationSig) {
this.revocation_sig = revocationSig; this.revocationSig = revocationSig;
} }
}
public class Meta implements Serializable { @JsonGetter(JsonFields.REVOKED_ON)
public String timestamp; public Long getRevokedOn() {
public Long block_number; return revokedOn;
public String getTimestamp() {
return timestamp;
} }
public void setTimestamp(String timestamp) { @JsonSetter(JsonFields.REVOKED_ON)
this.timestamp = timestamp; public void setRevokedOn(Long revokedOn) {
this.revokedOn = revokedOn;
} }
} }
public class OtherSignature { @Data
@FieldNameConstants
public String pubkey; public static class Meta implements Serializable {
public Meta meta;
public String signature;
public String[] uids;
public boolean isMember;
public boolean wasMember;
public String getPubkey() {
return pubkey;
}
public void setPubkey(String pubkey) {
this.pubkey = pubkey;
}
public Meta getMeta() {
return meta;
}
public void setMeta(Meta meta) {
this.meta = meta;
}
public String getSignature() { public interface JsonFields {
return signature; String BLOCK_HASH="block_hash";
String BLOCK_NUMBER="block_number";
} }
public void setSignature(String signature) { private String timestamp;
this.signature = signature; private String blockHash;
} private Long blockNumber;
public String[] getUids() {
return uids;
}
public void setUids(String[] uids) { @JsonGetter(JsonFields.BLOCK_HASH)
this.uids = uids; public String getBlockHash() {
return blockHash;
} }
public boolean isMember() { @JsonSetter(JsonFields.BLOCK_HASH)
return isMember; public void setBlockHash(String blockHash) {
this.blockHash = blockHash;
} }
@JsonGetter(JsonFields.BLOCK_NUMBER)
public void setMember(boolean member) { public Long getBlockNumber() {
isMember = member; return blockNumber;
} }
public boolean isWasMember() { @JsonSetter(JsonFields.BLOCK_NUMBER)
return wasMember; public void setBlockNumberH(Long blockNumber) {
this.blockNumber = blockNumber;
} }
public void setWasMember(boolean wasMember) {
this.wasMember = wasMember;
}
} }
public class SignedSignature { @Data
@FieldNameConstants
public String uid; public static class OtherSignature {
public String pubkey;
public Meta meta;
public String signature;
public boolean isMember;
public boolean wasMember;
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getPubkey() { private String pubkey;
return pubkey; private Meta meta;
} private String signature;
private String[] uids;
public void setPubkey(String pubkey) { private boolean isMember;
this.pubkey = pubkey; private boolean wasMember;
} }
public Meta getMeta() { @Data
return meta; @FieldNameConstants
public static class SignedSignature {
public interface JsonFields {
String CERT_TIME="cert_time";
} }
public void setMeta(Meta meta) { private String uid;
this.meta = meta; private String pubkey;
} private Meta meta;
private CertTime cerTime;
private String signature;
private boolean isMember;
private boolean wasMember;
public String getSignature() { @JsonGetter(JsonFields.CERT_TIME)
return signature; public CertTime getCerTime() {
return cerTime;
} }
public void setSignature(String signature) { @JsonSetter(JsonFields.CERT_TIME)
this.signature = signature; public void setCerTime(CertTime cerTime) {
this.cerTime = cerTime;
} }
}
public boolean isMember() { @Data
return isMember; @FieldNameConstants
public static class CertTime implements Serializable {
public interface JsonFields {
String BLOCK_HASH="block_hash";
} }
public void setMember(boolean member) { private Long block;
isMember = member; private String blockHash;
}
public boolean isWasMember() { @JsonGetter(JsonFields.BLOCK_HASH)
return wasMember; public String getBlockHash() {
return blockHash;
} }
public void setWasMember(boolean wasMember) { @JsonSetter(JsonFields.BLOCK_HASH)
this.wasMember = wasMember; public void setBlockHash(String blockHash) {
this.blockHash = blockHash;
} }
} }
} }
package org.duniter.core.client.model.bma;
/*-
* #%L
* Duniter4j :: Core Client API
* %%
* Copyright (C) 2014 - 2021 Duniter Team
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import lombok.Data;
import lombok.experimental.FieldNameConstants;
import java.io.Serializable;
@Data
@FieldNameConstants
public class WotPendingMembership implements Serializable {
private static final long serialVersionUID = 1L;
private String pubkey;
private String uid;
private String version;
private String currency;
private String membership;
private Integer blockNumber;
private String blockHash;
private Boolean written;
}
package org.duniter.core.client.dao; package org.duniter.core.client.model.bma;
/* /*
* #%L * #%L
...@@ -22,14 +22,16 @@ package org.duniter.core.client.dao; ...@@ -22,14 +22,16 @@ package org.duniter.core.client.dao;
* #L% * #L%
*/ */
import org.duniter.core.client.model.local.Peer; import lombok.Data;
import lombok.experimental.FieldNameConstants;
import java.util.List; import java.io.Serializable;
/** @Data
* Created by blavenie on 29/12/15. @FieldNameConstants
*/ public class WotPendingMemberships implements Serializable {
public interface PeerDao extends EntityDao<Peer> { private static final long serialVersionUID = -5631089862725952431L;
private WotPendingMembership[] memberships;
List<Peer> getPeersByCurrencyId(long currencyId);
} }
package org.duniter.core.client.model.bma;
/*-
* #%L
* Duniter4j :: Core Client API
* %%
* Copyright (C) 2014 - 2021 Duniter Team
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonSetter;
import lombok.Data;
import lombok.experimental.FieldNameConstants;
import java.io.Serializable;
@Data
@FieldNameConstants
public class WotRequirements implements Serializable {
private static final long serialVersionUID = 1L;
private String pubkey;
private String uid;
private String sig;
private Meta meta;
private String revocation_sig;
private Boolean revoked;
private Long revoked_on;
private Boolean expired;
private Boolean outdistanced;
private Boolean isSentry;
private Boolean wasMember;
private Long membershipPendingExpiresIn;
private Long membershipExpiresIn;
private Certification[] certifications;
@Data
@FieldNameConstants
public static class Meta implements Serializable {
private String timestamp;
}
@Data
@FieldNameConstants
public static class Certification implements Serializable {
private long timestamp;
private String from;
private String to;
private String sig;
private long expiresIn;
}
}
package org.duniter.core.client.model.elasticsearch; package org.duniter.core.client.model.bma;
/* /*
* #%L * #%L
* Duniter4j :: Core Client API * UCoin Java :: Core Client API
* %% * %%
* Copyright (C) 2014 - 2016 EIS * Copyright (C) 2014 - 2016 EIS
* %% * %%
...@@ -22,13 +22,14 @@ package org.duniter.core.client.model.elasticsearch; ...@@ -22,13 +22,14 @@ package org.duniter.core.client.model.elasticsearch;
* #L% * #L%
*/ */
/** import lombok.Data;
* Created by blavenie on 01/03/16. import lombok.experimental.FieldNameConstants;
*/ import org.duniter.core.client.model.BaseIdentity;
public class UserProfile extends Record {
public static final String PROPERTY_TITLE = "title"; @Data
public static final String PROPERTY_DESCRIPTION="description"; @FieldNameConstants
public static final String PROPERTY_CITY="city"; public class WotRequirementsResponse extends BaseIdentity {
private static final long serialVersionUID = -5631089862725952141L;
private WotRequirements[] identities;
} }
package org.duniter.core.client.model.bma;
/*
* #%L
* Duniter4j :: Core Client API
* %%
* Copyright (C) 2014 - 2017 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.Joiner;
import lombok.Data;
import lombok.experimental.FieldNameConstants;
import java.io.Serializable;
/**
* Created by blavenie on 22/01/19.
*/
@Data
@FieldNameConstants
public class Ws2pHead implements Serializable {
public Integer version;
public String pubkey;
public String block;
public String ws2pid;
public String software;
public String softwareVersion;
public String powPrefix;
public String signature;
public AccessConfig privateConfig = new AccessConfig();
public AccessConfig publicConfig = new AccessConfig();
@Data
@FieldNameConstants
public static class AccessConfig {
public boolean useTor;
private String mode;
}
@Override
public String toString() {
return Joiner.on(':').skipNulls().join(new Object[]{
getPrefix(), "HEAD", version, pubkey, block, ws2pid, software, softwareVersion, powPrefix
});
}
@JsonIgnore
protected String getPrefix() {
StringBuilder sb = new StringBuilder();
sb.append("WS2P");
// Private access
if (getPrivateConfig() != null) {
sb.append("O");
if (getPrivateConfig().isUseTor()) {
sb.append("T");
} else {
sb.append("C");
}
if (getPrivateConfig().getMode() != null) {
switch (getPrivateConfig().getMode()) {
case "all":
sb.append("A");
break;
case "mixed":
sb.append("M");
break;
case "strict":
sb.append("S");
break;
}
}
}
// Public access
if (getPublicConfig() != null) {
sb.append("I");
if (getPublicConfig().isUseTor()) {
sb.append("T");
}
else {
sb.append("C");
}
}
return sb.toString();
}
}
\ No newline at end of file
package org.duniter.core.client.model.bma;
/*-
* #%L
* Duniter4j :: Core Client API
* %%
* Copyright (C) 2014 - 2021 Duniter Team
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import org.duniter.core.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Ws2pHeads {
private static final Logger log = LoggerFactory.getLogger(Ws2pHeads.class);
public static final String WS2P_PREFIX = "^WS2P(?:O([CT][SAM]))?(?:I([CT]))?$";
public static final Pattern WS2P_PREFIX_PATTERN = Pattern.compile(WS2P_PREFIX);
private Ws2pHeads() {
// helper class
}
public static Ws2pHead parse(String message) throws IOException {
try {
String[] parts = message.split(":");
if (parts.length < 3 || !parts[0].startsWith("WS2P")) {
throw new IOException("Invalid WS2P message format: " + message);
}
// Head message
if ("HEAD".equals(parts[1])) {
if (parts.length < 4) {
throw new IllegalArgumentException("Invalid WS2P message format: " + message);
}
// Duniter version < 1.6.9
if (parts.length == 4) {
Ws2pHead result = new Ws2pHead();
result.setPubkey(parts[2]);
result.setBlock(parts[3]);
} else {
int version = Integer.parseInt(parts[2]);
if (version >= 1) {
Ws2pHead result = new Ws2pHead();
String prefix = parts[0];
// Private/public options
if (prefix.length() > 4) {
Matcher matches = WS2P_PREFIX_PATTERN.matcher(prefix);
if (!matches.matches()) {
throw new IllegalArgumentException("Invalid WS2P message format: " + message);
}
// Private options
String privateOptions = matches.group(1);
if (StringUtils.isNotBlank(privateOptions)) {
Ws2pHead.AccessConfig privateConfig = result.getPrivateConfig();
privateConfig.setUseTor(privateOptions.startsWith("T"));
String mode = privateOptions.substring(1);
switch (mode) {
case "A":
privateConfig.setMode("all");
break;
case "M":
privateConfig.setMode("mixed");
break;
case "S":
privateConfig.setMode("strict");
break;
}
}
// Public options
String publicOptions = matches.group(2);
if (StringUtils.isNotBlank(publicOptions)) {
Ws2pHead.AccessConfig publicConfig = result.getPrivateConfig();
publicConfig.setUseTor(publicOptions.startsWith("T"));
publicConfig.setMode("all");
}
// For DEBUG only:
log.debug(String.format("Parsing WS2P prefix {%s} into: private %s, public %s",
prefix,
((result.getPrivateConfig().isUseTor() ? "TOR " : "" ) + (result.getPrivateConfig().getMode())),
((result.getPublicConfig().isUseTor() ? "TOR " : "" ) + (result.getPublicConfig().getMode()))
));
}
result.setVersion(version);
result.setPubkey(parts[3]);
result.setBlock(parts[4]);
result.setWs2pid(parts[5]);
result.setSoftware(parts[6]);
result.setSoftwareVersion(parts[7]);
result.setPowPrefix(parts[8]);
return result;
}
}
}
return null;
}
catch(Exception e) {
throw new IOException(e.getMessage(), e);
}
}
}
package org.duniter.core.client.model.bma.converter;
/*
* #%L
* Duniter4j :: Core Client API
* %%
* Copyright (C) 2014 - 2017 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import org.apache.commons.lang3.StringUtils;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.duniter.core.exception.TechnicalException;
import org.duniter.core.util.converter.Converter;
/**
* Created by blavenie on 07/12/16.
*/
public class StringToCertificationConverter
implements Converter<String, BlockchainBlock.Certification> {
@Override
public BlockchainBlock.Certification convert(String source) {
if (StringUtils.isBlank(source)) return null;
String[] parts = source.split(":");
if (parts.length != 4) {
throw new TechnicalException(String.format("Bad format for BlockchainBlock.Certification. Should have 4 parts, but found %s.", parts.length));
}
int i = 0;
return BlockchainBlock.Certification.builder()
.fromPubkey(parts[i++])
.toPubkey(parts[i++])
.blockId(parts[i++])
.signature(parts[i++])
.build();
}
}
\ No newline at end of file
package org.duniter.elasticsearch.rest; package org.duniter.core.client.model.bma.converter;
/* /*
* #%L * #%L
* Duniter4j :: ElasticSearch Plugin * Duniter4j :: Core Client API
* %% * %%
* Copyright (C) 2014 - 2016 EIS * Copyright (C) 2014 - 2017 EIS
* %% * %%
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as * it under the terms of the GNU General Public License as
...@@ -22,17 +22,25 @@ package org.duniter.elasticsearch.rest; ...@@ -22,17 +22,25 @@ package org.duniter.elasticsearch.rest;
* #L% * #L%
*/ */
import org.elasticsearch.common.xcontent.XContentBuilder; import org.duniter.core.client.model.bma.Endpoints;
import org.elasticsearch.rest.BytesRestResponse; import org.duniter.core.client.model.bma.NetworkPeering;
import org.elasticsearch.rest.RestRequest; import org.duniter.core.exception.TechnicalException;
import org.elasticsearch.rest.RestStatus; import org.duniter.core.util.converter.Converter;
import java.io.IOException; import java.io.IOException;
public class XContentRestResponse extends BytesRestResponse { /**
* Created by blavenie on 07/12/16.
*/
public class StringToEndpointConverter
implements Converter<String, NetworkPeering.Endpoint> {
public XContentRestResponse(RestRequest request, RestStatus status, XContentBuilder builder) throws IOException { @Override
super(status, builder); public NetworkPeering.Endpoint convert(String source) {
try {
return Endpoints.parse(source).orElse(null);
} catch (IOException e) {
throw new TechnicalException(e);
}
} }
}
} \ No newline at end of file
package org.duniter.core.client.model.bma.converter;
/*
* #%L
* Duniter4j :: Core Client API
* %%
* Copyright (C) 2014 - 2017 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import org.apache.commons.lang3.StringUtils;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.duniter.core.exception.TechnicalException;
import org.duniter.core.util.converter.Converter;
/**
* Created by blavenie on 07/12/16.
*/
public class StringToIdentityConverter
implements Converter<String, BlockchainBlock.Identity> {
@Override
public BlockchainBlock.Identity convert(String identityStr) {
if (StringUtils.isBlank(identityStr)) return null;
String[] parts = identityStr.split(":");
if (parts.length != 4) {
throw new TechnicalException(String.format("Bad format for BlockchainBlock.Identity. Should have 4 parts, but found %s.", parts.length));
}
int i = 0;
return BlockchainBlock.Identity.builder()
.publicKey(parts[i++])
.signature(parts[i++])
.blockUid(parts[i++])
.userId(parts[i++])
.build();
}
}
\ No newline at end of file
package org.duniter.core.client.model.bma.converter;
/*
* #%L
* Duniter4j :: Core Client API
* %%
* Copyright (C) 2014 - 2017 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import org.apache.commons.lang3.StringUtils;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.duniter.core.exception.TechnicalException;
import org.duniter.core.util.converter.Converter;
/**
* Converter for BlockchainBlock.Joiner
*/
public class StringToJoinerConverter implements Converter<String, BlockchainBlock.Joiner> {
@Override
public BlockchainBlock.Joiner convert(String source) {
if (StringUtils.isBlank(source)) return null;
String[] parts = source.split(":");
if (parts.length != 5) {
throw new TechnicalException(String.format("Bad format for BlockchainBlock.Identity. Should have 5 parts, but found %s.", parts.length));
}
int i = 0;
return BlockchainBlock.Joiner.builder()
.publicKey(parts[i++])
.signature(parts[i++])
.membershipBlockUid(parts[i++])
.idtyBlockUid(parts[i++])
.userId(parts[i++])
.build();
}
}
\ No newline at end of file
package org.duniter.core.client.model.bma.converter;
/*
* #%L
* Duniter4j :: Core Client API
* %%
* Copyright (C) 2014 - 2017 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import org.apache.commons.lang3.StringUtils;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.duniter.core.exception.TechnicalException;
import org.duniter.core.util.converter.Converter;
/**
* Created by blavenie on 07/12/16.
*/
public class StringToRevokedConverter implements Converter<String, BlockchainBlock.Revoked> {
@Override
public BlockchainBlock.Revoked convert(String source) {
if (StringUtils.isBlank(source)) return null;
String[] parts = source.split(":");
if (parts.length != 2) {
throw new TechnicalException(String.format("Bad format for BlockchainBlock.Revoked. Should have 2 parts, but found %s.", parts.length));
}
int i = 0;
return BlockchainBlock.Revoked.builder()
.pubkey(parts[i++])
.signature(parts[i++])
.build();
}
}
\ No newline at end of file
package org.duniter.core.client.model.elasticsearch; package org.duniter.core.client.model.bma.converter;
/* /*
* #%L * #%L
* Duniter4j :: Core Client API * Duniter4j :: Core Client API
* %% * %%
* Copyright (C) 2014 - 2016 EIS * Copyright (C) 2014 - 2017 EIS
* %% * %%
* This program is free software: you can redistribute it and/or modify * This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as * it under the terms of the GNU General Public License as
...@@ -22,40 +22,31 @@ package org.duniter.core.client.model.elasticsearch; ...@@ -22,40 +22,31 @@ package org.duniter.core.client.model.elasticsearch;
* #L% * #L%
*/ */
/** import com.fasterxml.jackson.core.JsonParser;
* Created by blavenie on 01/03/16. import com.fasterxml.jackson.databind.DeserializationContext;
*/ import com.fasterxml.jackson.databind.JsonDeserializer;
public class MessageRecord extends Record { import org.duniter.core.client.model.bma.NetworkPeering;
import org.duniter.core.client.model.bma.Ws2pHead;
public static final String PROPERTY_TIME="time"; import org.duniter.core.client.model.bma.Ws2pHeads;
public static final String PROPERTY_CONTENT="content"; import org.duniter.core.exception.TechnicalException;
public static final String PROPERTY_RECIPIENT="recipient"; import org.duniter.core.util.converter.Converter;
import org.slf4j.Logger;
private Integer time; import org.slf4j.LoggerFactory;
private String content;
private String recipient;
public Integer getTime() { import java.io.IOException;
return time;
}
public void setTime(Integer time) {
this.time = time;
}
public String getContent() { /**
return content; * Created by blavenie on 07/12/16.
} */
public class StringToWs2pHeadConverter
public void setContent(String content) { implements Converter<String, Ws2pHead> {
this.content = content;
} @Override
public Ws2pHead convert(String ept) {
public String getRecipient() { try {
return recipient; return Ws2pHeads.parse(ept);
} } catch (IOException e) {
throw new TechnicalException(e);
public void setRecipient(String recipient) { }
this.recipient = recipient;
} }
} }
\ No newline at end of file
package org.duniter.core.client.model.bma.gson;
/*
* #%L
* UCoin Java :: Core Client API
* %%
* Copyright (C) 2014 - 2016 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import org.duniter.core.client.model.bma.EndpointProtocol;
import org.duniter.core.client.model.bma.NetworkPeering;
import org.apache.http.conn.util.InetAddressUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
public class EndpointAdapter extends TypeAdapter<NetworkPeering.Endpoint> {
@Override
public NetworkPeering.Endpoint read(JsonReader reader) throws IOException {
if (reader.peek() == com.google.gson.stream.JsonToken.NULL) {
reader.nextNull();
return null;
}
String ept = reader.nextString();
ArrayList<String> parts = new ArrayList<>(Arrays.asList(ept.split(" ")));
NetworkPeering.Endpoint endpoint = new NetworkPeering.Endpoint();
endpoint.port = Integer.parseInt(parts.remove(parts.size() - 1));
for (String word : parts) {
if (InetAddressUtils.isIPv4Address(word)) {
endpoint.ipv4 = word;
} else if (InetAddressUtils.isIPv6Address(word)) {
endpoint.ipv6 = word;
} else if (word.startsWith("http")) {
endpoint.url = word;
} else {
try {
endpoint.protocol = EndpointProtocol.valueOf(word);
} catch (IllegalArgumentException e) {
// skip this part
}
}
}
if (endpoint.protocol == null) {
endpoint.protocol = EndpointProtocol.UNDEFINED;
}
return endpoint;
}
public void write(JsonWriter writer, NetworkPeering.Endpoint endpoint) throws IOException {
if (endpoint == null) {
writer.nullValue();
return;
}
writer.value(endpoint.protocol.name() + " " +
endpoint.url + " " +
endpoint.ipv4 + " " +
endpoint.ipv6 + " " +
endpoint.port);
}
}
\ No newline at end of file
package org.duniter.core.client.model.bma.gson;
/*
* #%L
* UCoin Java Client :: Core API
* %%
* Copyright (C) 2014 - 2015 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.google.common.collect.Multimap;
import com.google.gson.GsonBuilder;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.duniter.core.client.model.bma.NetworkPeering;
import java.util.List;
public class GsonUtils {
public static final String DATE_PATTERN = "yyyy-MM-dd HH:mm:ss";
public static GsonBuilder newBuilder() {
return new GsonBuilder()
// make sure date will be serialized
.setDateFormat(DATE_PATTERN)
// Register Multimap adapter
.registerTypeAdapter(Multimap.class, new MultimapTypeAdapter())
// Register Blockchain.* adapter
.registerTypeAdapter(BlockchainBlock.Identity.class, new IdentityTypeAdapter())
.registerTypeAdapter(BlockchainBlock.Joiner.class, new JoinerTypeAdapter())
.registerTypeAdapter(BlockchainBlock.Revoked.class, new RevokedTypeAdapter())
// Register endpoint adapter
.registerTypeAdapter(NetworkPeering.Endpoint.class, new EndpointAdapter())
;
}
public static List<String> getValuesFromJSONAsString(String jsonString, String attributeName) {
return new JsonAttributeParser(attributeName).getValues(jsonString);
}
public static String getValueFromJSONAsString(String jsonString, String attributeName) {
return new JsonAttributeParser(attributeName).getValueAsString(jsonString);
}
public static Number getValueFromJSONAsNumber(String jsonString, String attributeName) {
return new JsonAttributeParser(attributeName).getValueAsNumber(jsonString);
}
public static int getValueFromJSONAsInt(String jsonString, String attributeName) {
return new JsonAttributeParser(attributeName).getValueAsInt(jsonString);
}
public static List<String> getArrayValuesFromJSONAsInt(String jsonString) {
return new JsonArrayParser().getValuesAsList(jsonString);
}
}
package org.duniter.core.client.model.bma.gson;
/*
* #%L
* UCoin Java Client :: Core API
* %%
* Copyright (C) 2014 - 2015 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.google.gson.*;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Type;
public class IdentityTypeAdapter implements JsonDeserializer<BlockchainBlock.Identity>, JsonSerializer<BlockchainBlock.Identity>{
@Override
public BlockchainBlock.Identity deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String identityStr = json.getAsString();
if (StringUtils.isBlank(identityStr)) {
return null;
}
String[] identityParts = identityStr.split(":");
if (identityParts.length != 4) {
throw new JsonParseException(String.format("Bad format for BlockchainBlock.Identity. Should have 4 parts, but found %s.", identityParts.length));
}
BlockchainBlock.Identity result = new BlockchainBlock.Identity();
int i = 0;
result.setPublicKey(identityParts[i++]);
result.setSignature(identityParts[i++]);
result.setBlockUid(identityParts[i++]);
result.setUserId(identityParts[i++]);
return result;
}
@Override
public JsonElement serialize(BlockchainBlock.Identity identity, Type type, JsonSerializationContext context) {
String result = new StringBuilder()
.append(identity.getPublicKey()).append(":")
.append(identity.getSignature()).append(":")
.append(identity.getBlockUid()).append(":")
.append(identity.getUserId()).toString();
return context.serialize(result.toString(), String.class);
}
}
package org.duniter.core.client.model.bma.gson;
/*
* #%L
* UCoin Java Client :: Core API
* %%
* Copyright (C) 2014 - 2015 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.google.gson.*;
import org.duniter.core.client.model.bma.BlockchainBlock;
import org.apache.commons.lang3.StringUtils;
import java.lang.reflect.Type;
public class JoinerTypeAdapter implements JsonDeserializer<BlockchainBlock.Joiner>, JsonSerializer<BlockchainBlock.Joiner>{
@Override
public BlockchainBlock.Joiner deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
String identityStr = json.getAsString();
if (StringUtils.isBlank(identityStr)) {
return null;
}
String[] identityParts = identityStr.split(":");
if (identityParts.length != 5) {
throw new JsonParseException(String.format("Bad format for BlockchainBlock.Identity. Should have 5 parts, but found %s.", identityParts.length));
}
BlockchainBlock.Joiner result = new BlockchainBlock.Joiner();
int i = 0;
result.setPublicKey(identityParts[i++]);
result.setSignature(identityParts[i++]);
result.setMBlockUid(identityParts[i++]);
result.setIBlockUid(identityParts[i++]);
result.setUserId(identityParts[i++]);
return result;
}
@Override
public JsonElement serialize(BlockchainBlock.Joiner member, Type type, JsonSerializationContext context) {
String result = new StringBuilder()
.append(member.getPublicKey()).append(":")
.append(member.getSignature()).append(":")
.append(member.getMBlockUid()).append(":")
.append(member.getIBlockUid()).append(":")
.append(member.getUserId()).toString();
return context.serialize(result.toString(), String.class);
}
}
package org.duniter.core.client.model.bma.gson;
/*
* #%L
* UCoin Java :: Core Client API
* %%
* Copyright (C) 2014 - 2016 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import org.duniter.core.exception.TechnicalException;
import org.duniter.core.util.ObjectUtils;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class JsonAttributeParser {
public static final String REGEX_ATTRIBUTE_STRING_VALUE = "\\\"%s\\\"\\s*:\\s*\"([^\"]+)\\\"";
public static final String REGEX_ATTRIBUTE_NUMERIC_VALUE = "\\\"%s\\\"\\s*:\\s*([\\d]+(?:[.][\\d]+)?)";
private Pattern pattern;
private Pattern numericPattern;
private DecimalFormat decimalFormat;
private String attributeName;
public JsonAttributeParser(String attributeName) {
ObjectUtils.checkNotNull(attributeName);
this.attributeName = attributeName;
this.numericPattern = Pattern.compile(String.format(REGEX_ATTRIBUTE_NUMERIC_VALUE, attributeName));
this.pattern = Pattern.compile(String.format(REGEX_ATTRIBUTE_STRING_VALUE, attributeName));
this.decimalFormat = new DecimalFormat();
this.decimalFormat.getDecimalFormatSymbols().setDecimalSeparator('.');
}
public Number getValueAsNumber(String jsonString) {
ObjectUtils.checkNotNull(jsonString);
Matcher matcher = numericPattern.matcher(jsonString);
if (!matcher.find()) {
return null;
}
String group = matcher.group(1);
try {
Number result = decimalFormat.parse(group);
return result;
} catch (ParseException e) {
throw new TechnicalException(String.format("Error while parsing json numeric value, for attribute [%s]: %s", attributeName,e.getMessage()), e);
}
}
public int getValueAsInt(String jsonString) {
Number numberValue = getValueAsNumber(jsonString);
if (numberValue == null) {
return 0;
}
return numberValue.intValue();
}
public String getValueAsString(String jsonString) {
Matcher matcher = pattern.matcher(jsonString);
if (!matcher.find()) {
return null;
}
return matcher.group(1);
}
public List<String> getValues(String jsonString) {
Matcher matcher = pattern.matcher(jsonString);
List<String> result = new ArrayList<>();
while (matcher.find()) {
String group = matcher.group(1);
result.add(group);
}
return result;
}
}
\ No newline at end of file
package org.duniter.core.client.model.bma.gson;
/*
* #%L
* UCoin Java Client :: Core API
* %%
* Copyright (C) 2014 - 2015 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import com.google.common.base.Preconditions;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import com.google.gson.*;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Map;
@SuppressWarnings({ "rawtypes", "unchecked" })
public class MultimapTypeAdapter implements JsonSerializer<Multimap>, JsonDeserializer<Multimap> {
@Override
public JsonElement serialize(final Multimap src, final Type typeOfSrc, final JsonSerializationContext context) {
return context.serialize(src.asMap(), createMapType(typeOfSrc));
}
@Override
public Multimap deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
throws JsonParseException {
final Multimap multimap = HashMultimap.create();
final Map map = context.deserialize(json, createMapType(typeOfT));
for (final Object key : map.keySet()) {
final Collection values = (Collection) map.get(key);
multimap.putAll(key, values);
}
return multimap;
}
private Type createMapType(final Type multimapType) {
Preconditions.checkArgument(multimapType instanceof ParameterizedType);
final ParameterizedType paramType = (ParameterizedType)multimapType;
final Type[] typeArguments = paramType.getActualTypeArguments();
Preconditions.checkArgument(2 == typeArguments.length, "Type must contain exactly 2 type arguments.");
final ParameterizedTypeImpl valueType = new ParameterizedTypeImpl(Collection.class, null, typeArguments[1]);
final ParameterizedTypeImpl mapType = new ParameterizedTypeImpl(Map.class, null, typeArguments[0], valueType);
return mapType;
}
}
\ No newline at end of file
package org.duniter.core.client.model.bma.gson;
/*
* #%L
* UCoin Java Client :: Core API
* %%
* Copyright (C) 2014 - 2015 EIS
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-3.0.html>.
* #L%
*/
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public class ParameterizedTypeImpl implements ParameterizedType {
private final Type rawType;
private final Type ownerType;
private final Type[] typeArguments;
public ParameterizedTypeImpl(final Type rawType, final Type ownerType, final Type... typeArguments) {
this.rawType = rawType;
this.ownerType = ownerType;
this.typeArguments = typeArguments;
}
@Override
public Type[] getActualTypeArguments() {
return typeArguments;
}
@Override
public Type getRawType() {
return rawType;
}
@Override
public Type getOwnerType() {
return ownerType;
}
}
\ No newline at end of file