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.unit.core; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.lang.StringUtils; import org.apache.wink.common.internal.uri.UriEncoder; import com.fujitsu.dc.core.model.Box; import com.fujitsu.dc.core.model.ctl.ReceivedMessage; import com.fujitsu.dc.core.model.ctl.SentMessage; /** * URL????. */ public final class UrlUtils { /** ?URL???. */ public static final String PROP_TARGET_URL = "com.fujitsu.dc.test.target"; /** ??URL. */ public static final String DEFAULT_TARGET_URL = "http://localhost:9998"; /** * ??URL?? ??????URL? */ private static String baseUrl = System.getProperty(PROP_TARGET_URL, DEFAULT_TARGET_URL); /** * @param baseUrl baseUrl */ public static void setBaseUrl(String baseUrl) { UrlUtils.baseUrl = baseUrl; } /** * Host?getter. * @return baseUrl */ public static String getHost() { URL url; try { url = new URL(UrlUtils.baseUrl); } catch (MalformedURLException e) { throw new RuntimeException(e); } StringBuilder host = new StringBuilder(); host.append(url.getAuthority()); return host.toString(); } /** * baseUrl?getter. * @return baseUrl */ public static String getBaseUrl() { return UrlUtils.baseUrl; } private UrlUtils() { } /** * Unit?URL?. * @return Unit?URL */ public static String unitRoot() { return String.format("%s/", baseUrl); } /** * ?URL?. * @param cellName ?? * @return ?URL */ public static String cellRoot(final String cellName) { return String.format("%s/%s/", baseUrl, cellName); } /** * ?URL?. * @param cellName ?? * @return ?URL */ public static String receivedMessage(final String cellName) { return String.format("%s/%s/__message/port", baseUrl, cellName); } /** * ?URL?. * @param cellName ?? * @return ?URL */ public static String receivedMessageCtl(final String cellName) { return String.format("%s/%s/__ctl/%s", baseUrl, cellName, ReceivedMessage.EDM_TYPE_NAME); } /** * ?URL?. * @param cellName ?? * @param messageId ID * @return ?URL */ public static String receivedMessageCtl(final String cellName, final String messageId) { return String.format("%s/%s/__ctl/%s('%s')", baseUrl, cellName, ReceivedMessage.EDM_TYPE_NAME, messageId); } /** * ?URL?. * @param cellName ?? * @param messageId ID * @return ?URL */ public static String approvedMessage(final String cellName, final String messageId) { return String.format("%s/%s/__message/received/%s", baseUrl, cellName, messageId); } /** * ?URL?. * @param cellName ?? * @return ?URL */ public static String sentMessageCtl(final String cellName) { return String.format("%s/%s/__ctl/%s", baseUrl, cellName, SentMessage.EDM_TYPE_NAME); } /** * ?URL?. * @param cellName ?? * @param messageId ID * @return ?URL */ public static String sentMessageCtl(final String cellName, final String messageId) { return String.format("%s/%s/__ctl/%s('%s')", baseUrl, cellName, SentMessage.EDM_TYPE_NAME, messageId); } /** * API?URL?. * @param type ?? * @return URL */ public static String unitCtl(final String type) { return String.format("%s/__ctl/%s", baseUrl, type); } /** * API?URL?. * @param type ?? * @param idString ID * @return URL */ public static String unitCtl(final String type, final String idString) { return String.format("%s/__ctl/%s('%s')", baseUrl, type, idString); } /** * API?URL?. * @param type ?? * @param idString ID * @return URL */ public static String unitCtlCompKey(final String type, final String idString) { return String.format("%s/__ctl/%s(%s)", baseUrl, type, idString); } /** * API?URL?. * @param type ?? * @param idString ID * @param navprop Navigation * @return URL */ public static String unitCtl(final String type, final String idString, final String navprop) { return String.format("%s/__ctl/%s('%s')/%s", baseUrl, type, idString, navprop); } /** * API?URL?. * @param cellName ?? * @param type ??Account?Role * @return URL */ public static String cellCtl(final String cellName, final String type) { return String.format("%s/%s/__ctl/%s", baseUrl, cellName, type); } /** * API?URL?. * @param cellName ?? * @param type ??Account?Role * @param idString ID * @return URL */ public static String cellCtl(final String cellName, final String type, final String idString) { return String.format("%s/%s/__ctl/%s('%s')", baseUrl, cellName, type, idString); } /** * API?URL?(?). * @param cellName ?? * @param type ??Account?Role * @param idString ID * @return URL */ public static String cellCtlWithoutSingleQuote(final String cellName, final String type, final String idString) { return String.format("%s/%s/__ctl/%s(%s)", baseUrl, cellName, type, idString); } /** * API?NavigationPropertyURL?. * @param cellName ?? * @param entityTypeName ?? * @param entityTypeKey * @param navigationPropertyName ?? * @return API?NavigationPropertyURL */ public static String cellCtlNagvigationProperty(final String cellName, final String entityTypeName, final String entityTypeKey, final String navigationPropertyName) { return String.format("%s/%s/__ctl/%s('%s')/%s?$format=json", baseUrl, cellName, entityTypeName, entityTypeKey, navigationPropertyName); } /** * API?$links?MultiURL?. * @param cellName ?? * @param entityTypeName ?? * @param entityTypeKey * @param navigationPropertyName ?? * @return API?$links?MultiURL? */ public static String cellCtlLinksMulti(final String cellName, final String entityTypeName, final String entityTypeKey, final String navigationPropertyName) { return String.format("%s/%s/__ctl/%s('%s')/$links/%s?$format=json", baseUrl, cellName, entityTypeName, entityTypeKey, navigationPropertyName); } /** * API?$linksURL?. * @param cellName ?? * @param entityTypeName ?? * @param entityTypeKey * @param navigationPropertyName ?? * @param navigationPropertyKey * @return API?$links */ public static String cellCtlLinks(final String cellName, final String entityTypeName, final String entityTypeKey, final String navigationPropertyName, final String navigationPropertyKey) { return String.format("%s/%s/__ctl/%s('%s')/$links/%s('%s')?$format=json", baseUrl, cellName, entityTypeName, entityTypeKey, navigationPropertyName, navigationPropertyKey); } /** * ????API?URL?. * @param cellName ?? * @param account ?? * @return ????API?URL */ public static String accountLinks(final String cellName, final String account) { // TODO Accept?????$format=json return String.format("%s/%s/__ctl/Account('%s')/$links/_Role?$format=json", baseUrl, cellName, account); } /** * ????API?URL?. * @param cellName ?? * @param account ?? * @param role ?? * @return ????API?URL */ public static String accountLink(final String cellName, final String account, final String role) { return String.format("%s/%s/__ctl/Account('%s')/$links/_Role('%s')", baseUrl, cellName, account, role); } /** * ??. * @param cellName ?? * @return ??URL */ public static String auth(final String cellName) { return String.format("%s/%s/__auth", baseUrl, cellName); } /** * ???. * @param cellName ?? * @return ??URL */ public static String log(final String cellName) { return String.format("%s/%s/__log", baseUrl, cellName); } /** * BOX. * @param cellName ?? * @param boxName ?? * @param pathInBox * @return ??URL */ public static String box(final String cellName, final String boxName, final String... pathInBox) { String path = ""; if (pathInBox != null) { path = StringUtils.join(pathInBox, "/"); } return String.format("%s/%s/%s/%s", baseUrl, cellName, boxName, path); } /** * BOXroot. * @param cellName ?? * @param boxName ?? * @return ??URL */ public static String boxRoot(final String cellName, final String boxName) { return String.format("%s/%s/%s", baseUrl, cellName, boxName); } /** * BoxURl?API. * @param cellName ?? * @return ??URL */ public static String boxUrl(final String cellName) { return boxUrl(cellName, null); } /** * BoxURl?API. * @param cellName ?? * @param query * @return ??URL */ public static String boxUrl(final String cellName, final String query) { if (query == null) { return String.format("%s/%s/__box", baseUrl, cellName); } else { return String.format("%s/%s/__box?schema=%s", baseUrl, cellName, UriEncoder.encodeString(query)); } } /** * AssociationEnd. * @param cellName ?? * @param boxName ?? * @param colName ?? * @param assocName ? * @param etName EntityTypeName? * @return ??URL */ public static String associationEnd(final String cellName, final String boxName, final String colName, final String assocName, final String etName) { if (etName != null) { return String.format("%s/%s/%s/%s/$metadata/AssociationEnd(Name='%s',_EntityType.Name='%s')", baseUrl, cellName, boxName, colName, assocName, etName); } else { return String.format("%s/%s/%s/%s/$metadata/AssociationEnd(Name='%s',_EntityType.Name=null)", baseUrl, cellName, boxName, colName, assocName); } } /** * AssociationEnd?$links. * @param cellName ?? * @param boxName ?? * @param colName ?? * @param srcAssocName ?? * @param srcEtName ?EntityTypeName? * @param tgtAssocName ?? * @param tgtEtName ?EntityTypeName? * @return ??URL */ public static String associationEndLink(final String cellName, final String boxName, final String colName, final String srcAssocName, final String srcEtName, final String tgtAssocName, final String tgtEtName) { String format = "%s/%s/%s/%s/$metadata/AssociationEnd(Name='%s',_EntityType.Name='%s')" + "/$links/_AssociationEnd(Name='%s',_EntityType.Name='%s')"; return String.format(format, baseUrl, cellName, boxName, colName, srcAssocName, srcEtName, tgtAssocName, tgtEtName); } /** * userData. * @param cellName ?? * @param boxName ?? * @param colName ?? * @param entityType ?? * @return userDataURL */ public static String userData(final String cellName, final String boxName, final String colName, final String entityType) { return String.format("%s/%s/%s/%s/%s", baseUrl, cellName, boxName, colName, entityType); } /** * ?URL?. * @param cellName ?? * @param boxName ?? * @param roleName ?? * @return URL */ public static String roleResource(final String cellName, final String boxName, final String roleName) { String box = null; if (boxName == null) { box = Box.DEFAULT_BOX_NAME; } else { box = boxName; } return String.format("%s/%s/__role/%s/%s", baseUrl, cellName, box, roleName); } /** * ?URL?. * @param cellName ?? * @param boxName ?? * @param roleName ?? * @return URL */ public static String roleUrl(final String cellName, final String boxName, final String roleName) { String box = null; if (boxName == null) { box = "null"; } else { box = "'" + boxName + "'"; } return String.format("%s/%s/__ctl/Role(_Box.Name=%s,Name='%s')", baseUrl, cellName, box, roleName); } /** * Relation?URL?. * @param cellName ?? * @param boxName ?? * @param relationName Relation?? * @return RelationURL */ public static String relationUrl(final String cellName, final String boxName, final String relationName) { String box = null; if (boxName == null) { box = "null"; } else { box = "'" + boxName + "'"; } return String.format("%s/%s/__ctl/Relation(_Box.Name=%s,Name='%s')", baseUrl, cellName, box, relationName); } /** * ExtRole?URL?. * @param cellName ?? * @param boxName ?? * @param relationName ?? * @param extRoleName ExtRole?? * @return ExtRoleURL */ public static String extRoleUrl(final String cellName, final String boxName, final String relationName, final String extRoleName) { String box = null; String relation = null; String extRole = "'" + extRoleName + "'"; if (boxName == null) { box = "null"; } else { box = "'" + boxName + "'"; } if (relationName == null) { relation = "null"; } else { relation = "'" + relationName + "'"; } return String.format("%s/%s/__ctl/ExtRole(ExtRole=%s,_Relation.Name=%s,_Relation._Box.Name=%s)", baseUrl, cellName, extRole, relation, box); } /** * ExtCell?URL?. * @param cellName cellName * @param extCellUri extCellUri * @return ExtCellURL */ public static String extCellResource(final String cellName, final String extCellUri) { return String.format("%s/%s/__ctl/ExtCell('%s')", baseUrl, cellName, extCellUri); } /** * ACL?URL?. * @param boxName ?? * @param roleName ?? * @return ?URL */ public static String aclRelativePath(final String boxName, final String roleName) { return String.format("../%s/%s", boxName, roleName); } /** * Subject??URL?. * @param cellName ?? * @param accountName ?? * @return Subject??URL */ public static String subjectUrl(final String cellName, final String accountName) { return String.format("%s/%s/#%s", baseUrl, cellName, accountName); } /** * ?URL?. * @return ?URL */ public static String status() { return String.format("%s/__status", baseUrl); } /** * EntityTypeURL??. * @param cellName ?? * @param boxName ?? * @param colName ?? * @param entityTypeName ?? * @return EntityTypeURL */ public static String entityType(final String cellName, final String boxName, final String colName, final String entityTypeName) { if (entityTypeName == null) { return String.format("%s/%s/%s/%s/$metadata/EntityType", baseUrl, cellName, boxName, colName); } else { return String.format("%s/%s/%s/%s/$metadata/EntityType('%s')", baseUrl, cellName, boxName, colName, entityTypeName); } } /** * ComplexTypeURL??. * @param cellName ?? * @param boxName ?? * @param colName ?? * @param complexTypeName ?? * @return ComplexTypeURL */ public static String complexType(final String cellName, final String boxName, final String colName, final String complexTypeName) { if (complexTypeName == null) { return String.format("%s/%s/%s/%s/$metadata/ComplexType", baseUrl, cellName, boxName, colName); } else { return String.format("%s/%s/%s/%s/$metadata/ComplexType('%s')", baseUrl, cellName, boxName, colName, complexTypeName); } } /** * PropertyURL??. * @param cellName ?? * @param boxName ?? * @param colName ?? * @param propertyName ?? * @param entityTypeName ?? * @return PropertyURL */ public static String property(final String cellName, final String boxName, final String colName, final String propertyName, final String entityTypeName) { if (propertyName == null || entityTypeName == null) { return String.format("%s/%s/%s/%s/$metadata/Property", baseUrl, cellName, boxName, colName); } else { return String.format("%s/%s/%s/%s/$metadata/Property(Name='%s',_EntityType.Name='%s')", baseUrl, cellName, boxName, colName, propertyName, entityTypeName); } } /** * ComplexTypePropertyURL??. * @param cellName ?? * @param boxName ?? * @param colName ?? * @param complexTypePropertyName ?? * @param complexTypeName ?? * @return ComplextTypePropertyURL */ public static String complexTypeProperty(final String cellName, final String boxName, final String colName, final String complexTypePropertyName, final String complexTypeName) { if (complexTypePropertyName == null || complexTypeName == null) { return String.format("%s/%s/%s/%s/$metadata/ComplexTypeProperty", baseUrl, cellName, boxName, colName); } else { return String.format("%s/%s/%s/%s/$metadata/ComplexTypeProperty(Name='%s',_ComplexType.Name='%s')", baseUrl, cellName, boxName, colName, complexTypePropertyName, complexTypeName); } } /** * ?$linksURL??. * @param cellName ?? * @param boxName ?? * @param colName ?? * @param entityType ?? * @param entityTypeId ID * @param navigationProperty ?? * @param navigationPropertyId ID * @return $linksURL */ public static String schemaLinksWithSingleQuote(final String cellName, final String boxName, final String colName, final String entityType, final String entityTypeId, final String navigationProperty, final String navigationPropertyId) { if (navigationPropertyId == null) { return String.format("%s/%s/%s/%s/$metadata/%s('%s')/$links/_%s", baseUrl, cellName, boxName, colName, entityType, entityTypeId, navigationProperty); } else { return String.format("%s/%s/%s/%s/$metadata/%s('%s')/$links/_%s('%s')", baseUrl, cellName, boxName, colName, entityType, entityTypeId, navigationProperty, navigationPropertyId); } } /** * ?$linksURL??(?). * @param cellName ?? * @param boxName ?? * @param colName ?? * @param entityType ?? * @param entityTypeId ID * @param navigationProperty ?? * @param navigationPropertyId ID * @return $linksURL */ public static String schemaLinks(final String cellName, final String boxName, final String colName, final String entityType, final String entityTypeId, final String navigationProperty, final String navigationPropertyId) { if (navigationPropertyId == null) { return String.format("%s/%s/%s/%s/$metadata/%s(%s)/$links/_%s", baseUrl, cellName, boxName, colName, entityType, entityTypeId, navigationProperty); } else { return String.format("%s/%s/%s/%s/$metadata/%s(%s)/$links/_%s(%s)", baseUrl, cellName, boxName, colName, entityType, entityTypeId, navigationProperty, navigationPropertyId); } } /** * URL??. * @param cellName ?? * @param boxName ?? * @param colName ?? * @param entityTypeName ?? * @param key * @return URL */ public static String userdata(final String cellName, final String boxName, final String colName, final String entityTypeName, final String key) { if (key == null) { return String.format("%s/%s/%s/%s/%s", baseUrl, cellName, boxName, colName, entityTypeName); } else { return String.format("%s/%s/%s/%s/%s('%s')", baseUrl, cellName, boxName, colName, entityTypeName, key); } } /** * NPURL??. * @param cellName ?? * @param boxName ?? * @param colName ?? * @param entityTypeName ?? * @param key * @param navPropName NP?? * @return URL */ public static String userdataNP(final String cellName, final String boxName, final String colName, final String entityTypeName, final String key, final String navPropName) { return String.format("%s/%s/%s/%s/%s('%s')/_%s", baseUrl, cellName, boxName, colName, entityTypeName, key, navPropName); } }