Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into ps_risk_report_gene…
Browse files Browse the repository at this point in the history
…ration
  • Loading branch information
jrichard committed Nov 22, 2016
2 parents 5b5f13f + d3bcd4f commit 5f8ab9c
Show file tree
Hide file tree
Showing 140 changed files with 1,660 additions and 11,642 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>

<artifactId>hub-common</artifactId>
<version>2.1.1-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>

<name>Hub Common</name>
<description>General Code for Hub Integration with CI systems</description>
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/*******************************************************************************
* Copyright (C) 2016 Black Duck Software, Inc.
* http://www.blackducksoftware.com/
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*******************************************************************************/
package com.blackducksoftware.integration.hub;

import java.io.IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,16 @@
import com.blackducksoftware.integration.hub.api.item.HubItem;
import com.blackducksoftware.integration.hub.exception.BDRestException;
import com.blackducksoftware.integration.hub.rest.RestConnection;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class HubItemRestService<T extends HubItem> extends HubRestService {
private final Gson gson;

private final JsonParser jsonParser;

private final Type itemType;

private final Type itemListType;

public HubItemRestService(final RestConnection restConnection, final Gson gson, final JsonParser jsonParser,
final Type itemType, final Type itemListType) {
public HubItemRestService(final RestConnection restConnection, final Type itemType, final Type itemListType) {
super(restConnection);

this.gson = gson;
this.jsonParser = jsonParser;
this.itemType = itemType;
this.itemListType = itemListType;
}
Expand All @@ -76,36 +67,36 @@ public List<T> getAll(final JsonObject jsonObject, final HubRequest hubRequest)
}

public List<T> getItems(final JsonObject jsonObject) {
final List<T> items = gson.fromJson(jsonObject.get("items"), itemListType);
final List<T> items = getRestConnection().getGson().fromJson(jsonObject.get("items"), itemListType);
return items;
}

public List<T> getItems(final String url) throws IOException, URISyntaxException, BDRestException {
final HubRequest itemRequest = new HubRequest(getRestConnection(), jsonParser);
final HubRequest itemRequest = new HubRequest(getRestConnection());
itemRequest.setMethod(Method.GET);
itemRequest.setUrl(url);

final String response = itemRequest.executeForResponseString();
return gson.fromJson(response, itemListType);
return getRestConnection().getGson().fromJson(response, itemListType);
}

public T getItem(final JsonObject jsonObject, final Class<T> clazz) {
return gson.fromJson(jsonObject, clazz);
return getRestConnection().getGson().fromJson(jsonObject, clazz);
}

public T getItem(final String url) throws IOException, BDRestException, URISyntaxException {
final HubRequest itemRequest = new HubRequest(getRestConnection(), jsonParser);
final HubRequest itemRequest = new HubRequest(getRestConnection());
itemRequest.setMethod(Method.GET);
itemRequest.setUrl(url);
itemRequest.setOffset(HubRequest.EXCLUDE_INTEGER_QUERY_PARAMETER);
itemRequest.setLimit(HubRequest.EXCLUDE_INTEGER_QUERY_PARAMETER);

final String response = itemRequest.executeForResponseString();
return gson.fromJson(response, itemType);
return getRestConnection().getGson().fromJson(response, itemType);
}

public void deleteItem(final String url) throws IOException, BDRestException, URISyntaxException {
final HubRequest itemRequest = new HubRequest(getRestConnection(), jsonParser);
final HubRequest itemRequest = new HubRequest(getRestConnection());
itemRequest.setMethod(Method.DELETE);
itemRequest.setUrl(url);
itemRequest.setOffset(HubRequest.EXCLUDE_INTEGER_QUERY_PARAMETER);
Expand All @@ -115,22 +106,14 @@ public void deleteItem(final String url) throws IOException, BDRestException, UR
}

public T getItem(final List<String> urlSegments) throws IOException, BDRestException, URISyntaxException {
final HubRequest itemRequest = new HubRequest(getRestConnection(), jsonParser);
final HubRequest itemRequest = new HubRequest(getRestConnection());
itemRequest.setMethod(Method.GET);
itemRequest.addUrlSegments(urlSegments);
itemRequest.setOffset(HubRequest.EXCLUDE_INTEGER_QUERY_PARAMETER);
itemRequest.setLimit(HubRequest.EXCLUDE_INTEGER_QUERY_PARAMETER);

final String response = itemRequest.executeForResponseString();
return gson.fromJson(response, itemType);
}

public Gson getGson() {
return gson;
}

public JsonParser getJsonParser() {
return jsonParser;
return getRestConnection().getGson().fromJson(response, itemType);
}

public Type getItemType() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*******************************************************************************
* Copyright (C) 2016 Black Duck Software, Inc.
* http://www.blackducksoftware.com/
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*******************************************************************************/
package com.blackducksoftware.integration.hub.api;

import static com.blackducksoftware.integration.hub.api.UrlConstants.SEGMENT_API;
import static com.blackducksoftware.integration.hub.api.UrlConstants.SEGMENT_REGISTRATIONS;
import static com.blackducksoftware.integration.hub.api.UrlConstants.SEGMENT_V1;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.List;

import org.restlet.data.Method;

import com.blackducksoftware.integration.hub.exception.BDRestException;
import com.blackducksoftware.integration.hub.rest.RestConnection;
import com.google.gson.JsonObject;

public class HubRegistrationRestService extends HubRestService {
private static final List<String> REGISTRATION_SEGMENTS = Arrays.asList(SEGMENT_API, SEGMENT_V1, SEGMENT_REGISTRATIONS);

public HubRegistrationRestService(RestConnection restConnection) {
super(restConnection);
}

public String getRegistrationId() throws IOException, URISyntaxException, BDRestException {
final HubRequest registrationRequest = new HubRequest(getRestConnection());
registrationRequest.setMethod(Method.GET);
registrationRequest.addUrlSegments(REGISTRATION_SEGMENTS);

final JsonObject jsonObject = registrationRequest.executeForResponseJson();
String registrationId = jsonObject.get("registrationId").getAsString();
return registrationId;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,12 @@
import com.blackducksoftware.integration.hub.exception.ResourceDoesNotExistException;
import com.blackducksoftware.integration.hub.rest.RestConnection;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

public class HubRequest {
public static final int EXCLUDE_INTEGER_QUERY_PARAMETER = -999;

private final RestConnection restConnection;

private final JsonParser jsonParser;

private Method method;

private String url;
Expand All @@ -65,9 +62,8 @@ public class HubRequest {

private String q;

public HubRequest(final RestConnection restConnection, final JsonParser jsonParser) {
public HubRequest(final RestConnection restConnection) {
this.restConnection = restConnection;
this.jsonParser = jsonParser;
}

public JsonObject executeForResponseJson() throws IOException, URISyntaxException, BDRestException {
Expand All @@ -79,7 +75,7 @@ public JsonObject executeForResponseJson() throws IOException, URISyntaxExceptio
final int responseCode = response.getStatus().getCode();
if (restConnection.isSuccess(responseCode)) {
final String responseString = restConnection.readResponseAsString(response);
final JsonObject jsonObject = jsonParser.parse(responseString).getAsJsonObject();
final JsonObject jsonObject = restConnection.getJsonParser().parse(responseString).getAsJsonObject();
return jsonObject;
} else {
final String message = String.format("Request was not successful. (responseCode: %s)", responseCode);
Expand Down Expand Up @@ -124,6 +120,11 @@ public void executeDelete() throws BDRestException, URISyntaxException {
final ClientResource clientResource = buildClientResource(restConnection);
try {
restConnection.handleRequest(clientResource);
final int responseCode = clientResource.getResponse().getStatus().getCode();
if (!restConnection.isSuccess(responseCode)) {
throw new BDRestException("There was a problem deleting this item : " + url + ". Error Code: " + responseCode,
clientResource);
}
} finally {
releaseResource(clientResource);
}
Expand Down

0 comments on commit 5f8ab9c

Please sign in to comment.