ファイルのカスタムダウンロード例
Apex を使用して、ダウンロードが試行されたときのファイルの動作をカスタマイズできます。以下の例は、1 つのファイルがダウンロードされる場合を想定しています。ファイルのダウンロードのカスタマイズは、API バージョン 39.0 以降で使用できます。
例
この例では、ダウンロードで一部のユーザーが IRM 制御を通過する必要があるシステムを示します。ファイルのダウンロードが許可され、ユーザー ID が 005xx のすべてのデータの編集 (MAD) ユーザーの場合、次のようになります。
1// Allow customization of the content Download experience
2public class ContentDownloadHandlerFactoryImpl implements Sfc.ContentDownloadHandlerFactory {
3
4public Sfc.ContentDownloadHandler getContentDownloadHandler(List<ID> ids, Sfc.ContentDownloadContext context) {
5 Sfc.ContentDownloadHandler contentDownloadHandler = new Sfc.ContentDownloadHandler();
6
7 if(UserInfo.getUserId() == '005xx') {
8 contentDownloadHandler.isDownloadAllowed = true;
9 return contentDownloadHandler;
10 }
11
12 contentDownloadHandler.isDownloadAllowed = false;
13 contentDownloadHandler.downloadErrorMessage = 'This file needs to be IRM controlled. You're not allowed to download it';
14 contentDownloadHandler.redirectUrl ='/apex/IRMControl?Id='+ids.get(0);
15 return contentDownloadHandler;
16}
17}この例では、IRMControl は IRM システムからファイルをダウンロードするリンクを表示するために作成された Visualforce ページです。IRM システムをコールするこのページのコントローラーが必要です。ファイルが処理されるとき、制御されている場合にファイルをダウンロードするエンドポイントが提供されます。IRM システムは、sObject API を使用してこの ContentVersion の VersionData を取得します。そのため、IRM システムは VersionID を必要とし、MAD ユーザーを使用して VersionData を取得する必要があります。
IRM システムは http://irmsystem にあり、クエリパラメーターとして VersionID を要求します。IRM システムは、downloadEndpoint 値にダウンロードエンドポイントが含まれた JSON 応答を返します。
1public class IRMController {
2
3private String downloadEndpoint;
4
5public IRMController() {
6 downloadEndpoint = '';
7}
8
9public void applyIrmControl() {
10 String versionId = ApexPages.currentPage().getParameters().get('id');
11 Http h = new Http();
12
13 //Instantiate a new HTTP request, specify the method (GET) as well as the endpoint
14 HttpRequest req = new HttpRequest();
15 req.setEndpoint('http://irmsystem?versionId=' + versionId);
16 req.setMethod('GET');
17
18 // Send the request, and retrieve a response
19 HttpResponse r = h.send(req);
20 JSONParser parser = JSON.createParser(r.getBody());
21 while (parser.nextToken() != null) {
22 if ((parser.getCurrentToken() == JSONToken.FIELD_NAME) &&
23 (parser.getText() == 'downloadEndpoint')) {
24 parser.nextToken();
25 downloadEndpoint = parser.getText();
26 break;
27 }
28 }
29}
30
31public String getDownloadEndpoint() {
32 return downloadEndpoint;
33}
34
35}例
次の例では、ContentDownloadHandlerFactory インターフェースを実装し、モバイルデバイスにファイルをダウンロードできないようにするダウンロードハンドラーを返すクラスを作成します。
1// Allow customization of the content Download experience
2public class ContentDownloadHandlerFactoryImpl implements Sfc.ContentDownloadHandlerFactory {
3
4public Sfc.ContentDownloadHandler getContentDownloadHandler(List<ID> ids, Sfc.ContentDownloadContext context) {
5 Sfc.ContentDownloadHandler contentDownloadHandler = new Sfc.ContentDownloadHandler();
6
7 if(context == Sfc.ContentDownloadContext.MOBILE) {
8 contentDownloadHandler.isDownloadAllowed = false;
9 contentDownloadHandler.downloadErrorMessage = 'Downloading a file from a mobile device isn't allowed.';
10 return contentDownloadHandler;
11 }
12 contentDownloadHandler.isDownloadAllowed = true;
13 return contentDownloadHandler;
14}例
モバイルデバイスからファイルをダウンロードできないようにし、ファイルは IRM 制御を通過する必要があるようにすることもできます。
1// Allow customization of the content Download experience
2public class ContentDownloadHandlerFactoryImpl implements Sfc.ContentDownloadHandlerFactory {
3
4public Sfc.ContentDownloadHandler getContentDownloadHandler(List<ID> ids, Sfc.ContentDownloadContext context) {
5 Sfc.ContentDownloadHandler contentDownloadHandler = new Sfc.ContentDownloadHandler();
6
7 if(UserInfo.getUserId() == '005xx000001SvogAAC') {
8 contentDownloadHandler.isDownloadAllowed = true;
9 return contentDownloadHandler;
10 }
11 if(context == Sfc.ContentDownloadContext.MOBILE) {
12 contentDownloadHandler.isDownloadAllowed = false;
13 contentDownloadHandler.downloadErrorMessage = 'Downloading a file from a mobile device isn't allowed.';
14 return contentDownloadHandler;
15 }
16
17 contentDownloadHandler.isDownloadAllowed = false;
18 contentDownloadHandler.downloadErrorMessage = 'This file needs to be IRM controlled. You're not allowed to download it';
19 contentDownloadHandler.redirectUrl ='/apex/IRMControl?Id='+id.get(0);
20 return contentDownloadHandler;
21}
22}