Skip to content

Commit a4e3e2b

Browse files
justineechengkwan-ibm
authored andcommitted
Revert MP Rest Client (#55)
* Reverting RC * Reverting MP Rest Client * Updating start dir * Updated copyright year
1 parent 947a27e commit a4e3e2b

File tree

20 files changed

+228
-358
lines changed

20 files changed

+228
-358
lines changed

README.adoc

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2017 IBM Corporation and others.
1+
// Copyright (c) 2017, 2019 IBM Corporation and others.
22
// Licensed under Creative Commons Attribution-NoDerivatives
33
// 4.0 International (CC BY-ND 4.0)
44
// https://creativecommons.org/licenses/by-nd/4.0/
@@ -86,19 +86,13 @@ mvn liberty:start-server
8686

8787
Make sure that your Zipkin server is running and point your browser to the {inv-url}/localhost URL. When you visit
8888
this endpoint, you make two GET HTTP requests, one to the `system` service and one to the `inventory`
89-
service. Both of these requests are configured to be traced, so two new traces will be recorded in Zipkin.
89+
service. Both of these requests are configured to be traced, so a new trace will be recorded in Zipkin.
9090
Visit the {zipkin-url} URL or another location where you configured Zipkin to run and sort the traces
91-
by newest first. Verify that two new traces have been recorded, one for `system` and one for `inventory`,
92-
and that they contain spans with the following names:
93-
94-
The `system` trace:
95-
96-
- `get:io.openliberty.guides.system.systemresource.getproperties`
97-
98-
The `inventory` trace:
91+
by newest first. Verify that this new trace contains three spans with the following names:
9992

10093
- `get:io.openliberty.guides.inventory.inventoryresource.getpropertiesforhost`
101-
- `add() span`
94+
- `get:io.openliberty.guides.system.systemresource.getproperties`
95+
- `addtoinventory() span`
10296

10397
// image::/guides/draft-guide-microprofile-opentracing/resources/disable-tracing.png[png][Disable tracing, width=100%]
10498

@@ -109,13 +103,12 @@ If you examine the other traces, you might notice a red trace entry, which happe
109103
caught by the span. In this case, since one of the tests accesses the `/inventory/systems/badhostname`
110104
endpoint, which is invalid, an error is thrown. This behavior is expected.
111105

112-
When you are done checking out the traces, stop both Open Liberty servers using the Maven
106+
When you are done checking out the services, stop both Open Liberty servers using the Maven
113107
`liberty:stop-server` goal:
114108

115109
```
116110
mvn liberty:stop-server
117111
```
118-
119112
// =================================================================================================
120113
// Running the application with Docker
121114
// =================================================================================================

finish/inventory/pom.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,6 @@
5050
<artifactId>cdi-2.0</artifactId>
5151
<type>esa</type>
5252
</dependency>
53-
<dependency>
54-
<groupId>io.openliberty.features</groupId>
55-
<artifactId>mpConfig-1.3</artifactId>
56-
<type>esa</type>
57-
</dependency>
58-
<dependency>
59-
<groupId>io.openliberty.features</groupId>
60-
<artifactId>mpRestClient-1.1</artifactId>
61-
<type>esa</type>
62-
</dependency>
6353
<dependency>
6454
<groupId>io.openliberty.features</groupId>
6555
<artifactId>mpOpenTracing-1.1</artifactId>

finish/inventory/src/main/java/io/openliberty/guides/inventory/InventoryManager.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//tag::copyright[]
22
/*******************************************************************************
3-
* Copyright (c) 2017, 2018 IBM Corporation and others.
3+
* Copyright (c) 2017, 2019 IBM Corporation and others.
44
* All rights reserved. This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License v1.0
66
* which accompanies this distribution, and is available at
@@ -22,25 +22,25 @@
2222
import java.util.Collections;
2323

2424
import org.eclipse.microprofile.opentracing.Traced;
25-
import org.eclipse.microprofile.rest.client.inject.RestClient;
2625

2726
import io.opentracing.Scope;
2827
import io.opentracing.Tracer;
2928
import io.openliberty.guides.inventory.model.*;
3029

3130
@ApplicationScoped
3231
public class InventoryManager {
33-
32+
3433
private List<SystemData> systems = Collections.synchronizedList(new ArrayList<>());
35-
private InventoryUtils invUtils = new InventoryUtils();
34+
private SystemClient systemClient = new SystemClient();
3635

3736
// tag::custom-tracer[]
38-
@Inject
39-
Tracer tracer;
37+
@Inject Tracer tracer;
4038
// end::custom-tracer[]
4139

4240
public Properties get(String hostname) {
43-
Properties properties = invUtils.getProperties(hostname);
41+
systemClient.init(hostname, 9080);
42+
Properties properties = systemClient.getProperties();
43+
4444
return properties;
4545
}
4646

@@ -66,5 +66,5 @@ public void add(String hostname, Properties systemProps) {
6666
public InventoryList list() {
6767
return new InventoryList(systems);
6868
}
69-
69+
7070
}

finish/inventory/src/main/java/io/openliberty/guides/inventory/InventoryResource.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// tag::copyright[]
22
/*******************************************************************************
3-
* Copyright (c) 2017, 2018 IBM Corporation and others.
3+
* Copyright (c) 2017, 2019 IBM Corporation and others.
44
* All rights reserved. This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License v1.0
66
* which accompanies this distribution, and is available at
@@ -30,8 +30,7 @@
3030
@Path("/systems")
3131
public class InventoryResource {
3232

33-
@Inject
34-
InventoryManager manager;
33+
@Inject InventoryManager manager;
3534

3635
@GET
3736
@Path("/{hostname}")
@@ -40,11 +39,10 @@ public Response getPropertiesForHost(@PathParam("hostname") String hostname) {
4039
Properties props = manager.get(hostname);
4140
if (props == null) {
4241
return Response.status(Response.Status.NOT_FOUND)
43-
.entity("ERROR: Unknown hostname or the system service may "
44-
+ "not be running on " + hostname)
45-
.build();
42+
.entity("ERROR: Unknown hostname or the system service may "
43+
+ "not be running on " + hostname)
44+
.build();
4645
}
47-
4846
manager.add(hostname, props);
4947
return Response.ok(props).build();
5048
}
@@ -55,5 +53,5 @@ public Response getPropertiesForHost(@PathParam("hostname") String hostname) {
5553
public InventoryList listContents() {
5654
return manager.list();
5755
}
58-
56+
5957
}

finish/inventory/src/main/java/io/openliberty/guides/inventory/InventoryUtils.java

Lines changed: 0 additions & 57 deletions
This file was deleted.
Lines changed: 95 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// tag::copyright[]
22
/*******************************************************************************
3-
* Copyright (c) 2017, 2018 IBM Corporation and others.
3+
* Copyright (c) 2017, 2019 IBM Corporation and others.
44
* All rights reserved. This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License v1.0
66
* which accompanies this distribution, and is available at
@@ -10,26 +10,100 @@
1010
* IBM Corporation - Initial implementation
1111
*******************************************************************************/
1212
// end::copyright[]
13-
package io.openliberty.guides.inventory.client;
13+
package io.openliberty.guides.inventory.client;
1414

15-
import java.util.Properties;
16-
import javax.enterprise.context.Dependent;
17-
import javax.ws.rs.ProcessingException;
18-
import javax.ws.rs.GET;
19-
import javax.ws.rs.Path;
20-
import javax.ws.rs.Produces;
21-
import javax.ws.rs.core.MediaType;
22-
import org.eclipse.microprofile.rest.client.annotation.RegisterProvider;
23-
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;
15+
import javax.ws.rs.client.Client;
16+
import javax.ws.rs.client.ClientBuilder;
17+
import javax.ws.rs.client.Invocation.Builder;
18+
import javax.ws.rs.core.HttpHeaders;
19+
import javax.ws.rs.core.MediaType;
20+
import javax.ws.rs.core.Response;
21+
import javax.ws.rs.core.Response.Status;
22+
import java.util.Properties;
23+
import java.net.URI;
2424

25-
@Dependent
26-
@RegisterRestClient
27-
@RegisterProvider(UnknownUrlExceptionMapper.class)
28-
@Path("/properties")
29-
public interface SystemClient {
30-
31-
@GET
32-
@Produces(MediaType.APPLICATION_JSON)
33-
public Properties getProperties() throws UnknownUrlException, ProcessingException;
34-
25+
public class SystemClient {
26+
27+
// Constants for building URI to the system service.
28+
private final int DEFAULT_PORT = Integer.valueOf(System.getProperty("default.http.port"));
29+
private final String SYSTEM_PROPERTIES = "/system/properties";
30+
private final String PROTOCOL = "http";
31+
32+
private String url;
33+
private Builder clientBuilder;
34+
35+
// Used by the following guide(s): CDI, MP-METRICS, FAULT-TOLERANCE
36+
public void init(String hostname) {
37+
this.initHelper(hostname, DEFAULT_PORT);
38+
}
39+
40+
// Used by the following guide(s): MP-CONFIG, MP-HEALTH
41+
public void init(String hostname, int port) {
42+
this.initHelper(hostname, port);
43+
}
44+
45+
// Helper method to set the attributes.
46+
private void initHelper(String hostname, int port) {
47+
this.url = buildUrl(PROTOCOL, hostname, port, SYSTEM_PROPERTIES);
48+
this.clientBuilder = buildClientBuilder(this.url);
49+
}
50+
51+
// Wrapper function that gets properties
52+
public Properties getProperties() {
53+
return getPropertiesHelper(this.clientBuilder);
54+
}
55+
56+
// tag::doc[]
57+
/**
58+
* Builds the URI string to the system service for a particular host.
59+
* @param protocol
60+
* - http or https.
61+
* @param host
62+
* - name of host.
63+
* @param port
64+
* - port number.
65+
* @param path
66+
* - Note that the path needs to start with a slash!!!
67+
* @return String representation of the URI to the system properties service.
68+
*/
69+
// end::doc[]
70+
protected String buildUrl(String protocol, String host, int port, String path) {
71+
try {
72+
URI uri = new URI(protocol, null, host, port, path, null, null);
73+
return uri.toString();
74+
} catch (Exception e) {
75+
System.out.println("URISyntaxException");
76+
return null;
77+
}
78+
}
79+
80+
// Method that creates the client builder
81+
protected Builder buildClientBuilder(String urlString) {
82+
try {
83+
Client client = ClientBuilder.newClient();
84+
Builder builder = client.target(urlString).request();
85+
return builder.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON);
86+
} catch (Exception e) {
87+
System.out.println("ClientBuilderException");
88+
return null;
89+
}
90+
}
91+
92+
// Helper method that processes the request
93+
protected Properties getPropertiesHelper(Builder builder) {
94+
try {
95+
Response response = builder.get();
96+
if (response.getStatus() == Status.OK.getStatusCode()) {
97+
return response.readEntity(Properties.class);
98+
} else {
99+
System.out.println("Response Status is not OK.");
100+
}
101+
} catch (RuntimeException e) {
102+
System.out.println("Runtime exception: " + e.getMessage());
103+
} catch (Exception e) {
104+
System.out.println("Exception thrown while invoking the request.");
105+
}
106+
return null;
107+
}
35108
}
109+

finish/inventory/src/main/java/io/openliberty/guides/inventory/client/UnknownUrlException.java

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)