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.test.utils; import static org.junit.Assert.assertEquals; import java.io.File; import java.util.HashMap; import javax.ws.rs.HttpMethod; import javax.ws.rs.core.MediaType; import org.apache.http.HttpHeaders; import org.apache.http.HttpStatus; import org.json.simple.JSONObject; import com.fujitsu.dc.common.utils.DcCoreUtils; import com.fujitsu.dc.test.jersey.AbstractCase; import com.fujitsu.dc.test.jersey.DcException; import com.fujitsu.dc.test.jersey.DcRequest; import com.fujitsu.dc.test.jersey.DcResponse; import com.fujitsu.dc.test.jersey.DcRestAdapter; import com.fujitsu.dc.test.jersey.bar.BarInstallTestUtils; import com.fujitsu.dc.test.unit.core.UrlUtils; /** * Http?. */ public class CellUtils { private CellUtils() { } /** * Cell1??. * @param cellName ?? * @param tokenWithAuthSchema (auth-schema?) * @param code ? * @return ? */ public static TResponse getWithAnyAuthSchema(final String cellName, final String tokenWithAuthSchema, int code) { return Http.request("cell-retrieve.txt").with("token", tokenWithAuthSchema).with("cellPath", cellName) .returns().statusCode(code); } /** * Cell1??(OAuth2?). * @param cellName ?? * @param token (Bearer ??) * @param code ? */ public static void get(final String cellName, final String token, final int code) { // Cell? TResponse res = Http.request("cell-retrieve.txt").with("token", "Bearer " + token) .with("cellPath", cellName).returns().statusCode(code); res.getStatusCode(); } /** * ?Cell1??. * @param cellName ?? * @param token * @param owner X-Dc-Unit-User?? * @param code ? */ public static void get(final String cellName, final String token, final String owner, final int code) { // Cell1? TResponse res = Http.request("cell-retrieveWithOwner.txt").with("token", token).with("cellPath", cellName) .with("owner", owner).returns().statusCode(code); res.getStatusCode(); } /** * Cell??. * @param tokenWithAuthSchema (auth-schema?) * @param code ? * @return ? */ public static TResponse listWithAnyAuthSchema(final String tokenWithAuthSchema, final int code) { // Cell? TResponse res = Http.request("cell-get.txt").with("token", tokenWithAuthSchema).returns().statusCode(code); return res; } /** * Cell??(OAuth2?). * @param token (Bearer ??) * @param code ? * @return ? */ public static TResponse list(final String token, final int code) { // Cell? TResponse res = Http.request("cell-get.txt").with("token", "Bearer " + token).returns().statusCode(code); res.getStatusCode(); return res; } /** * ?Cell??. * @param token * @param owner X-Dc-Unit-User?? * @param code ? * @return ? */ public static TResponse list(final String token, final String owner, final int code) { // Cell? TResponse res = Http.request("cell-getWithOwner.txt").with("token", token).with("owner", owner).returns() .statusCode(code); res.getStatusCode(); return res; } /** * Cell??. * @param cellName ?? * @param tokenWithAuthSchema (auth-schema?) * @param code ? * @return ? */ public static TResponse createWithAnyAuthSchema(final String cellName, final String tokenWithAuthSchema, int code) { // Cell? return Http.request("cell-create.txt").with("token", tokenWithAuthSchema).with("cellPath", cellName) .returns().statusCode(code); } /** * Cell??(OAuth2?). * @param cellName ?? * @param token (Bearer ??) * @param code ? */ public static void create(final String cellName, final String token, int code) { // Cell? TResponse res = Http.request("cell-create.txt").with("token", "Bearer " + token).with("cellPath", cellName) .returns().statusCode(code); res.getStatusCode(); } /** * ?Cell??. * @param cellName ?? * @param token * @param owner X-Dc-Unit-User?? * @param code ? */ public static void create(final String cellName, final String token, final String owner, final int code) { // Cell? TResponse res = Http.request("cell-createWithOwner.txt").with("token", token).with("cellPath", cellName) .with("owner", owner).returns().statusCode(code); res.getStatusCode(); } /** * NP?. * @param method ?? * @param cell ?? * @param entityType ?? * @param id ID * @param navPropName navPropName * @param body * @param token ? * @param sc ? * @return ? */ public static TResponse createNp(String method, String cell, String entityType, String id, String navPropName, JSONObject body, String token, int sc) { TResponse res = Http.request("cell/createNP.txt").with("method", method).with("cell", cell) .with("entityType", entityType).with("id", id).with("navPropName", navPropName) .with("accept", MediaType.APPLICATION_JSON).with("contentType", MediaType.APPLICATION_JSON) .with("token", token).with("body", body.toString()).returns().statusCode(sc); return res; } /** * Cell?. * @param cellPath ???? * @param cellName ??? * @param tokenWithAuthSchema (auth-schema?) * @param code ? * @return ? */ public static TResponse updateWithAnyAuthSchema(final String cellPath, final String cellName, final String tokenWithAuthSchema, int code) { return Http.request("cell-update.txt").with("token", tokenWithAuthSchema).with("cellPath", cellPath) .with("cellName", cellName).returns().statusCode(code); } /** * Cell?(OAuth2?). * @param cellPath ???? * @param cellName ??? * @param token (Bearer ??) * @param code ? */ public static void update(final String cellPath, final String cellName, final String token, int code) { // Cell? TResponse res = Http.request("cell-update.txt").with("token", "Bearer " + token).with("cellPath", cellPath) .with("cellName", cellName).returns().statusCode(code); res.getStatusCode(); } /** * ?Cell?. * @param cellPath ???? * @param cellName ??? * @param token * @param owner X-Dc-Unit-User?? * @param code ? */ public static void update(final String cellPath, final String cellName, final String token, final String owner, int code) { // Cell TResponse res = Http.request("cell-updateWithOwner.txt").with("token", token).with("cellPath", cellPath) .with("cellName", cellName).with("owner", owner).returns().statusCode(code); res.getStatusCode(); } /** * Cell?. * @param token * @param cellName ?? */ public static void delete(final String token, final String cellName) { delete(token, cellName, HttpStatus.SC_NO_CONTENT); } /** * Cell?(OAuth2?). * @param token (Bearer ??) * @param cellName ?? * @param code ?? */ public static void delete(final String token, final String cellName, int code) { // Cell Http.request("cell-delete.txt").with("token", "Bearer " + token).with("cellName", cellName).returns() .statusCode(code); } /** * Cell?. * @param tokenWithAuthSchema (auth-schema?) * @param cellName ?? * @param code ?? * @return ? */ public static TResponse deleteWithAnyAuthSchema(final String tokenWithAuthSchema, final String cellName, int code) { // Cell return Http.request("cell-delete.txt").with("token", tokenWithAuthSchema).with("cellName", cellName) .returns().statusCode(code); } /** * Cell?. * @param token * @param cellName ?? * @param owner * @param code ?? */ public static void delete(final String token, final String cellName, String owner, int code) { // Cell TResponse res = Http.request("cell-deleteWithOwner.txt").with("token", token).with("cellName", cellName) .with("owner", owner).returns(); if (code != -1) { assertEquals(code, res.getStatusCode()); } } /** * Cell?. * @param tokenWithAuthSchema (auth-schema?) * @param cellName ?? * @return ? */ public static DcResponse bulkDeletion(final String tokenWithAuthSchema, final String cellName) { // ?API? DcRequest request = DcRequest.delete(UrlUtils.cellRoot(cellName)); request.header(HttpHeaders.AUTHORIZATION, tokenWithAuthSchema).header("X-Dc-Recursive", "true"); return AbstractCase.request(request); } /** * PROPFIND?. * @param url URL * @param token ? * @param depth Depth * @param code ?? * @return ? */ public static TResponse propfind(String url, String token, String depth, int code) { TResponse tresponse = Http.request("cell/propfind-cell-allprop.txt").with("url", url).with("token", token) .with("depth", depth).returns(); tresponse.statusCode(code); return tresponse; } /** * PROPFIND?. * @param cellName Cell?? * @param authorization auth-schema?Authorization? * @param depth Depth * @param code ?? * @return ? */ public static TResponse propfindWithAnyAuthSchema(String cellName, String authorization, String depth, int code) { TResponse tresponse = Http.request("cell/propfind-cell-allprop-anyAuthSchema.txt").with("url", cellName) .with("authorization", authorization).with("depth", depth).returns(); tresponse.statusCode(code); return tresponse; } /** * PROPFIND?(??). * @param url URL * @param filename ???? * @param token ? * @param depth Depth * @param code ?? * @return ? */ public static TResponse propfind(String url, String filename, String token, String depth, int code) { TResponse tresponse = Http.request(filename).with("url", url).with("token", token).with("depth", depth) .returns(); tresponse.statusCode(code); return tresponse; } /** * event?PROPPATCH?. * @param cell cell * @param token ? * @param code ? * @param values * @return ? */ public static TResponse proppatch(String cell, String token, int code, String... values) { // PROPPATCH TResponse tresponse = Http.request("cell/event-proppacth.txt").with("cellPath", cell).with("token", token) .with("author1", values[0]).with("hoge", values[1]).returns(); tresponse.statusCode(code); return tresponse; } /** * event?PROPPATCH?. * @param cell cell * @param authorization Authorization?(auth-schema?) * @param code ? * @param values * @return ? */ public static TResponse proppatchWithAnyAuthSchema(String cell, String authorization, int code, String... values) { // PROPPATCH return Http.request("cell/proppacth-anyAuthSchema.txt").with("cellPath", cell) .with("authorization", authorization).with("author1", values[0]).returns().statusCode(code); } /** * event?POST?. * @param token ? * @param code ? * @param cellName ?? * @param jsonBody */ public static void event(String token, int code, String cellName, String jsonBody) { Http.request("cell/cell-event.txt").with("METHOD", HttpMethod.POST).with("token", token) .with("cellPath", cellName).with("requestKey", "").with("json", jsonBody).returns() .statusCode(code); } /** * event?POST?. * @param authorization Authorization?(auth-shema?) * @param code ? * @param cellName ?? * @param level * @param action ? * @param object ? * @param result ?? * @return ? */ @SuppressWarnings("unchecked") public static TResponse eventWithAnyAuthSchema(String authorization, int code, String cellName, String level, String action, String object, String result) { JSONObject body = new JSONObject(); body.put("level", level); body.put("action", action); body.put("object", object); body.put("result", result); return Http.request("cell/cell-event-anyAuthSchema.txt").with("METHOD", HttpMethod.POST) .with("authorization", authorization).with("cellPath", cellName).with("requestKey", "") .with("json", body.toJSONString()).returns().statusCode(code); } /** * __event/{boxName}?POST?. * @param authorization Authorization?(auth-shema?) * @param cellName ?? * @param boxName ?? * @param level * @param action ? * @param object ? * @param result ?? * @return ? * @throws DcException */ @SuppressWarnings("unchecked") public static DcResponse eventUnderBox(String authorization, String cellName, String boxName, String level, String action, String object, String result) throws DcException { JSONObject body = new JSONObject(); body.put("level", level); body.put("action", action); body.put("object", object); body.put("result", result); DcRestAdapter adaper = new DcRestAdapter(); HashMap<String, String> header = new HashMap<String, String>(); header.put(HttpHeaders.AUTHORIZATION, authorization); return adaper.post(UrlUtils.cellRoot(cellName) + "__event/" + boxName, body.toJSONString(), header); } /** * event?GET?(TResponse). * @param token ? * @param code ? * @param cellName ?? * @param collection ?"current" or "archive") * @param fileName ?("default.log" or "default.log.{no}") * @return ? */ public static TResponse getLog(String token, int code, String cellName, String collection, String fileName) { TResponse response = Http.request("cell/log-get.txt").with("METHOD", HttpMethod.GET).with("token", token) .with("cellPath", cellName).with("collection", collection).with("fileName", fileName) .with("ifNoneMatch", "*").returns(); response.statusCode(code); return response; } /** * event?GET?(DcResponse). * @param cellName ?? * @param collection ?"current" or "archive") * @param fileName ?("default.log" or "default.log.{no}") * @return ? DcResponse * @throws DcException DcException */ public static DcResponse getLog(String cellName, String collection, String fileName) throws DcException { DcRestAdapter adaper = new DcRestAdapter(); HashMap<String, String> header = new HashMap<String, String>(); header.put(HttpHeaders.AUTHORIZATION, AbstractCase.BEARER_MASTER_TOKEN); return adaper.get(UrlUtils.cellRoot(cellName) + "__log/" + collection + "/" + fileName, header); } /** * event?GET?. * @param cellName ?? * @param fileName ?("default.log" or "default.log.{no}") * @param authorization auth-schema?Authorization? * @return ? DcResponse * @throws DcException DcException */ public static DcResponse getCurrentLogWithAnyAuth(String cellName, String fileName, String authorization) throws DcException { DcRestAdapter adaper = new DcRestAdapter(); HashMap<String, String> header = new HashMap<String, String>(); header.put(HttpHeaders.AUTHORIZATION, authorization); return adaper.get(UrlUtils.cellRoot(cellName) + "__log/current/" + fileName, header); } /** * PROPFIND?. * @param cellName Cell?? * @param authorization auth-schema?Authorization? * @param depth Depth * @param code ?? * @return ? */ public static TResponse propfindArchiveLogDir(String cellName, String authorization, String depth, int code) { TResponse tresponse = Http.request("cell/propfind-cell-allprop-anyAuthSchema.txt") .with("url", cellName + "/__log/archive").with("authorization", authorization).with("depth", depth) .returns(); tresponse.statusCode(code); return tresponse; } /** * ?(__mypassword). * @param cellName Cell?? * @param newPassword ?? * @param authorization auth-schema?Authorization? * @return ? * @throws DcException */ public static DcResponse changePassword(String cellName, String newPassword, String authorization) throws DcException { DcRestAdapter rest = new DcRestAdapter(); // HashMap<String, String> requestheaders = new HashMap<String, String>(); requestheaders.put(HttpHeaders.AUTHORIZATION, authorization); requestheaders.put(DcCoreUtils.HttpHeaders.X_DC_CREDENTIAL, newPassword); return rest.put(UrlUtils.cellRoot(cellName) + "__mypassword", "", requestheaders); } /** * ?Basic????. * @param cellName ??Cell?? * @param account ??Cell??? * @param password ??Cell??? * @param schemaCell ?Cell?? * @param schemaAccount ?Cell??? * @param schemaPassword ?Cell??? * @return ? * @throws DcException */ public static DcResponse schemaAuthenticateWithBasic(String cellName, String account, String password, String schemaCell, String schemaAccount, String schemaPassword) throws DcException { // ?Cell?? TResponse res = tokenAuthenticationWithTarget(schemaCell, schemaAccount, schemaPassword, cellName); String schemaAuthenticatedToken = (String) res.bodyAsJson().get("access_token"); // ?(Basic?) DcRestAdapter rest = new DcRestAdapter(); // String schemaCellUrl = UrlUtils.cellRoot(schemaCell); String authorization = "Basic " + DcCoreUtils.createBasicAuthzHeader(schemaCellUrl, schemaAuthenticatedToken); HashMap<String, String> requestheaders = new HashMap<String, String>(); requestheaders.put(HttpHeaders.AUTHORIZATION, authorization); String body = String.format("grant_type=password&username=%s&password=%s", account, password); return rest.post(UrlUtils.cellRoot(cellName) + "__auth", body, requestheaders); } /** * ????. * @param cellName Cell?? * @param account ?? * @param pass * @param targetCell ????? * @return */ public static TResponse tokenAuthenticationWithTarget(String cellName, String account, String pass, String targetCell) { String targetUrl = UrlUtils.cellRoot(targetCell); return Http.request("authn/password-tc-c0.txt").with("remoteCell", cellName).with("username", account) .with("password", pass).with("dc_target", targetUrl).returns().statusCode(HttpStatus.SC_OK); } /** * ImplicitFlow??. * @param cellName Cell?? * @param schemaCell ?Cell?? * @param schemaAccount ?Cell??? * @param schemaPassword ?Cell??? * @param redirectPath ? * @param state ??????????? * @param addHeader ? * @return ? * @throws DcException */ public static DcResponse implicitflowAuthenticate(String cellName, String schemaCell, String schemaAccount, String schemaPassword, String redirectPath, String state, HashMap<String, String> addHeader) throws DcException { String clientId = UrlUtils.cellRoot(schemaCell); if (null == redirectPath) { redirectPath = "__/redirect.html"; } String body = "response_type=token&client_id=" + clientId + "&redirect_uri=" + clientId + redirectPath + "&username=" + schemaAccount + "&password=" + schemaPassword + "&state=" + state; DcRestAdapter rest = new DcRestAdapter(); // HashMap<String, String> requestheaders = new HashMap<String, String>(); if (addHeader != null) { requestheaders.putAll(addHeader); } requestheaders.put(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED); return rest.post(UrlUtils.cellRoot(cellName) + "__authz", body, requestheaders); } /** * Box URL?. * @param cellName Cell?? * @param schemaCell schemaCell?? * @param authorization auth-schema?Authorization? * @return ? * @throws DcException */ public static DcResponse getBoxUrl(String cellName, String schemaCell, String authorization) throws DcException { DcRestAdapter rest = new DcRestAdapter(); HashMap<String, String> requestheaders = new HashMap<String, String>(); requestheaders.put(HttpHeaders.AUTHORIZATION, authorization); String query = null; if (schemaCell != null) { query = UrlUtils.cellRoot(schemaCell); } return rest.get(UrlUtils.boxUrl(cellName, query), requestheaders); } /** * Box?. * @param cellName Cell?? * @param boxName Box?? * @param authorization auth-schema?Authorization? * @return ? */ public static TResponse boxInstall(String cellName, String boxName, String authorization) { File barFile = new File("requestData/barInstall/V1_1_2_bar_minimum.bar"); byte[] body = BarInstallTestUtils.readBarFile(barFile); Http client = Http.request("bar-install.txt").with("cellPath", cellName).with("path", boxName) .with("token", authorization).with("contType", "application/zip") .with("contLength", String.valueOf(body.length)); return client.setBodyBinary(body).returns().debug(); } /** * ACL?(principal: all, privilege: all). * @param cellName Cell?? * @param authorization auth-schema?Authorization? * @return ? */ public static TResponse setAclPriviriegeAllPrincipalAll(String cellName, String authorization) { return Http.request("cell/acl-setting-all.txt").with("url", cellName).with("token", authorization) .with("roleBaseUrl", UrlUtils.roleResource(cellName, null, "")).returns().debug(); } /** * ACL?(). * @param cellName Cell?? * @param token * @return ? */ public static TResponse setAclDefault(String cellName, String token) { return Http.request("cell/acl-default.txt").with("url", cellName).with("token", token) .with("roleBaseUrl", UrlUtils.roleResource(cellName, null, "")).returns().debug(); } }