Skip to content

Commit

Permalink
Return relative URIs (#76)
Browse files Browse the repository at this point in the history
* Update version to 1.2.0

* Fix returned uri

* Consistent return uris

* Generate tar.gz package plus checksums
  • Loading branch information
robbinspg authored and tobespc committed Feb 28, 2018
1 parent 5ddea8f commit 4bc9a08
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 24 deletions.
14 changes: 7 additions & 7 deletions REST-API.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ or from the time of a clear request

1. Create a new metrics collections. Metrics are recorded from collection creation time.
- `POST <context_root>/api/v1/collections`
- returned URI `<context_root>/api/v1/collection/3`
- returned URI `collections/3`
2. Retrieve the metrics from the collection at required interval.
- `GET <context_root>/api/v1/collection/3`
- `GET <context_root>/api/v1/collections/3`
- Process the returned JSON format metrics.
- Optionally clear the metrics from the collection.<br>
`PUT <context_root>/api/v1/collection/3`
`PUT <context_root>/api/v1/collections/3`
3. Delete the collection.
- `DELETE <context_root>/api/v1/collection/3`
- `DELETE <context_root>/api/v1/collections/3`



Expand Down Expand Up @@ -62,8 +62,8 @@ Returns a list of the current metrics collections URIs.
Example:
```JSON
{
"collectionUris": ["http://localhost:9080/javametrics/api/v1/collections/0",
"http://localhost:9080/javametrics/api/v1/collections/1"]
"collectionUris": ["collections/0",
"collections/1"]
}
```

Expand Down Expand Up @@ -99,7 +99,7 @@ A maximum of 10 collections are allowed at any one time. Return code 400 indicat
* **Content:** The uri of the created **collection**.
Example:
```JSON
{"uri":"http://localhost:9080/javametrics/api/v1/collections/1"}
{"uri":"collections/1"}
```

* **Error Responses**
Expand Down
5 changes: 5 additions & 0 deletions packageRelease.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
<copy file="rest/target/javametrics-rest-${project.version}.war" todir="${rest.dir}" />

<zip destfile="${dist.dir}/javametrics-release-${project.version}.zip" basedir="${javametrics.dir}" includes="**/**" />
<tar destfile="${dist.dir}/javametrics-release-${project.version}.tar.gz" basedir="${javametrics.dir}" includes="**/**" compression="gzip"/>
<checksum file="${dist.dir}/javametrics-release-${project.version}.zip" algorithm="MD5"/>
<checksum file="${dist.dir}/javametrics-release-${project.version}.zip" algorithm="SHA-512"/>
<checksum file="${dist.dir}/javametrics-release-${project.version}.tar.gz" algorithm="MD5"/>
<checksum file="${dist.dir}/javametrics-release-${project.version}.tar.gz" algorithm="SHA-512"/>

</target>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Response getContexts(@Context UriInfo uriInfo) {
sb.append(',');
}
sb.append('\"');
UriBuilder builder = uriInfo.getAbsolutePathBuilder();
UriBuilder builder = UriBuilder.fromPath(uriInfo.getPath());
builder.path(Integer.toString(contextId));
URI uri = builder.build();
sb.append(uri.toString());
Expand All @@ -69,10 +69,10 @@ public Response newContext(@Context UriInfo uriInfo) {
return Response.status(Status.BAD_REQUEST).build();
}
int contextId = mp.addContext();
UriBuilder builder = uriInfo.getAbsolutePathBuilder();
UriBuilder builder = UriBuilder.fromPath(uriInfo.getPath());
builder.path(Integer.toString(contextId));
URI uri = builder.build();
return Response.created(uri).entity("{\"uri\":\"" + builder.build() + "\"}").build();
return Response.status(Status.CREATED).header("Location", uri).entity("{\"uri\":\"" + uri + "\"}").build();
}

@Path("/{metricsId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
******************************************************************************/
package com.ibm.javametrics.spring.rest;

import java.net.URI;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -48,19 +46,16 @@ private void init() {
MetricsProcessor mp = MetricsProcessor.getInstance();

@RequestMapping(produces = "application/json", path = "/collections", method = RequestMethod.GET)
public ResponseEntity<?> getCollections(UriComponentsBuilder ucb) {
public ResponseEntity<?> getCollections() {
Integer[] contextIds = mp.getContextIds();
UriComponents uriComponents = ucb.path("/collections/").build();
String uri = uriComponents.toUriString();

StringBuilder sb = new StringBuilder("{\"collectionUris\":[");
boolean comma = false;
for (Integer contextId : contextIds) {
if (comma) {
sb.append(',');
}
sb.append('\"');
sb.append(uri);
sb.append("\"collections/");
sb.append(contextId);
sb.append('\"');
comma = true;
Expand All @@ -71,24 +66,23 @@ public ResponseEntity<?> getCollections(UriComponentsBuilder ucb) {
}

@RequestMapping(produces = "application/json", path = "/collections", method = RequestMethod.POST)
public ResponseEntity<?> createCollection(UriComponentsBuilder ucb) {
public ResponseEntity<?> createCollection() {
if (!initialized) {
init();
}

if (mp.getContextIds().length > 9) {
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
}

int contextId = mp.addContext();

UriComponents uriComponents = ucb.path("/collections/{id}").buildAndExpand(contextId);
URI uri = uriComponents.toUri();
UriComponents uriComponents = UriComponentsBuilder.fromPath("collections/{id}").buildAndExpand(contextId);

HttpHeaders headers = new HttpHeaders();
headers.setLocation(uri);
headers.setLocation(uriComponents.toUri());

String json = new String("{\"uri\":\"" + uri + "\"}");
String json = new String("{\"uri\":\"" + uriComponents.getPath() + "\"}");
return new ResponseEntity<>(json, headers, HttpStatus.CREATED);
}

Expand Down

0 comments on commit 4bc9a08

Please sign in to comment.