import StorageErrorFactory from "../../queue/errors/StorageErrorFactory" ; import QueueStorageContext from "../context/QueueStorageContext" ; import * as Models from "../generated/artifacts/models" ; import Context from "../generated/Context" ; import IQueueHandler from "../generated/handlers/IQueueHandler" ; import { QueueModel } from "../persistence/IQueueMetadataStore" ; import { HeaderConstants , QUEUE_API_VERSION , QUEUE_SERVICE_PERMISSION } from "../utils/constants" ; import BaseHandler from "./BaseHandler" ; export class implements IQueueHandler export default class QueueHandler extends BaseHandler implements IQueueHandler { param { Models . QueueCreateOptionalParams } options param { Context } context returns { Promise < Models . QueueCreateResponse > } memberof public async create ( options , context ) { const queueCtx = new QueueStorageContext ( context ) ; const accountName = queueCtx . account ! ; const queueName = queueCtx . queue ! ; const req = context . request ! as any ; const rawHeaders = req . req . rawHeaders ; const metadata = this . parseMetadata ( options . metadata , rawHeaders ) ; const queue = { accountName , name : queueName , metadata } ; const statusCode = await this . metadataStore . createQueue ( queue , context ) ; const response = { date : context . startTime , requestId : queueCtx . contextID , statusCode , version : QUEUE_API_VERSION , clientRequestId : options . requestId } ; return response ; } param { Models . QueueDeleteMethodOptionalParams } options param { Context } context returns { Promise < Models . QueueDeleteResponse > } memberof public async delete ( options , context ) { const queueCtx = new QueueStorageContext ( context ) ; const accountName = queueCtx . account ! ; const queueName = queueCtx . queue ! ; await this . metadataStore . deleteQueue ( accountName , queueName , context ) ; const response = { date : context . startTime , requestId : context . contextID , statusCode : 204 , version : QUEUE_API_VERSION , clientRequestId : options . requestId } ; return response ; } param { Models . QueueGetPropertiesOptionalParams } options param { Context } context returns { Promise < Models . QueueGetPropertiesResponse > } memberof public async getProperties ( options , context ) { const queueCtx = new QueueStorageContext ( context ) ; const accountName = queueCtx . account ! ; const queueName = queueCtx . queue ! ; const queue = await this . metadataStore . getQueue ( accountName , queueName , context ) ; const response = { approximateMessagesCount : await this . metadataStore . getMessagesCount ( queue . accountName , queue . name ) , metadata : queue . metadata , date : context . startTime , requestId : context . contextID , statusCode : 200 , version : QUEUE_API_VERSION , clientRequestId : options . requestId } ; return response ; } param { Models . QueueGetPropertiesWithHeadOptionalParams } options param { Context } context returns { Promise < Models . QueueGetPropertiesWithHeadResponse > } memberof public async getPropertiesWithHead ( options , context ) { return this . getProperties ( options , context ) ; } param { Models . QueueSetMetadataOptionalParams } options param { Context } context returns { Promise < Models . QueueSetMetadataResponse > } memberof public async setMetadata ( options , context ) { const queueCtx = new QueueStorageContext ( context ) ; const accountName = queueCtx . account ! ; const queueName = queueCtx . queue ! ; const req = context . request ! as any ; const rawHeaders = req . req . rawHeaders ; const metadata = this . parseMetadata ( options . metadata , rawHeaders ) ; await this . metadataStore . setQueueMetadata ( accountName , queueName , metadata , context ) ; const response = { date : context . startTime , requestId : context . contextID , statusCode : 204 , version : QUEUE_API_VERSION , clientRequestId : options . requestId } ; return response ; } param { Models . QueueGetAccessPolicyOptionalParams } options param { Context } context returns { Promise < Models . QueueGetAccessPolicyResponse > } memberof public async getAccessPolicy ( options , context ) { const queueCtx = new QueueStorageContext ( context ) ; const accountName = queueCtx . account ! ; const queueName = queueCtx . queue ! ; const queue = await this . metadataStore . getQueue ( accountName , queueName , context ) ; const response = [ ] ; const responseArray = response as Models . SignedIdentifier [ ] ; const responseObject = response as Models . QueueGetAccessPolicyHeaders & { statusCode : 200 ; } ; if ( queue . queueAcl !== undefined ) { responseArray . push ( ... queue . queueAcl ) ; } responseObject . date = context . startTime ; responseObject . requestId = context . contextID ; responseObject . version = QUEUE_API_VERSION ; responseObject . statusCode = 200 ; responseObject . clientRequestId = options . requestId ; return response ; } param { Models . QueueGetAccessPolicyWithHeadOptionalParams } options param { Context } context returns { Promise < Models . QueueGetAccessPolicyWithHeadResponse > } memberof public async getAccessPolicyWithHead ( options , context ) { return this . getAccessPolicy ( options , context ) ; } param { Models . QueueSetAccessPolicyOptionalParams } options param { Context } context returns { Promise < Models . QueueSetAccessPolicyResponse > } memberof public async setAccessPolicy ( options , context ) { const queueCtx = new QueueStorageContext ( context ) ; const accountName = queueCtx . account ! ; const queueName = queueCtx . queue ! ; if ( options . queueAcl !== undefined ) { if ( options . queueAcl . length > 5 ) { throw StorageErrorFactory . getInvalidXmlDocument ( context . contextID ) ; } for ( const acl of options . queueAcl ) { const permission = acl . accessPolicy . permission ; for ( const item of permission ) { if ( ! QUEUE_SERVICE_PERMISSION . includes ( item ) ) { throw StorageErrorFactory . getInvalidXmlDocument ( context . contextID ) ; } } } } await this . metadataStore . setQueueACL ( accountName , queueName , options . queueAcl , context ) ; const response = { date : context . startTime , requestId : context . contextID , version : QUEUE_API_VERSION , statusCode : 204 , clientRequestId : options . requestId } ; return response ; } private param { ( { [ propertyName : string ] : string } | undefined ) } reqMetadata param { { [ id : string ] : string } } headers returns memberof private parseMetadata ( reqMetadata , headers ) { if ( reqMetadata === undefined ) { return undefined ; } const metaPrefix = HeaderConstants . X_MS_META ; const metadata = { } ; for ( const item in reqMetadata ) { if ( reqMetadata . hasOwnProperty ( item ) ) { for ( const id in headers ) { if ( headers . hasOwnProperty ( id ) ) { const name = headers [ id ] ; if ( metaPrefix + item === name . toLowerCase ( ) ) { metadata [ name . substr ( metaPrefix . length ) ] = reqMetadata [ item ] ; break ; } } } } } return metadata ; } }