Java tutorial
/** * personium.io * Copyright 2014 FUJITSU LIMITED * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.fujitsu.dc.client; import java.io.UnsupportedEncodingException; import java.util.HashMap; import org.apache.commons.codec.DecoderException; import org.apache.commons.codec.net.URLCodec; import org.apache.http.client.HttpClient; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; import com.fujitsu.dc.client.http.CacheMap; ///** // * personium.io. // */ /** * It creates a new object of DcContext. This class is the Data cloud context used as the package for all the files. */ public class DcContext { // /** URL. */ /** Base URL. */ private String baseUrl; // /** ??CellName. */ /** Cell Name. */ private String cellName; // /** ??Box?DataSchemaURI. */ /** DataSchemaURI of the current Box. */ private String schema; // /** ??BoxName. */ /** Box Name. */ private String boxName; // /** . */ /** Client Token. */ private String clientToken; // /** ???????API??. */ /** Version of the API that is obtained from the server response. */ private String serverVersion = null; // /** . */ /** Default Headers. */ HashMap<String, String> defaultHeaders = new HashMap<String, String>(); // /** // * ??. // */ /** Header that specifies the version information. */ public static final String DC_VERSION = "X-Dc-Version"; // /** // * ?getter. // * @return // */ /** * This method returns the client token value. * @return clientToken */ public String getClientToken() { return clientToken; } // /** ????. */ /** DaoConfig object managing a customizable information. */ private DaoConfig config; // /** . */ /** Cache for class. */ private CacheMap cacheMap; // /** . */ /** Operating platforms. */ private static String platform = "insecure"; // /** // * . // * @param url URL // * @param name Cell Name // * @param boxSchema Box DataSchemaURI // * @param bName Box Name // */ /** * This is the parameterized constructor with four arguments. It initializes various class variables. * @param url Path URL * @param name Cell Name * @param boxSchema Box DataSchemaURI * @param bName Box Name */ public DcContext(final String url, final String name, final String boxSchema, String bName) { this.baseUrl = url; String c = this.baseUrl.substring(this.baseUrl.length() - 1); if (!c.equals("/")) { this.baseUrl += "/"; } this.cellName = name; this.schema = boxSchema; this.boxName = bName; if (this.cellName == null) { this.cellName = ""; } if (this.schema == null) { this.schema = ""; } if (this.boxName == null) { this.boxName = ""; } this.config = new DaoConfig(); this.cacheMap = new CacheMap(); } // /** // * Cell?ID?. // * @return Cell?ID // */ /** * This method gets the cell name. * @return Cell Name */ public final String getCellName() { return cellName; } // /** // * Cell?ID. // * @param value Cell?ID // */ /** * This method sets the cell name. * @param value Cell Name */ public final void setCellName(final String value) { this.cellName = value; } // /** // * Cell?URL?. // * @return Cell?URL // */ /** * This method gets the cell URL. * @return CellURL value */ public final String getCellUrl() { return this.baseUrl + this.cellName + "/"; } // /** // * Box?DataSchemaURI??. // * @return Box?DataSchemaURI // */ /** * This method gets the DataSchemaURI of the current box. * @return DataSchemaURI of Current Box */ public final String getBoxSchema() { return schema; } // /** // * Box?DataSchemaURI?. // * @param value Box?DataSchemaURI // */ /** * This method sets the DataSchemaURI of the current box. * @param value DataSchemaURI of Current Box */ public final void setBoxSchema(final String value) { this.schema = value; } // /** // * Box Name??. // * @return Box Nmae // */ /** * This method gets the Box Name. * @return Box Name value */ public String getBoxName() { return this.boxName; } // /** // * Box Name?. // * @param value Box Name // */ /** * This method sets the Box Name. * @param value Box Name value */ public void setBoxName(String value) { this.boxName = value; } // /** // * URL??. // * @return URL // */ /** * This method gets the BaseURL value. * @return BaseURL value */ public final String getBaseUrl() { return this.baseUrl; } // /** // * ?. // * @param value // */ /** * This method sets the client token value. * @param value Client Token */ public final void setClientToken(final String value) { this.clientToken = value; if (this.clientToken == null) { this.clientToken = ""; } } // /** // * HTTP?. // * @param value // */ /** * This method sets the HTTP Connection timeout value. * @param value HTTP Connection timeout */ public final void setConnectionTimeout(final int value) { config.setConnectionTimeout(value); } // /** // * Chunked. // * @param value Chunked // */ /** * This method sets the Chunked value. * @param value Chunked */ public final void setChunked(final Boolean value) { config.setChunked(value); } // /** // * DC??. // * @param value ? // */ /** * This method sets the DcVersion value. * @param value DcVersion */ public final void setDcVersion(final String value) { defaultHeaders.put(DC_VERSION, value); // ???core??? /** Initialize the version information of the core that processed the request. */ this.serverVersion = null; } // /** // * DC???. // * @return personium.io? // */ /** * This method gets the DcVersion value. * @return DcVersion value */ public final String getDcVersion() { return defaultHeaders.get(DC_VERSION); } // /** // * ??. // * @param value ?? // */ /** * This method sets the Threadable value. * @param value Threadable */ public final void setThreadable(final Boolean value) { config.setThreadable(value); } // /** // * ???. // * @return ?? // */ /** * This method gets the Threadable value. * @return Threadable value */ public final Boolean getThreadable() { return config.getThreadable(); } // /** // * . // * @param value ?? // */ /** * This method sets the Platform value. * @param value Platform */ public static void setPlatform(String value) { platform = value; } // /** // * ?. // * @return ?? // */ /** * This method gets the Platform value. * @return Platform value */ public static String getPlatform() { return platform; } // /** // * (CacheMap)?. // * @param value CacheMap // */ /** * This method sets cache of objects (CacheMap). * @param value CacheMap object */ public final void setCacheMap(final CacheMap value) { this.cacheMap = value; } // /** // * (CacheMap)??. // * @return CacheMap // */ /** * This method gets cache of objects (CacheMap). * @return CacheMap object */ public final CacheMap getCacheMap() { return this.cacheMap; } // /** // * DaoConfig??. // * @return DaoConfig // */ /** * This method gets DaoConfig object. * @return DaoConfig object */ public final DaoConfig getDaoConfig() { return config; } // /** // * HttpClient. // * @param value HttpClient // */ /** * This method sets the HttpClien object. * @param value HttpClient object */ public final void setHttpClient(final HttpClient value) { config.setHttpClient(value); } // /** // * ???????API???. // * @return API?? ?????null? setDcVersion?????null??? // */ /** * This method gets the version of the API that is retrieved from the server response. Returns null until you send a * request. * @return serverVersion, initialized to null if you set the version by using the method setDcVersion. */ public final String getServerVersion() { return this.serverVersion; } // /** // * ???????API??. // * @param version ???API?? // */ /** * This method sets the version of the API that is retrieved from the server response. * @param version Version of the API that processed the request */ void setServerVersion(String version) { this.serverVersion = version; } // /** // * ????. ??????? // * @param cellUrl ?Cell // * @param userId ID // * @param password // * @return ???Accessor // * @throws DaoException DAO // */ /** * This method generates a accessor. Utilizes token in the request header, to generate an accessor. * @param cellUrl authentication destination Cell * @param userId User ID * @param password User Password * @return Accessor that is generated * @throws DaoException Exception thrown */ public final Accessor asAccount(String cellUrl, String userId, String password) throws DaoException { return this.getAccessorWithAccount(cellUrl, userId, password); } // /** // * ????. ??????? // * @param cellUrl ?Cell // * @param userId ID // * @param password // * @return ???Accessor // * @throws DaoException DAO // */ /** * This method generates a accessor. Utilizes token in the request header, to generate an accessor. * @param cellUrl authentication destination Cell * @param userId User ID * @param password User Password * @return Accessor that is generated * @throws DaoException Exception thrown */ public final Accessor getAccessorWithAccount(String cellUrl, String userId, String password) throws DaoException { Accessor as = new Accessor(this); if (!as.getCellName().equals(cellUrl)) { as.setBoxName(""); } as.setCellName(cellUrl); as.setUserId(userId); as.setPassword(password); as.setDefaultHeaders(this.defaultHeaders); return as; } // /** // * ????. ??????? // * @param cellUrl ?Cell // * @param token // * @return ???Accessor // * @throws DaoException DAO // */ /** * This method generates a accessor. Utilizes transformer cell token in the request header, to generate an accessor. * @param cellUrl authentication destination Cell * @param token Transformer cell access token * @return Accessor that is generated * @throws DaoException Exception thrown */ public final Accessor getAccessorWithTransCellToken(String cellUrl, String token) throws DaoException { Accessor as = new Accessor(this); if (!as.getCellName().equals(cellUrl)) { as.setBoxName(""); } as.setCellName(cellUrl); as.setTransCellToken(token); as.setDefaultHeaders(this.defaultHeaders); return as; } // /** // * ????. ??????? // * @param token // * @return ???Accessor // * @throws DaoException DAO // */ /** * This method generates a accessor. Utilizes token in the request header, to generate an accessor. * @param token value * @return Accessor that is generated * @throws DaoException Exception thrown */ public final Accessor withToken(String token) throws DaoException { Accessor as = new Accessor(this); as.setAccessType(Accessor.KEY_TOKEN); as.setAccessToken(token); as.setDefaultHeaders(this.defaultHeaders); return as; } // /** // * ????. ??????? // * @param cellUrl ?Cell // * @param token // * @return ???Accessor // * @throws DaoException DAO // */ /** * This method generates a accessor. Utilizes refresh cell token in the request header, to generate an accessor. * @param cellUrl authentication destination Cell * @param token Refresh Token * @return Accessor that is generated * @throws DaoException Exception thrown */ public final Accessor getAccessorWithRefreshToken(String cellUrl, String token) throws DaoException { Accessor as = new Accessor(this); if (!as.getCellName().equals(cellUrl)) { as.setBoxName(""); } as.setCellName(cellUrl); as.setTransCellRefreshToken(token); as.setDefaultHeaders(this.defaultHeaders); return as; } // /** // * ????. ??????? // * @param cellUrl ?Cell // * @param userId ID // * @param password // * @param schemaUrl url // * @param schemaUserId ID // * @param schemaPassword // * @return ???Accessor // * @throws DaoException DAO // */ /** * This method generates a accessor. Utilizes schema authentication to generate an accessor. * @param cellUrl authentication destination Cell * @param userId User ID * @param password User Password * @param schemaUrl Schema Cell URL * @param schemaUserId Schema Cell User ID * @param schemaPassword Cell Schema User Password * @return Accessor that is generated * @throws DaoException Exception thrown */ public final Accessor asAccountWithSchemaAuthn(String cellUrl, String userId, String password, String schemaUrl, String schemaUserId, String schemaPassword) throws DaoException { return this.getAccessorWithAccountAndSchemaAuthn(cellUrl, userId, password, schemaUrl, schemaUserId, schemaPassword); } // /** // * ????. ??????? // * @param cellUrl ?Cell // * @param userId ID // * @param password // * @param schemaUrl url // * @param schemaUserId ID // * @param schemaPassword // * @return ???Accessor // * @throws DaoException DAO // */ /** * This method generates a accessor. Utilizes account and schema authentication to generate an accessor. * @param cellUrl authentication destination Cell * @param userId User ID * @param password User Password * @param schemaUrl Schema Cell URL * @param schemaUserId Schema Cell User ID * @param schemaPassword Cell Schema User Password * @return Accessor that is generated * @throws DaoException Exception thrown */ public final Accessor getAccessorWithAccountAndSchemaAuthn(String cellUrl, String userId, String password, String schemaUrl, String schemaUserId, String schemaPassword) throws DaoException { Accessor as = new Accessor(this); if (!as.getCellName().equals(cellUrl)) { as.setBoxName(""); } as.setCellName(cellUrl); as.setUserId(userId); as.setPassword(password); as.setSchema(schemaUrl); as.setSchemaUserId(schemaUserId); as.setSchemaPassword(schemaPassword); as.setDefaultHeaders(this.defaultHeaders); return as; } // /** // * ????. ??????? // * @param cellUrl ?Cell // * @param token // * @param schemaUrl url // * @param schemaUserId ID // * @param schemaPassword // * @return ???Accessor // * @throws DaoException DAO // */ /** * This method generates a accessor. Utilizes transformer cell access token and schema authentication to generate an * accessor. * @param cellUrl authentication destination Cell * @param token Transformer Cell Token * @param schemaUrl Schema Cell URL * @param schemaUserId Schema Cell User ID * @param schemaPassword Cell Schema User Password * @return Accessor that is generated * @throws DaoException Exception thrown */ public final Accessor getAccessorWithTransCellTokenAndSchemaAuthn(String cellUrl, String token, String schemaUrl, String schemaUserId, String schemaPassword) throws DaoException { Accessor as = new Accessor(this); if (!as.getCellName().equals(cellUrl)) { as.setBoxName(""); } as.setCellName(cellUrl); as.setTransCellToken(token); as.setSchema(schemaUrl); as.setSchemaUserId(schemaUserId); as.setSchemaPassword(schemaPassword); as.setDefaultHeaders(this.defaultHeaders); return as; } // /** // * JSONObject?. // * @return ???JSONObject // */ /** * This method creates a new JSONObject. * @return JSONObject object */ public final JSONObject newJson() { return new JSONObject(); } // /** // * JSON??JSONObject?. // * @param jsonStr JSON // * @return ??JSONObject // * @throws org.json.simple.parser.ParseException JSON // */ /** * This method creates a new JSONObject object from JSON string. * @param jsonStr JSON string * @return JSONObject object * @throws org.json.simple.parser.ParseException ParseException */ public final JSONObject newJson(final String jsonStr) throws org.json.simple.parser.ParseException { return (JSONObject) (new JSONParser().parse(jsonStr)); } // /** // * TODO Java DAO ????????????????. // * @param str ? // * @param charset // * @return ? // * @throws UnsupportedEncodingException // * @throws DecoderException // */ /** * TODO This is not a feature of the original JAVA DAO. There is a need to move to a different class. * @param str String decoded * @param charset Character Code * @return Dedoded URI * @throws UnsupportedEncodingException exception * @throws DecoderException exception */ public final String decodeURI(final String str, final String charset) throws UnsupportedEncodingException, DecoderException { URLCodec codec = new URLCodec(); return codec.decode(str, charset); } // /** // * TODO Java DAO ????????????????. // * @param str ? // * @return ? // * @throws UnsupportedEncodingException // * @throws DecoderException // */ /** * TODO This is not a feature of the original JAVA DAO. There is a need to move to a different class. * @param str String decoded * @return Dedoded URI * @throws UnsupportedEncodingException exception * @throws DecoderException exception */ public final String decodeURI(final String str) throws UnsupportedEncodingException, DecoderException { URLCodec codec = new URLCodec(); return codec.decode(str, "utf-8"); } // /** // * ????. // * @param key ?? // * @param value // */ /** * This method adds the key value pair to default header. * @param key Key Value * @param value Value */ public final void setDefaultHeader(String key, String value) { this.defaultHeaders.put(key, value); } // /** // * ????. // * @param key ?? // */ /** * This method removes the specified key value pair from the default header list. * @param key Key value */ public final void removeDefaultHeader(String key) { this.defaultHeaders.remove(key); } }