diff --git a/dotCMS/src/main/java/com/dotcms/contenttype/model/type/ContentType.java b/dotCMS/src/main/java/com/dotcms/contenttype/model/type/ContentType.java index ac7d8e5ae149..c069791c85bd 100644 --- a/dotCMS/src/main/java/com/dotcms/contenttype/model/type/ContentType.java +++ b/dotCMS/src/main/java/com/dotcms/contenttype/model/type/ContentType.java @@ -15,6 +15,7 @@ import com.dotcms.contenttype.model.field.Field; import com.dotcms.repackage.com.google.common.base.Preconditions; import com.dotcms.repackage.com.google.common.collect.ImmutableList; +import com.dotcms.repackage.org.apache.commons.lang.StringUtils; import com.dotcms.repackage.org.apache.commons.lang.time.DateUtils; import com.dotmarketing.beans.Host; import com.dotmarketing.beans.PermissionableProxy; @@ -56,6 +57,8 @@ public abstract class ContentType implements Serializable, Permissionable, Conte @Value.Check protected void check() { + Preconditions.checkArgument(StringUtils.isNotEmpty(name()), "Name cannot be empty for " + this.getClass()); + if (!(this instanceof UrlMapable)) { Preconditions.checkArgument(detailPage() == null, "Detail Page cannot be set for " + this.getClass()); Preconditions.checkArgument(urlMapPattern() == null, "urlmap cannot be set for " + this.getClass()); diff --git a/dotCMS/src/main/java/com/dotcms/rest/api/v1/contenttype/ContentTypeResource.java b/dotCMS/src/main/java/com/dotcms/rest/api/v1/contenttype/ContentTypeResource.java index c62e90e0fdeb..a173aaf54dd2 100644 --- a/dotCMS/src/main/java/com/dotcms/rest/api/v1/contenttype/ContentTypeResource.java +++ b/dotCMS/src/main/java/com/dotcms/rest/api/v1/contenttype/ContentTypeResource.java @@ -30,6 +30,7 @@ import com.dotcms.rest.annotation.NoCache; import com.dotcms.rest.exception.mapper.ExceptionMapperUtil; import com.dotmarketing.business.APILocator; +import com.dotmarketing.business.DotStateException; import com.dotmarketing.exception.DotDataException; import com.dotmarketing.exception.DotSecurityException; import com.dotmarketing.util.UtilMethods; @@ -38,12 +39,7 @@ import com.liferay.portal.model.User; import com.dotcms.repackage.javax.ws.rs.*; -import com.dotcms.rest.RESTParams; -import com.dotmarketing.portlets.structure.model.Structure; -import com.dotmarketing.util.UtilMethods; -import com.liferay.util.StringUtil; import static com.dotcms.util.CollectionsUtils.map; -import static com.dotcms.util.ConversionUtils.toInt; import java.util.Map; @Path("/v1/contenttype") @@ -78,24 +74,38 @@ public final Response createType(@Context final HttpServletRequest req, final St throws DotDataException, DotSecurityException { final InitDataObject initData = this.webResource.init(null, true, req, true, null); final User user = initData.getUser(); - List typesToSave = new JsonContentTypeTransformer(json).asList(); - // Validate input - for (ContentType type : typesToSave) { - if (UtilMethods.isSet(type.id())) { - return ExceptionMapperUtil.createResponse(null, "Field 'id' should not be set"); + Response response = null; + + try { + List typesToSave = new JsonContentTypeTransformer(json).asList(); + + // Validate input + for (ContentType type : typesToSave) { + if (UtilMethods.isSet(type.id())) { + return ExceptionMapperUtil.createResponse(null, "Field 'id' should not be set"); + } } - } - List retTypes = new ArrayList<>(); + List retTypes = new ArrayList<>(); - // Persist input - for (ContentType type :typesToSave) { - retTypes.add(APILocator.getContentTypeAPI(user, true).save(type)); - } + // Persist input + for (ContentType type :typesToSave) { + retTypes.add(APILocator.getContentTypeAPI(user, true).save(type)); + } + + response = Response.ok(new ResponseEntityView(new JsonContentTypeTransformer(retTypes).mapList())).build(); - return Response.ok(new ResponseEntityView(new JsonContentTypeTransformer(retTypes).mapList())).build(); + } catch (DotStateException e) { + response = ExceptionMapperUtil.createResponse(null, "Content-type is not valid ("+ e.getMessage() +")"); + + } catch (Exception e) { + + response = ExceptionMapperUtil.createResponse(e, Response.Status.INTERNAL_SERVER_ERROR); + } + + return response; } @@ -135,6 +145,10 @@ public Response updateType(@PathParam("id") final String id, final String json, response = Response.ok(new ResponseEntityView(new JsonContentTypeTransformer(contentType).mapObject())).build(); } } + } catch (DotStateException e) { + + response = ExceptionMapperUtil.createResponse(null, "Content-type is not valid ("+ e.getMessage() +")"); + } catch (NotFoundInDbException e) { response = ExceptionMapperUtil.createResponse(e, Response.Status.NOT_FOUND);