Skip to content

Commit 885a127

Browse files
authored
Merge pull request #26 from SMATechnologies/develop
Merge Release into master
2 parents 1a0b9af + f3bd6f5 commit 885a127

File tree

21 files changed

+438
-34
lines changed

21 files changed

+438
-34
lines changed

.github/workflows/deploy-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ jobs:
2929
if: endsWith(steps.version.outputs.version, '-SNAPSHOT') == false
3030
run: echo -e "${{ secrets.gpg_private_key }}" | gpg --import --batch
3131

32-
- name: Deploy Snapshot Maven package
32+
- name: Deploy Release Maven package
3333
if: endsWith(steps.version.outputs.version, '-SNAPSHOT') == false
3434
run: mvn --batch-mode -Dgpg.passphrase="${{ secrets.gpg_passphrase }}" clean deploy

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,59 @@ Available from Central Maven Repository (https://search.maven.org/artifact/com.s
2222
<dependency>
2323
<groupId>com.smatechnologies</groupId>
2424
<artifactId>opcon-rest-api-client</artifactId>
25-
<version>1.0.1</version>
25+
<version>1.0.2</version>
2626
</dependency>
2727
```
2828

29+
# Example
30+
31+
```java
32+
OpconApiProfile profile = new OpconApiProfile("https://localhost:9010/api");
33+
34+
OpconApi opconApi;
35+
try {
36+
Client client = SmClientBuilder.get()
37+
.configureDefaultClientBuilder(defaultClientBuilder -> defaultClientBuilder
38+
.setTrustAllCert(true)
39+
)
40+
.setDebug(true)
41+
.setFailOnUnknownProperties(true)
42+
.build();
43+
44+
opconApi = new OpconApi(client, profile, new OpconApi.OpconApiListener() {
45+
46+
@Override
47+
public void onFailed(WsException e) {
48+
e.printStackTrace();
49+
}
50+
});
51+
52+
opconApi.login("MyOpconUser", "MyOpconPassword");
53+
54+
} catch (KeyManagementException | NoSuchAlgorithmException | WsException e) {
55+
e.printStackTrace();
56+
return;
57+
}
58+
59+
try {
60+
//Fetch Machines
61+
List<Machine> machines = opconApi.machines().get(null);
62+
63+
//Fetch Roles with machines
64+
RolesCriteria rolesCriteria = new RolesCriteria();
65+
rolesCriteria.setIncludeMachines(true);
66+
List<Role> roles = opconApi.roles().get(rolesCriteria);
67+
68+
} catch (WsException e) {
69+
e.printStackTrace();
70+
}
71+
72+
try {
73+
opconApi.logout();
74+
} catch (WsException e) {
75+
e.printStackTrace();
76+
}
77+
```
2978
# License
3079
Copyright 2019 SMA Technologies
3180

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
<groupId>com.smatechnologies</groupId>
66
<artifactId>opcon-rest-api-client</artifactId>
7-
<version>1.0.2</version>
7+
<version>1.0.3</version>
88

99
<name>SMA OpCon Library Rest Api Client</name>
1010
<description>SMA OpCon Library Rest Api Client</description>

src/main/java/com/smatechnologies/opcon/restapiclient/DefaultClientBuilder.java

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
package com.smatechnologies.opcon.restapiclient;
22

3-
import com.fasterxml.jackson.databind.ObjectMapper;
43
import com.smatechnologies.opcon.commons.util.UrlUtil;
5-
import com.smatechnologies.opcon.restapiclient.jackson.DefaultObjectMapperProvider;
64
import org.glassfish.jersey.client.ClientProperties;
75

86
import javax.net.ssl.SSLContext;
97
import javax.ws.rs.client.Client;
108
import javax.ws.rs.client.ClientBuilder;
11-
import javax.ws.rs.ext.ContextResolver;
129
import java.security.KeyManagementException;
1310
import java.security.NoSuchAlgorithmException;
14-
import java.util.Objects;
1511

1612

1713
/**
@@ -21,9 +17,6 @@ public class DefaultClientBuilder {
2117

2218
private static final String ALGORITHM_PROTOCOL = "TLS";
2319

24-
private Class<? extends ContextResolver<ObjectMapper>> objectMapperProviderClass = DefaultObjectMapperProvider.class;
25-
private ContextResolver<ObjectMapper> objectMapperProvider;
26-
2720
private boolean trustAllCert;
2821
private Integer connectTimeout;
2922
private Integer readTimeout;
@@ -35,20 +28,6 @@ public static DefaultClientBuilder get() {
3528
return new DefaultClientBuilder();
3629
}
3730

38-
public DefaultClientBuilder setObjectMapperProviderClass(Class<? extends ContextResolver<ObjectMapper>> objectMapperProviderClass) {
39-
this.objectMapperProviderClass = Objects.requireNonNull(objectMapperProviderClass, "ObjectMapperProviderClass cannot be null");
40-
objectMapperProvider = null;
41-
42-
return this;
43-
}
44-
45-
public DefaultClientBuilder setObjectMapperProvider(ContextResolver<ObjectMapper> objectMapperProvider) {
46-
this.objectMapperProvider = Objects.requireNonNull(objectMapperProvider, "ObjectMapperProvider cannot be null");
47-
objectMapperProviderClass = null;
48-
49-
return this;
50-
}
51-
5231
public DefaultClientBuilder setTrustAllCert(boolean trustAllCert) {
5332
this.trustAllCert = trustAllCert;
5433

@@ -85,13 +64,6 @@ public Client build() throws KeyManagementException, NoSuchAlgorithmException {
8564
client.property(ClientProperties.READ_TIMEOUT, readTimeout);
8665
}
8766

88-
if (objectMapperProviderClass != null) {
89-
client.register(objectMapperProviderClass);
90-
}
91-
if (objectMapperProvider != null) {
92-
client.register(objectMapperProvider);
93-
}
94-
9567
return client;
9668
}
9769
}

src/main/java/com/smatechnologies/opcon/restapiclient/api/OpconApi.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.smatechnologies.opcon.restapiclient.api;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import com.smatechnologies.opcon.commons.deserializer.DeserializeException;
45
import com.smatechnologies.opcon.commons.deserializer.ZonedDateTimeDeserializer;
56
import com.smatechnologies.opcon.commons.util.VersionComparator;
@@ -27,6 +28,7 @@
2728
import com.smatechnologies.opcon.restapiclient.api.masterjobs.WsMasterJobs;
2829
import com.smatechnologies.opcon.restapiclient.api.masterschedules.WsMasterSchedules;
2930
import com.smatechnologies.opcon.restapiclient.api.mastervisionworkspaces.WsMasterVisionWorkspaces;
31+
import com.smatechnologies.opcon.restapiclient.api.propertyexpressions.WsPropertyExpressions;
3032
import com.smatechnologies.opcon.restapiclient.api.remoteinstances.WsRemoteInstances;
3133
import com.smatechnologies.opcon.restapiclient.api.repositories.WsRepositories;
3234
import com.smatechnologies.opcon.restapiclient.api.resources.WsResources;
@@ -55,13 +57,15 @@
5557
import com.smatechnologies.opcon.restapiclient.api.visionfrequencies.WsVisionFrequencies;
5658
import com.smatechnologies.opcon.restapiclient.api.visionjobanomalies.WsVisionJobAnomalies;
5759
import com.smatechnologies.opcon.restapiclient.api.visionstatistic.WsVisionStatistic;
60+
import com.smatechnologies.opcon.restapiclient.jackson.DefaultObjectMapperProvider;
5861
import com.smatechnologies.opcon.restapiclient.model.Token;
5962
import com.smatechnologies.opcon.restapiclient.model.Version;
6063
import org.slf4j.Logger;
6164
import org.slf4j.LoggerFactory;
6265

6366
import javax.ws.rs.client.Client;
6467
import javax.ws.rs.core.Response;
68+
import javax.ws.rs.ext.ContextResolver;
6569
import java.time.ZonedDateTime;
6670

6771

@@ -78,13 +82,20 @@ public class OpconApi {
7882
private static final String API_SERVER_TIME_HEADER_KEY = "apiServerTime";
7983
private static final String REQUIRED_VERSION = "18.3.0";
8084

85+
public static final String VERSION_ATTRIBUTE = "version";
86+
8187
private final WsFactory wsFactory;
8288

89+
private ContextResolver<ObjectMapper> objectMapperProvider;
8390
private Token token;
8491
private Version version;
8592
private ZonedDateTime lastApiServerTime;
8693

8794
public OpconApi(Client client, final OpconApiProfile profile, OpconApiListener opconApiListener) {
95+
this(client, profile, opconApiListener, new DefaultObjectMapperProvider());
96+
}
97+
98+
public OpconApi(Client client, final OpconApiProfile profile, OpconApiListener opconApiListener, ContextResolver<ObjectMapper> objectMapperProvider) {
8899
Ws.WsListener wsListener = new Ws.WsListener() {
89100

90101
@Override
@@ -137,6 +148,9 @@ public void callModifier(Ws ws) {
137148

138149
};
139150

151+
this.objectMapperProvider = objectMapperProvider;
152+
client.register(objectMapperProvider);
153+
140154
wsFactory = new WsFactory(client, profile, wsListener);
141155
}
142156

@@ -174,6 +188,10 @@ private Token init(Token token) throws WsException {
174188
throw new WsVersionException("Incompatible Version Rest API Server (Required=[" + REQUIRED_VERSION + "] Actual=[" + actualVersion + "])", REQUIRED_VERSION, actualVersion);
175189
}
176190

191+
ObjectMapper objectMapper = objectMapperProvider.getContext(Version.class);
192+
objectMapper.setConfig(objectMapper.getDeserializationConfig().withAttribute(VERSION_ATTRIBUTE, version.getOpConRestApiProductVersion()));
193+
objectMapper.setConfig(objectMapper.getSerializationConfig().withAttribute(VERSION_ATTRIBUTE, version.getOpConRestApiProductVersion()));
194+
177195
return token;
178196
}
179197

@@ -398,6 +416,10 @@ public WsScriptRunners scriptRunners() {
398416
return new WsScriptRunners(wsFactory);
399417
}
400418

419+
public WsPropertyExpressions propertyExpressions() {
420+
return new WsPropertyExpressions(wsFactory);
421+
}
422+
401423
public interface OpconApiListener {
402424

403425
/**

src/main/java/com/smatechnologies/opcon/restapiclient/api/machinegroups/MachineGroupsCriteria.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
*/
1414
public class MachineGroupsCriteria extends AbstractResourcesCriteria<MachineGroupsCriteria.MachineGroupColumns> {
1515

16+
@ObjMapperField
17+
private String name;
18+
1619
@ObjMapperField
1720
private Collection<Integer> ids;
1821

@@ -25,6 +28,14 @@ public class MachineGroupsCriteria extends AbstractResourcesCriteria<MachineGrou
2528
@ObjMapperField
2629
private Boolean includeMachines;
2730

31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
2839
public Collection<Integer> getIds() {
2940
return ids;
3041
}

src/main/java/com/smatechnologies/opcon/restapiclient/api/machines/WsMachines.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
import com.smatechnologies.opcon.restapiclient.WsException;
55
import com.smatechnologies.opcon.restapiclient.WsFactory;
66
import com.smatechnologies.opcon.restapiclient.WsResult;
7+
import com.smatechnologies.opcon.restapiclient.model.Calendar;
78
import com.smatechnologies.opcon.restapiclient.model.MachineStatusCount;
9+
import com.smatechnologies.opcon.restapiclient.model.PropertyExpression;
810
import com.smatechnologies.opcon.restapiclient.model.machine.Machine;
911

1012
import javax.ws.rs.core.GenericType;
1113
import javax.ws.rs.core.Response;
1214
import java.util.List;
15+
import java.util.Objects;
1316

1417

1518
/**
@@ -53,4 +56,15 @@ public int count(MachinesCriteria criteria) throws WsException {
5356
public MachineStatusCount countByStatus(MachinesCriteria criteria) throws WsException {
5457
return wsFactory.create(Ws.Type.GET).path("count_by_status").criteria(criteria).run(MachineStatusCount.class);
5558
}
59+
60+
public Machine put(Machine machine) throws WsException {
61+
Objects.requireNonNull(machine, "Machine cannot be null");
62+
return wsFactory.create(Ws.Type.PUT, machine).path(String.valueOf(machine.getId())).run(Machine.class);
63+
}
64+
65+
public Machine post(Machine machine) throws WsException {
66+
Objects.requireNonNull(machine, "Machine cannot be null");
67+
return wsFactory.create(Ws.Type.POST, machine).run(Machine.class);
68+
}
69+
5670
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.smatechnologies.opcon.restapiclient.api.propertyexpressions;
2+
3+
import java.util.Objects;
4+
5+
import com.smatechnologies.opcon.restapiclient.Ws;
6+
import com.smatechnologies.opcon.restapiclient.WsException;
7+
import com.smatechnologies.opcon.restapiclient.WsFactory;
8+
import com.smatechnologies.opcon.restapiclient.model.PropertyExpression;
9+
10+
public class WsPropertyExpressions {
11+
12+
private final WsFactory wsFactory;
13+
14+
public WsPropertyExpressions(WsFactory wsFactory) {
15+
this.wsFactory = wsFactory.path("PropertyExpression");
16+
}
17+
18+
public PropertyExpression post(PropertyExpression propertyExpression) throws WsException {
19+
Objects.requireNonNull(propertyExpression, "PropertyExpression cannot be null");
20+
return wsFactory.create(Ws.Type.POST, propertyExpression).run(PropertyExpression.class);
21+
}
22+
23+
}

src/main/java/com/smatechnologies/opcon/restapiclient/api/solutionmanagersettings/SolutionManagerSettingsCriteria.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class SolutionManagerSettingsCriteria extends AbstractResourcesCriteria<S
2222
private Integer roleId;
2323
@ObjMapperField
2424
private Boolean globalOnly;
25+
@ObjMapperField("public")
26+
private Boolean isPublic;
2527

2628
public Collection<Integer> getIds() {
2729
return ids;
@@ -63,6 +65,14 @@ public void setGlobalOnly(Boolean globalOnly) {
6365
this.globalOnly = globalOnly;
6466
}
6567

68+
public Boolean getPublic() {
69+
return isPublic;
70+
}
71+
72+
public void setPublic(Boolean isPublic) {
73+
this.isPublic = isPublic;
74+
}
75+
6676
/**
6777
* Allowed columns on SolutionManagerSettings to specify sort order.
6878
*
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.smatechnologies.opcon.restapiclient.jackson.deserializer;
2+
3+
import com.fasterxml.jackson.core.JsonParser;
4+
import com.fasterxml.jackson.core.JsonProcessingException;
5+
import com.fasterxml.jackson.databind.DeserializationContext;
6+
import com.fasterxml.jackson.databind.JsonDeserializer;
7+
import com.fasterxml.jackson.databind.JsonNode;
8+
import com.smatechnologies.opcon.commons.util.VersionComparator;
9+
import com.smatechnologies.opcon.restapiclient.api.OpconApi;
10+
import com.smatechnologies.opcon.restapiclient.model.Property;
11+
12+
import java.io.IOException;
13+
import java.util.ArrayList;
14+
import java.util.List;
15+
16+
17+
/**
18+
* Jackson's json deserializer for {@link List<Property>}
19+
*
20+
* @author Pierre PINON
21+
*/
22+
public class ListPropertyDeserializer extends JsonDeserializer<List<Property>> {
23+
24+
@Override
25+
public List<Property> deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
26+
String apiVersion = (String) ctxt.getAttribute(OpconApi.VERSION_ATTRIBUTE);
27+
28+
List<Property> properties = new ArrayList<>();
29+
30+
JsonNode jsonNode = p.readValueAsTree();
31+
32+
if (!jsonNode.isArray()) {
33+
return null;
34+
}
35+
36+
for (JsonNode node : jsonNode) {
37+
Property property = new Property();
38+
39+
if (new VersionComparator().compare(apiVersion, "19.1.0") >= 0) {
40+
property.setKey(node.get(Property.PROPERTY_KEY).asText());
41+
} else { //Before 19.1.0
42+
property.setKey(node.get(Property.OLD_PROPERTY_NAME).asText());
43+
}
44+
property.setValue(node.get(Property.PROPERTY_VALUE).asText());
45+
46+
properties.add(property);
47+
}
48+
49+
return properties;
50+
}
51+
}

0 commit comments

Comments
 (0)