Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
Adding InvalidCartridgeGroupDefinitionException
Browse files Browse the repository at this point in the history
  • Loading branch information
Vishanth authored and imesh committed May 11, 2015
1 parent 2fdac39 commit 0a6518d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -443,30 +443,21 @@ public Response removeCartridge(
@AuthorizationAction("/permission/protected/manage/addServiceGroup")
@SuperTenantService(true)
public Response addServiceGroup(
GroupBean serviceGroupDefinition) throws RestAPIException {
GroupBean serviceGroupDefinition) throws RestAPIException, InvalidCartridgeGroupDefinitionException {
try {
StratosApiV41Utils.addServiceGroup(serviceGroupDefinition);
URI url = uriInfo.getAbsolutePathBuilder().path(serviceGroupDefinition.getName()).build();

return Response.created(url).entity(new StatusResponseBean(Response.Status.CREATED.getStatusCode(),
String.format("Cartridge Group added successfully: [cartridge-group] %s",
serviceGroupDefinition.getName()))).build();
} catch (InvalidCartridgeGroupDefinitionException e) {
return Response.status(Response.Status.BAD_REQUEST).entity(new StatusResponseBean(
Response.Status.BAD_REQUEST.getStatusCode(), e.getMessage())).build();
} catch (RestAPIException e) {
if (e.getCause().getMessage().contains("already exists")) {
return Response.status(Response.Status.CONFLICT).entity(new StatusResponseBean(
Response.Status.CONFLICT.getStatusCode(), "Cartridge group not found")).build();
} else if (e.getCause().getMessage().contains("duplicate cartridges")) {
return Response.status(Response.Status.BAD_REQUEST).entity(new StatusResponseBean(
Response.Status.BAD_REQUEST.getStatusCode(), "Cartridges duplicated in the group " +
"definition")).build();
} else if (e.getCause().getMessage().contains("duplicate groups")) {
return Response.status(Response.Status.BAD_REQUEST).entity(new StatusResponseBean(
Response.Status.BAD_REQUEST.getStatusCode(), "Groups duplicated in the group " +
"definition")).build();
} else if (e.getCause().getMessage().contains("cyclic group")) {
return Response.status(Response.Status.BAD_REQUEST).entity(new StatusResponseBean(
Response.Status.BAD_REQUEST.getStatusCode(), "Cyclic group behaviour identified in the group " +
"definition")).build();
} else {
throw e;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -970,10 +970,11 @@ public static void notifyArtifactUpdatedEvent(GitNotificationPayloadBean payload
* @param serviceGroupDefinition serviceGroupDefinition
* @throws RestAPIException
*/
public static void addServiceGroup(GroupBean serviceGroupDefinition) throws RestAPIException {
public static void addServiceGroup(GroupBean serviceGroupDefinition)
throws InvalidCartridgeGroupDefinitionException, RestAPIException {
try {
if (serviceGroupDefinition == null) {
throw new RuntimeException("Service Group definition is null");
throw new RuntimeException("Cartridge group definition is null");
}

List<String> cartridgeTypes = new ArrayList<String>();
Expand All @@ -982,7 +983,7 @@ public static void addServiceGroup(GroupBean serviceGroupDefinition) throws Rest
String[] cartridgeGroupNames;

if (log.isDebugEnabled()) {
log.debug("checking cartridges in cartridge group " + serviceGroupDefinition.getName());
log.debug("Checking cartridges in cartridge group " + serviceGroupDefinition.getName());
}

findCartridgesInGroupBean(serviceGroupDefinition, cartridgeTypes);
Expand All @@ -1009,7 +1010,9 @@ public static void addServiceGroup(GroupBean serviceGroupDefinition) throws Rest
j++;
}
} catch (RemoteException e) {
throw new RestAPIException(e);
String message = "Could not add the cartridge group: " + serviceGroupDefinition.getName();
log.error(message, e);
throw new RestAPIException(message, e);
} catch (CloudControllerServiceCartridgeNotFoundExceptionException e) {
throw new RestAPIException(e);
}
Expand Down Expand Up @@ -1040,9 +1043,9 @@ public static void addServiceGroup(GroupBean serviceGroupDefinition) throws Rest
duplicatesOutput.append(dup).append(" ");
}
if (log.isDebugEnabled()) {
log.debug("duplicate subGroups defined: " + duplicatesOutput.toString());
log.debug("duplicate sub-groups defined: " + duplicatesOutput.toString());
}
throw new RestAPIException("Invalid Service Group definition, duplicate subGroups defined:" +
throw new RestAPIException("Invalid cartridge group definition, duplicate sub-groups defined:" +
duplicatesOutput.toString());
}
}
Expand All @@ -1056,6 +1059,8 @@ public static void addServiceGroup(GroupBean serviceGroupDefinition) throws Rest
// Add cartridge group elements to SM cache - done after service group has been added
StratosManagerServiceClient smServiceClient = getStratosManagerServiceClient();
smServiceClient.addUsedCartridgesInCartridgeGroups(serviceGroupDefinition.getName(), cartridgeNames);
} catch (InvalidCartridgeGroupDefinitionException e) {
throw e;
} catch (Exception e) {
// TODO: InvalidServiceGroupException is not received, only AxisFault. Need to fix get the custom exception
String message = "Could not add cartridge group";
Expand Down Expand Up @@ -3231,7 +3236,8 @@ public static List<UserInfoBean> getUsers() throws RestAPIException {
* @param groupBean - cartridge group definition
* @throws RestAPIException - throws the rest api exception when the group definition is invalid
*/
private static void validateCartridgeDuplicationInGroupDefinition(GroupBean groupBean) throws RestAPIException {
private static void validateCartridgeDuplicationInGroupDefinition(GroupBean groupBean)
throws InvalidCartridgeGroupDefinitionException {
if (groupBean == null) {
return;
}
Expand All @@ -3256,7 +3262,8 @@ private static void validateCartridgeDuplicationInGroupDefinition(GroupBean grou
* @param cartridges - list of strings which holds the cartridgeTypes values
* @throws RestAPIException - throws the rest api exception when the cartridges are duplicated
*/
private static void validateCartridgeDuplicationInGroup(List<String> cartridges) throws RestAPIException {
private static void validateCartridgeDuplicationInGroup(List<String> cartridges)
throws InvalidCartridgeGroupDefinitionException {
List<String> checkList = new ArrayList<String>();
for (String cartridge : cartridges) {
if (!checkList.contains(cartridge)) {
Expand All @@ -3265,8 +3272,8 @@ private static void validateCartridgeDuplicationInGroup(List<String> cartridges)
if (log.isDebugEnabled()) {
log.debug("duplicate cartridges defined: " + cartridge);
}
throw new RestAPIException("Invalid Service Group definition, duplicate cartridges defined: " +
cartridge);
throw new InvalidCartridgeGroupDefinitionException("Invalid cartridge group definition, " +
"duplicate cartridges defined: " + cartridge);
}
}
}
Expand All @@ -3279,7 +3286,7 @@ private static void validateCartridgeDuplicationInGroup(List<String> cartridges)
* @throws RestAPIException - throws the rest api exception when the group definition is invalid
*/
private static void validateGroupDuplicationInGroupDefinition(GroupBean groupBean, List<String> parentGroups)
throws RestAPIException {
throws InvalidCartridgeGroupDefinitionException {
if (groupBean == null) {
return;
}
Expand Down Expand Up @@ -3310,7 +3317,7 @@ private static void validateGroupDuplicationInGroupDefinition(GroupBean groupBea
* @throws RestAPIException - throws the rest api exception when group duplicate or when cyclic behaviour occurs
*/
private static void validateGroupDuplicationInGroup(List<String> groups, List<String> parentGroups)
throws RestAPIException {
throws InvalidCartridgeGroupDefinitionException {
List<String> checkList = new ArrayList<String>();
for (String group : groups) {
if (!checkList.contains(group)) {
Expand All @@ -3319,15 +3326,15 @@ private static void validateGroupDuplicationInGroup(List<String> groups, List<St
if (log.isDebugEnabled()) {
log.debug("duplicate group defined: " + group);
}
throw new RestAPIException("Invalid Service Group definition, duplicate groups defined: " +
group);
throw new InvalidCartridgeGroupDefinitionException("Invalid cartridge group definition, " +
"duplicate groups defined: " + group);
}
if (parentGroups.contains(group)) {
if (log.isDebugEnabled()) {
log.debug("cyclic group behaviour identified [group-name]: " + group);
}
throw new RestAPIException("Invalid Service Group definition, cyclic group behaviour identified: " +
group);
throw new InvalidCartridgeGroupDefinitionException("Invalid cartridge group definition, " +
"cyclic group behaviour identified: " + group);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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:https://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 org.apache.stratos.rest.endpoint.exception;

import javax.ws.rs.core.Response;

public class InvalidCartridgeGroupDefinitionException extends Exception {

private static final long serialVersionUID = 7396616678984911736L;

private String message;
private Response.Status httpStatusCode;

public InvalidCartridgeGroupDefinitionException() {
super();
}

public InvalidCartridgeGroupDefinitionException(String message, Throwable cause) {
super(message, cause);
this.message = message;
}

public InvalidCartridgeGroupDefinitionException(Response.Status httpStatusCode, String message, Throwable cause) {
super(message, cause);
this.message = message;
this.httpStatusCode = httpStatusCode;
}

public InvalidCartridgeGroupDefinitionException(String message) {
super(message);
this.message = message;
}

public InvalidCartridgeGroupDefinitionException(Response.Status httpStatusCode, String message) {
super(message);
this.message = message;
this.httpStatusCode = httpStatusCode;
}

public InvalidCartridgeGroupDefinitionException(Throwable cause) {
super(cause);
}

public String getMessage() {
return message;
}

public Response.Status getHTTPStatusCode() {
return httpStatusCode;
}
}

0 comments on commit 0a6518d

Please sign in to comment.