// WS-POS version 1.3 参照実装
// 
// 実装 version 0.6 

using System;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.IO;

namespace WSPOSInitiative.Common
{
    [ServiceContract]
    public interface IFileHost
    {
        [OperationContract, WebGet(UriTemplate = "{filename}")]
        Stream Files(string filename);
    }

    public class FileHostService : IFileHost
    {

        public Stream Files(string filename)
        {
            if (!filename.ToLower().Equals("clientaccesspolicy.xml"))
            {
                throw (new ApplicationException("only clientaccesspolicy.xml allowed"));
            }

            Stream stream = (Stream)new FileStream(filename, FileMode.Open);

            int extIndex = filename.LastIndexOf(".");
            string extension = filename.Substring(extIndex, filename.Length - extIndex);
            switch (extension)
            {
                case ".html":
                case ".htm":
                    WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
                    break;
                case ".xap":
                    WebOperationContext.Current.OutgoingResponse.ContentType = "application/x-silverlight-2-b2";
                    break;
                case ".xml":
                    WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
                    break;
                default:
                    throw (new ApplicationException("File type not supported"));
            }

            return stream;
        }

    }


}
