Cookie クラス
Cookie クラスにより、Apex を使用して Salesforce サイトの Cookie にアクセスできます。
名前空間
使用方法
PageReference クラスの setCookies メソッドを使用して、ページに Cookie を添付します。
Cookie クラスを使用する場合は、次の制限に留意してください。
- Cookie クラスには、Salesforce API バージョン 19 以降を使用して保存されている Apex を使用することでのみアクセスできます。
- Salesforce サイトドメインごとに設定できる Cookie の最大数はブラウザーにより異なります。新しいブラウザーは古いブラウザーより高い制限が設定されています。
- Cookie は名前および属性を含め 4K 未満である必要があります。
- Visualforce ページの最大ヘッダーサイズは、Cookie を含めて 8,192 バイトです。
サイトについての詳細は、Salesforce オンラインヘルプの「Salesforce サイト」を参照してください。
例
次の例では、CookieController クラスを作成します。このクラスは Visualforce ページ (下記マークアップを参照) を使用して、ユーザーにページが表示されるたびにカウンターが更新されます。ページへのアクセス回数が Cookie に保存されます。
1// A Visualforce controller class that creates a cookie
2// used to keep track of how often a user displays a page
3public class CookieController {
4
5 public CookieController() {
6 Cookie counter = ApexPages.currentPage().getCookies().get('counter');
7
8 // If this is the first time the user is accessing the page,
9 // create a new cookie with name 'counter', an initial value of '1',
10 // path 'null', maxAge '-1', and isSecure 'true'.
11 if (counter == null) {
12 counter = new Cookie('counter','1',null,-1,true);
13 } else {
14 // If this isn't the first time the user is accessing the page
15 // create a new cookie, incrementing the value of the original count by 1
16 Integer count = Integer.valueOf(counter.getValue());
17 counter = new Cookie('counter', String.valueOf(count+1),null,-1,true);
18 }
19
20 // Set the new cookie for the page
21 ApexPages.currentPage().setCookies(new Cookie[]{counter});
22 }
23
24 // This method is used by the Visualforce action {!count} to display the current
25 // value of the number of times a user had displayed a page.
26 // This value is stored in the cookie.
27 public String getCount() {
28 Cookie counter = ApexPages.currentPage().getCookies().get('counter');
29 if(counter == null) {
30 return '0';
31 }
32 return counter.getValue();
33 }
34}1// Test class for the Visualforce controller
2@isTest
3private class CookieControllerTest {
4 // Test method for verifying the positive test case
5 static testMethod void testCounter() {
6 //first page view
7 CookieController controller = new CookieController();
8 System.assert(controller.getCount() == '1');
9
10 //second page view
11 controller = new CookieController();
12 System.assert(controller.getCount() == '2');
13 }
14}次は、上記の CookieController Apex コントローラーを使用する Visualforce ページです。アクション {!count} では、上記のコントローラーで getCount メソッドをコールします。
1<apex:page controller="CookieController">
2You have seen this page {!count} times
3</apex:page>Cookie コンストラクター
Cookie のコンストラクターは次のとおりです。
Cookie(name, value, path, maxAge, isSecure)
指定された名前、値、パス、有効期間、およびセキュアな設定を使用して、Cookie クラスの新しいインスタンスを作成します。
署名
public Cookie(String name, String value, String path, Integer maxAge, Boolean isSecure)
Cookie(name, value, path, maxAge, isSecure, SameSite)
指定された名前、値、パス、有効期間、およびセキュリティとクロスドメインの動作の設定を使用して、Cookie クラスの新しいインスタンスを作成します。
署名
public Cookie(String name, String value, String path, Integer maxAge, Boolean isSecure, String SameSite)
パラメーター
- name
- 型: String
- Cookie 名。null にはできません。
- value
- 型: String
- Cookie データ (例: セッション ID)。
- path
- 型: String
- Cookie の取得元のパス。
- maxAge
- 型: Integer
- Cookie の有効期間を示す秒単位の数字。0 より小さく設定すると、セッション Cookie が発行されます。0 を設定すると、Cookie が削除されます。
- isSecure
- 型: Boolean
- Cookie が HTTPS でのみアクセス可能か (true)、否か (false) を示す値。
- SameSite
- 型: String
- Cookie の SameSite 属性はクロスドメインの動作を制御します。有効な値は None、Lax、Strict です。Chrome 80 リリースの後、SameSite の値が None である Cookie も、None; Secure の値を設定して、安全としてマークする必要があります。
Cookie メソッド
Cookie のメソッドは次のとおりです。すべてインスタンスメソッドです。
getMaxAge()
Cookie の有効期間を示す秒単位の数字が返されます。< 0 を設定すると、セッション Cookie が発行されます。0 を設定すると、Cookie は削除されます。
署名
public Integer getMaxAge()
戻り値
型: Integer
isSecure()
Cookie が HTTPS でのみアクセス可能な場合、true を返します。それ以外の場合は、false を返します。
署名
public Boolean isSecure()
戻り値
型: Boolean