Search in sources :

Example 51 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project jeeshop by remibantos.

the class MailTemplates method modify.

@PUT
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Transactional
@RolesAllowed(ADMIN)
public MailTemplate modify(MailTemplate mailTemplate) {
    MailTemplate existingMailTemplate = entityManager.find(MailTemplate.class, mailTemplate.getId());
    checkNotNull(existingMailTemplate);
    MailTemplate existingTplWithSameLocaleAndName = mailTemplateFinder.findByNameAndLocale(mailTemplate.getName(), mailTemplate.getLocale());
    if (existingTplWithSameLocaleAndName != null && !existingTplWithSameLocaleAndName.getId().equals(mailTemplate.getId())) {
        throw new WebApplicationException(Response.Status.CONFLICT);
    }
    mailTemplate.setCreationDate(existingMailTemplate.getCreationDate());
    return entityManager.merge(mailTemplate);
}
Also used : WebApplicationException(org.rembx.jeeshop.rest.WebApplicationException) MailTemplate(org.rembx.jeeshop.user.model.MailTemplate) RolesAllowed(javax.annotation.security.RolesAllowed) Transactional(javax.transaction.Transactional)

Example 52 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project smarthome by eclipse.

the class ItemResource method getItems.

@GET
@RolesAllowed({ Role.USER, Role.ADMIN })
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get all available items.", response = EnrichedItemDTO.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = EnrichedItemDTO.class, responseContainer = "List") })
public Response getItems(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") @Nullable String language, @QueryParam("type") @ApiParam(value = "item type filter", required = false) @Nullable String type, @QueryParam("tags") @ApiParam(value = "item tag filter", required = false) @Nullable String tags, @QueryParam("metadata") @ApiParam(value = "metadata selector", required = false) @Nullable String namespaceSelector, @DefaultValue("false") @QueryParam("recursive") @ApiParam(value = "get member items recursively", required = false) boolean recursive, @QueryParam("fields") @ApiParam(value = "limit output to the given fields (comma separated)", required = false) @Nullable String fields) {
    final Locale locale = localeService.getLocale(language);
    final Set<String> namespaces = splitAndFilterNamespaces(namespaceSelector, locale);
    logger.debug("Received HTTP GET request at '{}'", uriInfo.getPath());
    Stream<EnrichedItemDTO> itemStream = // 
    getItems(type, tags).stream().map(// 
    item -> EnrichedItemDTOMapper.map(item, recursive, null, uriInfo.getBaseUri(), locale)).peek(// 
    dto -> addMetadata(dto, namespaces, null)).peek(dto -> dto.editable = isEditable(dto.name));
    itemStream = dtoMapper.limitToFields(itemStream, fields);
    return Response.ok(new Stream2JSONInputStream(itemStream)).build();
}
Also used : Locale(java.util.Locale) JsonObject(com.google.gson.JsonObject) LocaleService(org.eclipse.smarthome.io.rest.LocaleService) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) DTOMapper(org.eclipse.smarthome.io.rest.DTOMapper) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) OnOffType(org.eclipse.smarthome.core.library.types.OnOffType) ApiParam(io.swagger.annotations.ApiParam) Command(org.eclipse.smarthome.core.types.Command) Metadata(org.eclipse.smarthome.core.items.Metadata) ApiOperation(io.swagger.annotations.ApiOperation) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) Consumes(javax.ws.rs.Consumes) Nullable(org.eclipse.jdt.annotation.Nullable) Locale(java.util.Locale) UpDownType(org.eclipse.smarthome.core.library.types.UpDownType) EnrichedItemDTO(org.eclipse.smarthome.io.rest.core.item.EnrichedItemDTO) Map(java.util.Map) DefaultValue(javax.ws.rs.DefaultValue) ManagedItemProvider(org.eclipse.smarthome.core.items.ManagedItemProvider) HeaderParam(javax.ws.rs.HeaderParam) State(org.eclipse.smarthome.core.types.State) DELETE(javax.ws.rs.DELETE) RESTResource(org.eclipse.smarthome.io.rest.RESTResource) Context(javax.ws.rs.core.Context) NonNullByDefault(org.eclipse.jdt.annotation.NonNullByDefault) MetadataKey(org.eclipse.smarthome.core.items.MetadataKey) GroupItemDTO(org.eclipse.smarthome.core.items.dto.GroupItemDTO) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) EventPublisher(org.eclipse.smarthome.core.events.EventPublisher) ReferencePolicy(org.osgi.service.component.annotations.ReferencePolicy) Role(org.eclipse.smarthome.core.auth.Role) EnrichedItemDTOMapper(org.eclipse.smarthome.io.rest.core.item.EnrichedItemDTOMapper) List(java.util.List) ItemBuilderFactory(org.eclipse.smarthome.core.items.ItemBuilderFactory) Stream(java.util.stream.Stream) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) MetadataDTO(org.eclipse.smarthome.core.items.dto.MetadataDTO) JSONResponse(org.eclipse.smarthome.io.rest.JSONResponse) WebApplicationException(javax.ws.rs.WebApplicationException) UriInfo(javax.ws.rs.core.UriInfo) SwitchItem(org.eclipse.smarthome.core.library.items.SwitchItem) PathParam(javax.ws.rs.PathParam) RollershutterItem(org.eclipse.smarthome.core.library.items.RollershutterItem) GET(javax.ws.rs.GET) TypeParser(org.eclipse.smarthome.core.types.TypeParser) HashMap(java.util.HashMap) ApiResponses(io.swagger.annotations.ApiResponses) GroupItem(org.eclipse.smarthome.core.items.GroupItem) ArrayList(java.util.ArrayList) Component(org.osgi.service.component.annotations.Component) Api(io.swagger.annotations.Api) Status(javax.ws.rs.core.Response.Status) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) POST(javax.ws.rs.POST) Logger(org.slf4j.Logger) GenericItem(org.eclipse.smarthome.core.items.GenericItem) ItemEventFactory(org.eclipse.smarthome.core.items.events.ItemEventFactory) EnrichedGroupItemDTO(org.eclipse.smarthome.io.rest.core.item.EnrichedGroupItemDTO) Stream2JSONInputStream(org.eclipse.smarthome.io.rest.Stream2JSONInputStream) ItemNotFoundException(org.eclipse.smarthome.core.items.ItemNotFoundException) Item(org.eclipse.smarthome.core.items.Item) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) MetadataRegistry(org.eclipse.smarthome.core.items.MetadataRegistry) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) ItemDTOMapper(org.eclipse.smarthome.core.items.dto.ItemDTOMapper) ApiResponse(io.swagger.annotations.ApiResponse) PUT(javax.ws.rs.PUT) Reference(org.osgi.service.component.annotations.Reference) EnrichedItemDTO(org.eclipse.smarthome.io.rest.core.item.EnrichedItemDTO) Stream2JSONInputStream(org.eclipse.smarthome.io.rest.Stream2JSONInputStream) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 53 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project smarthome by eclipse.

the class ThingResource method getAll.

@GET
@RolesAllowed({ Role.USER, Role.ADMIN })
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Get all available things.", response = EnrichedThingDTO.class, responseContainer = "Set")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = EnrichedThingDTO.class, responseContainer = "Set") })
public Response getAll(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language) {
    final Locale locale = localeService.getLocale(language);
    Stream<EnrichedThingDTO> thingStream = thingRegistry.stream().map(t -> convertToEnrichedThingDTO(t, locale)).distinct();
    return Response.ok(new Stream2JSONInputStream(thingStream)).build();
}
Also used : Locale(java.util.Locale) Channel(org.eclipse.smarthome.core.thing.Channel) StringUtils(org.apache.commons.lang.StringUtils) ItemFactory(org.eclipse.smarthome.core.items.ItemFactory) LocaleService(org.eclipse.smarthome.io.rest.LocaleService) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) URISyntaxException(java.net.URISyntaxException) Path(javax.ws.rs.Path) LoggerFactory(org.slf4j.LoggerFactory) ApiParam(io.swagger.annotations.ApiParam) ChannelUID(org.eclipse.smarthome.core.thing.ChannelUID) ApiOperation(io.swagger.annotations.ApiOperation) MediaType(javax.ws.rs.core.MediaType) QueryParam(javax.ws.rs.QueryParam) ChannelType(org.eclipse.smarthome.core.thing.type.ChannelType) Consumes(javax.ws.rs.Consumes) Locale(java.util.Locale) ConfigStatusService(org.eclipse.smarthome.config.core.status.ConfigStatusService) Map(java.util.Map) Thing(org.eclipse.smarthome.core.thing.Thing) DefaultValue(javax.ws.rs.DefaultValue) ConfigDescription(org.eclipse.smarthome.config.core.ConfigDescription) ManagedItemProvider(org.eclipse.smarthome.core.items.ManagedItemProvider) HeaderParam(javax.ws.rs.HeaderParam) BadRequestException(javax.ws.rs.BadRequestException) URI(java.net.URI) Configuration(org.eclipse.smarthome.config.core.Configuration) DELETE(javax.ws.rs.DELETE) RESTResource(org.eclipse.smarthome.io.rest.RESTResource) FirmwareRegistry(org.eclipse.smarthome.core.thing.firmware.FirmwareRegistry) Context(javax.ws.rs.core.Context) ConfigStatusInfo(org.eclipse.smarthome.config.core.status.ConfigStatusInfo) StrippedThingTypeDTO(org.eclipse.smarthome.core.thing.dto.StrippedThingTypeDTO) EnrichedThingDTO(org.eclipse.smarthome.io.rest.core.thing.EnrichedThingDTO) Collection(java.util.Collection) Set(java.util.Set) ThingManager(org.eclipse.smarthome.core.thing.ThingManager) ReferencePolicy(org.osgi.service.component.annotations.ReferencePolicy) ConfigDescriptionRegistry(org.eclipse.smarthome.config.core.ConfigDescriptionRegistry) ThingTypeRegistry(org.eclipse.smarthome.core.thing.type.ThingTypeRegistry) Role(org.eclipse.smarthome.core.auth.Role) ManagedItemChannelLinkProvider(org.eclipse.smarthome.core.thing.link.ManagedItemChannelLinkProvider) List(java.util.List) ChannelTypeUID(org.eclipse.smarthome.core.thing.type.ChannelTypeUID) Stream(java.util.stream.Stream) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) ManagedThingProvider(org.eclipse.smarthome.core.thing.ManagedThingProvider) FirmwareStatusDTO(org.eclipse.smarthome.core.thing.firmware.dto.FirmwareStatusDTO) Entry(java.util.Map.Entry) JSONResponse(org.eclipse.smarthome.io.rest.JSONResponse) BridgeType(org.eclipse.smarthome.core.thing.type.BridgeType) EnrichedThingDTOMapper(org.eclipse.smarthome.io.rest.core.thing.EnrichedThingDTOMapper) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) UriInfo(javax.ws.rs.core.UriInfo) ThingDTOMapper(org.eclipse.smarthome.core.thing.dto.ThingDTOMapper) ThingType(org.eclipse.smarthome.core.thing.type.ThingType) ThingHelper(org.eclipse.smarthome.core.thing.util.ThingHelper) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) ChannelDTOMapper(org.eclipse.smarthome.core.thing.dto.ChannelDTOMapper) PathParam(javax.ws.rs.PathParam) FirmwareDTO(org.eclipse.smarthome.core.thing.firmware.dto.FirmwareDTO) GET(javax.ws.rs.GET) HashMap(java.util.HashMap) Firmware(org.eclipse.smarthome.core.thing.binding.firmware.Firmware) ApiResponses(io.swagger.annotations.ApiResponses) ArrayList(java.util.ArrayList) Component(org.osgi.service.component.annotations.Component) FirmwareStatusInfo(org.eclipse.smarthome.core.thing.firmware.FirmwareStatusInfo) ChannelDTO(org.eclipse.smarthome.core.thing.dto.ChannelDTO) FirmwareUpdateService(org.eclipse.smarthome.core.thing.firmware.FirmwareUpdateService) Api(io.swagger.annotations.Api) Status(javax.ws.rs.core.Response.Status) ThingDTO(org.eclipse.smarthome.core.thing.dto.ThingDTO) POST(javax.ws.rs.POST) ConfigValidationException(org.eclipse.smarthome.config.core.validation.ConfigValidationException) ThingStatusInfoI18nLocalizationService(org.eclipse.smarthome.core.thing.i18n.ThingStatusInfoI18nLocalizationService) Logger(org.slf4j.Logger) IOException(java.io.IOException) ThingStatusInfo(org.eclipse.smarthome.core.thing.ThingStatusInfo) Stream2JSONInputStream(org.eclipse.smarthome.io.rest.Stream2JSONInputStream) ItemRegistry(org.eclipse.smarthome.core.items.ItemRegistry) ThingRegistry(org.eclipse.smarthome.core.thing.ThingRegistry) ReferenceCardinality(org.osgi.service.component.annotations.ReferenceCardinality) ChannelTypeRegistry(org.eclipse.smarthome.core.thing.type.ChannelTypeRegistry) ApiResponse(io.swagger.annotations.ApiResponse) ItemChannelLinkRegistry(org.eclipse.smarthome.core.thing.link.ItemChannelLinkRegistry) PUT(javax.ws.rs.PUT) Reference(org.osgi.service.component.annotations.Reference) Collections(java.util.Collections) ConfigUtil(org.eclipse.smarthome.config.core.ConfigUtil) EnrichedThingDTO(org.eclipse.smarthome.io.rest.core.thing.EnrichedThingDTO) Stream2JSONInputStream(org.eclipse.smarthome.io.rest.Stream2JSONInputStream) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 54 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project smarthome by eclipse.

the class ThingResource method create.

/**
 * create a new Thing
 *
 * @param thingBean
 * @return Response holding the newly created Thing or error information
 */
@POST
@RolesAllowed({ Role.ADMIN })
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Creates a new thing and adds it to the registry.")
@ApiResponses(value = { @ApiResponse(code = 201, message = "Created", response = String.class), @ApiResponse(code = 400, message = "A uid must be provided, if no binding can create a thing of this type."), @ApiResponse(code = 409, message = "A thing with the same uid already exists.") })
public Response create(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) @ApiParam(value = "language") String language, @ApiParam(value = "thing data", required = true) ThingDTO thingBean) {
    final Locale locale = localeService.getLocale(language);
    ThingUID thingUID = thingBean.UID == null ? null : new ThingUID(thingBean.UID);
    ThingTypeUID thingTypeUID = new ThingTypeUID(thingBean.thingTypeUID);
    if (thingUID != null) {
        // check if a thing with this UID already exists
        Thing thing = thingRegistry.get(thingUID);
        if (thing != null) {
            // report a conflict
            return getThingResponse(Status.CONFLICT, thing, locale, "Thing " + thingUID.toString() + " already exists!");
        }
    }
    ThingUID bridgeUID = null;
    if (thingBean.bridgeUID != null) {
        bridgeUID = new ThingUID(thingBean.bridgeUID);
    }
    // turn the ThingDTO's configuration into a Configuration
    Configuration configuration = new Configuration(normalizeConfiguration(thingBean.configuration, thingTypeUID, thingUID));
    normalizeChannels(thingBean, thingUID);
    Thing thing = thingRegistry.createThingOfType(thingTypeUID, thingUID, bridgeUID, thingBean.label, configuration);
    if (thing != null) {
        if (thingBean.properties != null) {
            for (Entry<String, String> entry : thingBean.properties.entrySet()) {
                thing.setProperty(entry.getKey(), entry.getValue());
            }
        }
        if (thingBean.channels != null) {
            List<Channel> channels = new ArrayList<>();
            for (ChannelDTO channelDTO : thingBean.channels) {
                channels.add(ChannelDTOMapper.map(channelDTO));
            }
            ThingHelper.addChannelsToThing(thing, channels);
        }
        if (thingBean.location != null) {
            thing.setLocation(thingBean.location);
        }
    } else if (thingUID != null) {
        // if there wasn't any ThingFactory capable of creating the thing,
        // we create the Thing exactly the way we received it, i.e. we
        // cannot take its thing type into account for automatically
        // populating channels and properties.
        thing = ThingDTOMapper.map(thingBean, thingTypeRegistry.getThingType(thingTypeUID) instanceof BridgeType);
    } else {
        return getThingResponse(Status.BAD_REQUEST, thing, locale, "A UID must be provided, since no binding can create the thing!");
    }
    thingRegistry.add(thing);
    return getThingResponse(Status.CREATED, thing, locale, null);
}
Also used : Locale(java.util.Locale) Configuration(org.eclipse.smarthome.config.core.Configuration) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Channel(org.eclipse.smarthome.core.thing.Channel) ChannelDTO(org.eclipse.smarthome.core.thing.dto.ChannelDTO) ArrayList(java.util.ArrayList) ThingTypeUID(org.eclipse.smarthome.core.thing.ThingTypeUID) Thing(org.eclipse.smarthome.core.thing.Thing) BridgeType(org.eclipse.smarthome.core.thing.type.BridgeType) RolesAllowed(javax.annotation.security.RolesAllowed) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Example 55 with RolesAllowed

use of javax.annotation.security.RolesAllowed in project smarthome by eclipse.

the class ThingResource method getConfigStatus.

@GET
@RolesAllowed({ Role.USER, Role.ADMIN })
@Path("/{thingUID}/config/status")
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Gets thing's config status.")
@ApiResponses(value = { @ApiResponse(code = 200, message = "OK", response = String.class), @ApiResponse(code = 404, message = "Thing not found.") })
public Response getConfigStatus(@HeaderParam(HttpHeaders.ACCEPT_LANGUAGE) String language, @PathParam("thingUID") @ApiParam(value = "thing") String thingUID) throws IOException {
    ThingUID thingUIDObject = new ThingUID(thingUID);
    // Check if the Thing exists, 404 if not
    Thing thing = thingRegistry.get(thingUIDObject);
    if (null == thing) {
        logger.info("Received HTTP GET request for thing config status at '{}' for the unknown thing '{}'.", uriInfo.getPath(), thingUID);
        return getThingNotFoundResponse(thingUID);
    }
    ConfigStatusInfo info = configStatusService.getConfigStatus(thingUID, localeService.getLocale(language));
    if (info != null) {
        return Response.ok().entity(info.getConfigStatusMessages()).build();
    }
    return Response.ok().entity(Collections.EMPTY_SET).build();
}
Also used : ConfigStatusInfo(org.eclipse.smarthome.config.core.status.ConfigStatusInfo) ThingUID(org.eclipse.smarthome.core.thing.ThingUID) Thing(org.eclipse.smarthome.core.thing.Thing) Path(javax.ws.rs.Path) RolesAllowed(javax.annotation.security.RolesAllowed) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ApiOperation(io.swagger.annotations.ApiOperation) ApiResponses(io.swagger.annotations.ApiResponses)

Aggregations

RolesAllowed (javax.annotation.security.RolesAllowed)191 Path (javax.ws.rs.Path)127 Produces (javax.ws.rs.Produces)110 Consumes (javax.ws.rs.Consumes)55 GET (javax.ws.rs.GET)54 POST (javax.ws.rs.POST)40 PUT (javax.ws.rs.PUT)35 HashMap (java.util.HashMap)34 ArrayList (java.util.ArrayList)32 IOException (java.io.IOException)30 ApiOperation (io.swagger.annotations.ApiOperation)29 ApiResponses (io.swagger.annotations.ApiResponses)29 Response (javax.ws.rs.core.Response)28 Adapter (nl.nn.adapterframework.core.Adapter)21 DELETE (javax.ws.rs.DELETE)19 WebApplicationException (org.rembx.jeeshop.rest.WebApplicationException)19 LinkedHashMap (java.util.LinkedHashMap)16 Locale (java.util.Locale)16 Map (java.util.Map)12 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)12