-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ContentTypeResource endpoint #3338
- Loading branch information
Showing
35 changed files
with
1,492 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...st/src/main/java/com/enonic/xp/app/contentstudio/json/schema/content/ContentTypeJson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package com.enonic.xp.app.contentstudio.json.schema.content; | ||
|
||
import com.google.common.base.Preconditions; | ||
|
||
import com.enonic.xp.app.contentstudio.json.form.FormJson; | ||
import com.enonic.xp.app.contentstudio.rest.resource.schema.content.ContentTypeIconUrlResolver; | ||
import com.enonic.xp.app.contentstudio.rest.resource.schema.content.LocaleMessageResolver; | ||
import com.enonic.xp.app.contentstudio.rest.resource.schema.mixin.InlineMixinResolver; | ||
import com.enonic.xp.schema.content.ContentType; | ||
|
||
@SuppressWarnings("UnusedDeclaration") | ||
public class ContentTypeJson | ||
extends ContentTypeSummaryJson | ||
{ | ||
private final FormJson form; | ||
|
||
public ContentTypeJson( final Builder builder ) | ||
{ | ||
super( builder.contentType, builder.contentTypeIconUrlResolver, builder.localeMessageResolver ); | ||
this.form = new FormJson( builder.contentType.getForm(), builder.localeMessageResolver, builder.inlineMixinResolver ); | ||
} | ||
|
||
public FormJson getForm() | ||
{ | ||
return this.form; | ||
} | ||
|
||
public static Builder create() | ||
{ | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder | ||
{ | ||
private ContentType contentType; | ||
|
||
private ContentTypeIconUrlResolver contentTypeIconUrlResolver; | ||
|
||
private LocaleMessageResolver localeMessageResolver; | ||
|
||
private InlineMixinResolver inlineMixinResolver; | ||
|
||
private Builder() | ||
{ | ||
|
||
} | ||
|
||
public Builder setContentType( final ContentType contentType ) | ||
{ | ||
this.contentType = contentType; | ||
return this; | ||
} | ||
|
||
public Builder setContentTypeIconUrlResolver( final ContentTypeIconUrlResolver contentTypeIconUrlResolver ) | ||
{ | ||
this.contentTypeIconUrlResolver = contentTypeIconUrlResolver; | ||
return this; | ||
} | ||
|
||
public Builder setLocaleMessageResolver( final LocaleMessageResolver localeMessageResolver ) | ||
{ | ||
this.localeMessageResolver = localeMessageResolver; | ||
return this; | ||
} | ||
|
||
public Builder setInlineMixinResolver( final InlineMixinResolver inlineMixinResolver ) | ||
{ | ||
this.inlineMixinResolver = inlineMixinResolver; | ||
return this; | ||
} | ||
|
||
private void validate() | ||
{ | ||
Preconditions.checkNotNull( contentType ); | ||
Preconditions.checkNotNull( localeMessageResolver ); | ||
Preconditions.checkNotNull( contentTypeIconUrlResolver ); | ||
Preconditions.checkNotNull( inlineMixinResolver ); | ||
} | ||
|
||
public ContentTypeJson build() | ||
{ | ||
return new ContentTypeJson( this ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
225 changes: 225 additions & 0 deletions
225
...ava/com/enonic/xp/app/contentstudio/rest/resource/schema/content/ContentTypeResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
package com.enonic.xp.app.contentstudio.rest.resource.schema.content; | ||
|
||
import java.util.Collection; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.annotation.security.RolesAllowed; | ||
import javax.ws.rs.DefaultValue; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.QueryParam; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.CacheControl; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.osgi.service.component.annotations.Component; | ||
import org.osgi.service.component.annotations.Reference; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
|
||
import com.enonic.xp.app.ApplicationKey; | ||
import com.enonic.xp.app.contentstudio.json.schema.content.ContentTypeJson; | ||
import com.enonic.xp.app.contentstudio.json.schema.content.ContentTypeSummaryJson; | ||
import com.enonic.xp.app.contentstudio.json.schema.content.ContentTypeSummaryListJson; | ||
import com.enonic.xp.app.contentstudio.rest.resource.ResourceConstants; | ||
import com.enonic.xp.app.contentstudio.rest.resource.schema.SchemaImageHelper; | ||
import com.enonic.xp.app.contentstudio.rest.resource.schema.mixin.InlineMixinResolver; | ||
import com.enonic.xp.content.ContentId; | ||
import com.enonic.xp.content.ContentService; | ||
import com.enonic.xp.i18n.LocaleService; | ||
import com.enonic.xp.icon.Icon; | ||
import com.enonic.xp.jaxrs.JaxRsComponent; | ||
import com.enonic.xp.schema.content.ContentType; | ||
import com.enonic.xp.schema.content.ContentTypeName; | ||
import com.enonic.xp.schema.content.ContentTypeNames; | ||
import com.enonic.xp.schema.content.ContentTypeService; | ||
import com.enonic.xp.schema.content.ContentTypes; | ||
import com.enonic.xp.schema.content.GetContentTypeParams; | ||
import com.enonic.xp.schema.mixin.MixinService; | ||
import com.enonic.xp.security.RoleKeys; | ||
import com.enonic.xp.site.Site; | ||
import com.enonic.xp.site.SiteConfig; | ||
import com.enonic.xp.support.AbstractImmutableEntityList; | ||
|
||
import static com.google.common.base.Strings.isNullOrEmpty; | ||
|
||
@Path(ResourceConstants.REST_ROOT + "{content:(schema|" + ResourceConstants.CMS_PATH + "/schema)}/content") | ||
@Produces("application/json") | ||
@RolesAllowed({RoleKeys.ADMIN_LOGIN_ID, RoleKeys.ADMIN_ID}) | ||
@Component(immediate = true, property = "group=v2") | ||
public final class ContentTypeResource | ||
implements JaxRsComponent | ||
{ | ||
private static final SchemaImageHelper HELPER = new SchemaImageHelper(); | ||
|
||
private ContentTypeService contentTypeService; | ||
|
||
private ContentTypeIconUrlResolver contentTypeIconUrlResolver; | ||
|
||
private ContentTypeIconResolver contentTypeIconResolver; | ||
|
||
private LocaleService localeService; | ||
|
||
private ContentService contentService; | ||
|
||
private MixinService mixinService; | ||
|
||
@GET | ||
public ContentTypeJson get( @QueryParam("name") final String nameAsString ) | ||
{ | ||
final ContentTypeName name = ContentTypeName.from( nameAsString ); | ||
final GetContentTypeParams getContentTypes = GetContentTypeParams.from( name ); | ||
|
||
final ContentType contentType = contentTypeService.getByName( getContentTypes ); | ||
if ( contentType == null ) | ||
{ | ||
throw new WebApplicationException( String.format( "ContentType [%s] not found", name ), Response.Status.NOT_FOUND ); | ||
} | ||
final LocaleMessageResolver localeMessageResolver = | ||
new LocaleMessageResolver( this.localeService, contentType.getName().getApplicationKey() ); | ||
|
||
return ContentTypeJson. | ||
create(). | ||
setContentType( contentType ). | ||
setContentTypeIconUrlResolver( this.contentTypeIconUrlResolver ). | ||
setInlineMixinResolver( new InlineMixinResolver( this.mixinService ) ). | ||
setLocaleMessageResolver( localeMessageResolver ). | ||
build(); | ||
} | ||
|
||
@GET | ||
@Path("all") | ||
public ContentTypeSummaryListJson all() | ||
{ | ||
return list(); | ||
} | ||
|
||
@GET | ||
@Path("list") | ||
public ContentTypeSummaryListJson list() | ||
{ | ||
|
||
final ContentTypes contentTypes = contentTypeService.getAll(); | ||
|
||
ImmutableList.Builder<ContentTypeSummaryJson> summariesJsonBuilder = new ImmutableList.Builder(); | ||
|
||
contentTypes.forEach( contentType -> { | ||
summariesJsonBuilder.add( new ContentTypeSummaryJson( contentType, this.contentTypeIconUrlResolver, | ||
new LocaleMessageResolver( localeService, contentType.getName() | ||
.getApplicationKey() ) ) ); | ||
} ); | ||
|
||
return new ContentTypeSummaryListJson( summariesJsonBuilder.build() ); | ||
} | ||
|
||
@GET | ||
@Path("getMimeTypes") | ||
public Collection<String> getMimeTypes( @QueryParam("typeNames") final String typeNames ) | ||
{ | ||
return contentTypeService.getMimeTypes( ContentTypeNames.from( typeNames.split( "," ) ) ); | ||
} | ||
|
||
@GET | ||
@Path("byContent") | ||
public ContentTypeSummaryListJson byContent( @QueryParam("contentId") final String content ) | ||
{ | ||
final ContentId contentId = ContentId.from( content ); | ||
final Site site = contentService.getNearestSite( contentId ); | ||
|
||
final ContentTypes contentTypes; | ||
if ( site != null ) | ||
{ | ||
final List<ContentType> types = site.getSiteConfigs().stream(). | ||
map( SiteConfig::getApplicationKey ). | ||
map( ( appKey ) -> contentTypeService.getByApplication( appKey ) ). | ||
flatMap( AbstractImmutableEntityList::stream ). | ||
collect( Collectors.toList() ); | ||
contentTypes = ContentTypes.from( types ); | ||
} | ||
else | ||
{ | ||
contentTypes = ContentTypes.empty(); | ||
} | ||
|
||
ImmutableList.Builder<ContentTypeSummaryJson> summariesJsonBuilder = new ImmutableList.Builder(); | ||
|
||
contentTypes.forEach( type -> { | ||
summariesJsonBuilder.add( new ContentTypeSummaryJson( type, this.contentTypeIconUrlResolver, | ||
new LocaleMessageResolver( localeService, | ||
type.getName().getApplicationKey() ) ) ); | ||
} ); | ||
|
||
return new ContentTypeSummaryListJson( summariesJsonBuilder.build() ); | ||
} | ||
|
||
@GET | ||
@Path("byApplication") | ||
public ContentTypeSummaryListJson getByApplication( @QueryParam("applicationKey") final String applicationKey ) | ||
{ | ||
final ContentTypes contentTypes = contentTypeService.getByApplication( ApplicationKey.from( applicationKey ) ); | ||
|
||
final LocaleMessageResolver localeMessageResolver = | ||
new LocaleMessageResolver( this.localeService, ApplicationKey.from( applicationKey ) ); | ||
return new ContentTypeSummaryListJson( contentTypes, this.contentTypeIconUrlResolver, localeMessageResolver ); | ||
} | ||
|
||
@GET | ||
@Path("icon/{contentTypeName}") | ||
@Produces("image/*") | ||
public Response getIcon( @PathParam("contentTypeName") final String contentTypeNameAsString, | ||
@QueryParam("size") @DefaultValue("128") final int size, @QueryParam("hash") final String hash ) | ||
throws Exception | ||
{ | ||
final ContentTypeName contentTypeName = ContentTypeName.from( contentTypeNameAsString ); | ||
final Icon icon = this.contentTypeIconResolver.resolveIcon( contentTypeName ); | ||
if ( icon == null ) | ||
{ | ||
throw new WebApplicationException( Response.Status.NOT_FOUND ); | ||
} | ||
|
||
final byte[] image = HELPER.readIconImage( icon, size ); | ||
final Response.ResponseBuilder responseBuilder = Response.ok( image, icon.getMimeType() ); | ||
|
||
if ( !isNullOrEmpty( hash ) ) | ||
{ | ||
applyMaxAge( Integer.MAX_VALUE, responseBuilder ); | ||
} | ||
return responseBuilder.build(); | ||
} | ||
|
||
private void applyMaxAge( int maxAge, final Response.ResponseBuilder responseBuilder ) | ||
{ | ||
final CacheControl cacheControl = new CacheControl(); | ||
cacheControl.setMaxAge( maxAge ); | ||
responseBuilder.cacheControl( cacheControl ); | ||
} | ||
|
||
@Reference | ||
public void setContentTypeService( final ContentTypeService contentTypeService ) | ||
{ | ||
this.contentTypeService = contentTypeService; | ||
this.contentTypeIconResolver = new ContentTypeIconResolver( contentTypeService ); | ||
this.contentTypeIconUrlResolver = new ContentTypeIconUrlResolver( this.contentTypeIconResolver ); | ||
} | ||
|
||
@Reference | ||
public void setLocaleService( final LocaleService localeService ) | ||
{ | ||
this.localeService = localeService; | ||
} | ||
|
||
@Reference | ||
public void setContentService( final ContentService contentService ) | ||
{ | ||
this.contentService = contentService; | ||
} | ||
|
||
@Reference | ||
public void setMixinService( final MixinService mixinService ) | ||
{ | ||
this.mixinService = mixinService; | ||
} | ||
} |
Oops, something went wrong.