原文出處:http://www.cnblogs.com/poissonnotes/p/3267190.html
還可參考:https://www.cnblogs.com/majiang/p/3672976.html?utm_source=tuicool
介紹Office Web Apps與其他系統的關系圖,
從上述圖中,可知實際上Office Web Apps也是可以接入自己開發的系統的。下面介紹一下整合Office Web Apps的一些理論知識。
這里在簡單描述一下原理吧:office web apps(owas)扮演者一個客服端,它會訪問我們asp.net 站點的文件然后呈現出來。而我們常用的API主要有如下3個:
GET api/wopi/files/{name}?access_token={access_token}
GET api/wopi/files/{name}/contents?access_token={access_token}
POST api/wopi/files/{name}/contents?access_token={access_token}
至於每個API做什么 ,第一個是owas 檢查文件,傳遞的信息是json數據格式,第二個是owas獲取文件流,第三個是owas post的文件流(保存修改文件)
要想讓自己的系統與Office Web Apps整合就一定要清楚一些概念,首先要理解什么是”WOPI”。
WOPI的英文全稱是“Web Application Open Platform Interface”,中文名為“Web應用程序開放平台接口協議”。
WOPI協議提供一系列基於web方式的,使文檔能在Office Web Apps中查看與編輯的接口服務(Web Service)。
只要web application按照標准,實現了WOPI的接口,那么就可以調用Office Web Apps。例子很多,比如SharePoint,Exchange,SkyDriver,Dropbox集成Office Web Apps。
如果自己做的web應用也實現了相應接口,也是可以調用Office Web Apps的。實現文檔的在線編輯查看。
這樣比市面上的一些基於ActiveX的在線Office產品有很大的優勢。
首先Office Web Apps是基於網頁技術,所以是跨平台的,可以在iOS,安卓,WP及PC使用,實現多屏一體。
其次Office Web Apps實現了桌面Office的大部分功能,能在客戶機沒有安裝Office的情況下,實現雲端上的文檔編輯查看。
下面介紹的內容都是基於http協議下的,https也是類似的。
在WOPI結構中,
我們把存放Office文檔的web應用叫WOPI Host或者WOPI Server。
把查看編輯操作Office文檔的web應用叫WOPI Client或者叫WOPI applications。
所以,Office Web Apps充當的就是WOPI Client的角色。
SharePoint,Exchange,自己開發的文檔管理系統充當的就是WOPI Host的角色。
下圖為瀏覽器,server,client三者的請求順序及關系:
從上圖可知,WOPI Client 向WOPI Server發送了兩次請求
1. Tell me about the file
2. Give me the file
所以WOPI client至少要提供兩個Web服務。
1. 一個是CheckFileInfo服務
此服務返回的是請求文件的基本信息,WOPI Host以json方式返回給WOPI Client.
HTTP://server/<...>/wopi*/files/<id>?access_token=<token>
此服務返回的json格式類似為:
{ "BaseFileName": "Sample Document.docx", "OwnerId": "tylerbutler", "Size": 300519, "SHA256": "+17lwXXN0TMwtVJVs4Ll+gDHEIO06l+hXK6zWTUiYms=", "Version": "GIYDCMRNGEYC2MJREAZDCORQGA5DKNZOGIZTQMBQGAVTAMB2GAYA====" }
Json中至少要包括五個屬性:BaseFileName, OwnerId, Size, SHA256, 和 Version
BaseFileName: 文件名。
Size: 文件大小,以bytes為單位。
SHA256: 文件的256位bit的SHA-2編碼散列內容。
Version: 文件版本號,文件如果被編輯,版本號也要跟着改變。
更多參數介紹請參考:http://msdn.microsoft.com/en-us/library/hh622920(v=office.12).aspx
2. 一個是GetFile服務
此服務返回的是請求文件的內容,WOPI host以數據流的方式返回給WOPI Client.
服務URI格式一般為
HTTP://server/<...>/wopi*/files/<id>/contents?access_token=<token>
注意:CheckFileInfo與GetFile服務的URI格式只差了一個/contents,其他地方的格式是沒有不同的。這么做是為了讓WOPI client可以通過CheckFileInfo服務URI推導出GetFile服務的URI,千萬不要別出心裁,寫出的服務URI格式破壞了這層關系。
在上述URI格式中,都有一個access_taken身份驗證令牌。這個身份驗證令牌是必須要有的,WOPI client會把此令牌回發給WOPI Host,由WOPI Host驗證當前用戶對當前文件的權限。所以實際上Office Web Apps根本不涉及文檔的權限管理。
我們在WOPI client上打開一個Office文檔的url地址類似如下:
http://wopi-app-server.contoso.com/wv/wordviewerframe.aspx?WOPISrc= http%3A%2F%2Fmy-wopi-host%2Flocal%2Fwopi %2Ffiles%2F1-Sample%2520Document.docx&access_token= dc172034-c6f9-4a43-bc3f-d80dd93c1de1
這個里面有兩個傳遞參數:WOPISrc和access_token
WOPISrc參數的內容為:http://my-wopi-host/local/wopi/files/1-Sample%20Document.docx
實際上這個是WOPI Host上的CheckFileInfo服務地址。
WOPI client會通過這個地址加上access_token從WOPI host上獲取到1-Sample%20Document.docx文件的信息;
並且通過這個地址推導出WOPI Host上的GetFile服務地址,通過GetFile服務獲取到1-Sample%20Document.docx文件的內容。
WOPI host上判斷什么類型的文件應該怎么用WOPI client打開,WOPI client會提供一個xml文件給WOPI host,這份xml文件叫WOPI Discovery。格式類似如下:
<?xml version="1.0" encoding="utf-8"?> <wopi-discovery> <net-zone name="external-https"> <app name="Word" favIconUrl="https://wopi-app-server.contoso.com/wv/ resources/1033/FavIcon_Word.ico" checkLicense="true"> <action name="view" ext="doc" default="true" urlsrc="https://wopi-app-server.contoso.com/ wv/wordviewerframe.aspx? <ui=UI_LLCC&><rs=DC_LLCC&><showpagestats=PERFSTATS&>"/> <action name="view" ext="docm" default="true" urlsrc="https://wopi-app-server.contoso.com/ wv/wordviewerframe.aspx? <ui=UI_LLCC&><rs=DC_LLCC&><showpagestats=PERFSTATS&>"/> …… </app> …… </net-zone> </wopi-discovery>
如上所述,打開doc文件,應該使用https://wopi-app-server.contoso.com/ wv/wordviewerframe.aspx的url打開。
WOPI host應該獲取這份文件一次,以后打開什么類型的文件,調用什么url自己判斷。
WOPI項目的創建
首先用vs2012創建一個mvc4的程序。如圖:
從上一篇我們可以知道,WOPI通訊主要通過兩個服務:
一個是GetFile服務。
所以下面我們主要介紹這兩個服務的創建。
1. 首先創建CheckFileInfo服務:
我們先確定這個服務的路由地址
設置為:http://<ServerName>/files/<filename>?access_token=<token>
修改App_Start文件夾下面的WebApiConfig.cs文件。
插入下列代碼:
config.Routes.MapHttpRoute(
name: "FileInfo",
routeTemplate: "wopi/files/{name}",
defaults: new { controller = "files", action = "GetFileInfo" }
);
如圖所示
創建一個名稱為files的Controller,
設置為空API控制器:
之所以我們不用平常的MVC控制器,而選API控制器,是因為我們做的是服務,來返回信息,所以要換成ApiController。
這個服務要返回的是json,屬性包括為
BaseFileName
OwerId
Size
SHA256
Version
所以我們要創建一個model,包含上述屬性
如下圖:
在上述的路由器規則中,action用的是GetFileInfo方法,所以要在FileController規則中寫一個GetFileInfo方法,這個方法返回CheckFileInfo類型。
public CheckFileInfo GetFileInfo(string name, string access_token)
{
string _access_token = access_token;
var file = HostingEnvironment.MapPath("~/App_Data/" + name);//從硬盤中獲取name文件
FileInfo info = new FileInfo(file);
var json = new CheckFileInfo
{
BaseFileName = info.Name ,//"test.docx",
OwnerId = "admin",
Size = info.Length,
SHA256 = "+17lwXXN0TMwtVJVs4Ll+gDHEIO06l+hXK6zWTUiYms=",
Version = "GIYDCMRNGEYC2MJREAZDCORQGA5DKNZOGIZTQMBQGAVTAMB2GAYA===="
};
return json;
}
如下圖
我們訪問一下這個地址:
http://192.9.206.52:1407/wopi/files/test.docx?access_token=06l+hXK6zWTUi
這個192.9.206.52是我的本機地址。
得到下列結果:
證明這個服務制作成功。
2.然后再來制作GetFile服務。
因為GetFileInfo的URI地址
http://<ServerName>/files/<filename>?access_token=<token>
所以GetFile地址應該比其多一個/Contents,所以為
http://<ServerName>/files/<filename>/Contents?access_token=<token>
設置它的路由地址
config.Routes.MapHttpRoute(
name: "Contents",
routeTemplate: "wopi/files/{name}/contents",
defaults: new { controller = "files", action = "GetFile" }
);
如下圖:
GetFile這個服務返回的應該是數據流,所以返回的類型應該是HttpResponseMessage類型。
從硬盤中獲取一個doc文件,轉換為Stream類型,代碼如下:
public HttpResponseMessage GetFile(string name, string access_token)
{
try
{
string _access_token = access_token;
var file = HostingEnvironment.MapPath("~/App_Data/" + name);//name是文件名
var rv = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(file, FileMode.Open, FileAccess.Read);
rv.Content = new StreamContent(stream);
rv.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
return rv;
}
catch (Exception ex)
{
var rv = new HttpResponseMessage(HttpStatusCode.InternalServerError);
var stream = new MemoryStream(UTF8Encoding.Default.GetBytes(ex.Message ?? ""));
rv.Content = new StreamContent(stream);
return rv;
}
}
如下圖:
至此,兩個服務制作完畢。
可以訪問下列地址查看效果,
其中
192.9.206.50為OWA的機器地址,
192.9.206.52為本機的地址。
這個URL地址帶了兩個參數
分別為WOPISrc,值為http://192.9.206.52:1407/wopi/files/test.docx
Access_token,值為06l+hXK6zWTUi
這兩個參數的意思,我已經在以前的博文中《如何整合Office Web Apps至自己開發的系統(一)》說過了。
在這個例子中,access_token我是隨便取的,並且在代碼中也沒有對這個令牌進行驗證。
確保兩台機器的相應端口能互相訪問。
訪問得到的結果如下:
怎么會訪問出錯呢?
翻了很久資料,發現有老外也遇到過類似這種問題:
I write this message because on actually working on this WOPI protocol. I try to build a WOPI host. I think i'm almost finish the "view" action. But i got some problems with the CheckFileInfo (JSON) or GetFile (/content). For me everything is well fonctionning, but the WAC doesn't work just after it call my JSON. I really dont know why.. I observed all the interactions between SharePoint and WAC, to show what is different with mine host. But i think i need some help now. Does anyone can try to give me some hint ? I checked everythings (Correlation-ID, JSON, access-token) ...
別人的回答是讓他考慮一下是不是SHA散列算法的問題:
You might also double-check that your SHA hashes are being calculated correctly - this can cause some problems.
並給了一個網站地址:www.tylerbutler.com/.../base64-encoded-sha256-hashes
那就按照提示把散列算法加上去,
代碼如下:
var file = HostingEnvironment.MapPath("~/App_Data/" + name);//從硬盤中獲取name文件
FileInfo info = new FileInfo(file);
var hasher = SHA256.Create();
byte[] hashValue;
using (Stream s = File.OpenRead(file))
{
hashValue = hasher.ComputeHash(s);
}
string sha256 = Convert.ToBase64String(hashValue);
如下圖:
再次運行,OK,大功告成
其實按照上述步驟,就可以在自己的系統中調用Office Web Apps的查看功能了