|
| 1 | +/*----------------------------------------------------------------------- |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +or more contributor license agreements. See the NOTICE file |
| 4 | +distributed with this work for additional information |
| 5 | +regarding copyright ownership. The ASF licenses this file |
| 6 | +to you under the Apache License, Version 2.0 (the |
| 7 | +"License"; you may not use this file except in compliance |
| 8 | +with the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | +Unless required by applicable law or agreed to in writing, |
| 13 | +software distributed under the License is distributed on an |
| 14 | +"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +KIND, either express or implied. See the License for the |
| 16 | +specific language governing permissions and limitations |
| 17 | +under the License. |
| 18 | +-----------------------------------------------------------------------*/ |
| 19 | +package com.f2i_consulting.fetpapi.client; |
| 20 | + |
| 21 | +import java.util.Optional; |
| 22 | +import java.util.UUID; |
| 23 | +import java.util.concurrent.TimeUnit; |
| 24 | + |
| 25 | +import com.f2i_consulting.fesapi.SWIGTYPE_p_double; |
| 26 | +import com.f2i_consulting.fesapi.fesapi; |
| 27 | +import com.f2i_consulting.fesapi.common.DataObjectRepository; |
| 28 | +import com.f2i_consulting.fesapi.resqml2.AbstractValuesProperty; |
| 29 | +import com.f2i_consulting.fesapi.resqml2.IjkGridExplicitRepresentation; |
| 30 | +import com.f2i_consulting.fetpapi.DataspaceVector; |
| 31 | +import com.f2i_consulting.fetpapi.MapStringDataObject; |
| 32 | +import com.f2i_consulting.fetpapi.MapStringString; |
| 33 | +import com.f2i_consulting.fetpapi.ResourceVector; |
| 34 | +import com.f2i_consulting.fetpapi.fetpapi; |
| 35 | +import com.f2i_consulting.fetpapi.Energistics.Etp.v12.Datatypes.Object.ContextInfo; |
| 36 | +import com.f2i_consulting.fetpapi.Energistics.Etp.v12.Datatypes.Object.ContextScopeKind; |
| 37 | +import com.f2i_consulting.fetpapi.Energistics.Etp.v12.Datatypes.Object.DataObject; |
| 38 | +import com.f2i_consulting.fetpapi.Energistics.Etp.v12.Datatypes.Object.Dataspace; |
| 39 | +import com.f2i_consulting.fetpapi.Energistics.Etp.v12.Datatypes.Object.Resource; |
| 40 | +import com.f2i_consulting.fetpapi.etp.ClientSession; |
| 41 | +import com.f2i_consulting.fetpapi.etp.CoreHandlers; |
| 42 | +import com.f2i_consulting.fetpapi.etp.DataArrayHandlers; |
| 43 | +import com.f2i_consulting.fetpapi.etp.DataspaceHandlers; |
| 44 | +import com.f2i_consulting.fetpapi.etp.DiscoveryHandlers; |
| 45 | +import com.f2i_consulting.fetpapi.etp.FesapiHdfProxyFactory; |
| 46 | +import com.f2i_consulting.fetpapi.etp.InitializationParameters; |
| 47 | +import com.f2i_consulting.fetpapi.etp.StoreHandlers; |
| 48 | + |
| 49 | +public class FetpapiClientUsingFesapi { |
| 50 | + |
| 51 | + /** |
| 52 | + * Loading the FesapiCpp and Fetpapi native libraries |
| 53 | + */ |
| 54 | + static { |
| 55 | + try { |
| 56 | + System.loadLibrary("${FESAPI_ASSEMBLY_NAME}"); |
| 57 | + } |
| 58 | + catch (UnsatisfiedLinkError e) { |
| 59 | + System.out.println("UnsatisfiedLinkError : " + e.toString()); |
| 60 | + } |
| 61 | + try { |
| 62 | + System.loadLibrary("${ASSEMBLY_NAME}"); |
| 63 | + } |
| 64 | + catch (UnsatisfiedLinkError e) { |
| 65 | + System.out.println("UnsatisfiedLinkError : " + e.toString()); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + public static void main(String[] args) { |
| 70 | + // Read arguments of the command line |
| 71 | + String serverUrl = "ws://etp.f2i-consulting.com:9002/"; |
| 72 | + String authorization = ""; |
| 73 | + |
| 74 | + if (args.length < 1) { |
| 75 | + System.out.println("Program arguments should be : serverUrl \"<BearerOrBasic> <token>\""); |
| 76 | + System.out.println("WARNING : you did not provide a server URL. The URL will be defaulted to ws://etp.f2i-consulting.com:9002/"); |
| 77 | + } |
| 78 | + else { |
| 79 | + serverUrl = args[0]; |
| 80 | + if (args.length < 2) { |
| 81 | + System.out.println("Program arguments should be : serverUrl \"Bearer <token>\""); |
| 82 | + System.out.println("WARNING : You did not provide any authorizaton after the URL. Please see https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Authorization for further details"); |
| 83 | + } |
| 84 | + else { |
| 85 | + if (!args[1].startsWith("Basic ") && !args[1].startsWith("Bearer ")) { |
| 86 | + System.out.println("WARNING : HTTP authorization \"" + args[3] + "\" field does not start with \"Basic \" nor \"Bearer \" which is unlikely"); |
| 87 | + } |
| 88 | + authorization = args[1]; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Create a DataObjectRepository in order to store retrieved dataobjects from the ETP server |
| 93 | + try (DataObjectRepository repo = new DataObjectRepository()) { |
| 94 | + repo.setDefaultStandard(DataObjectRepository.EnergisticsStandard.RESQML2_0_1); |
| 95 | + repo.setDefaultStandard(DataObjectRepository.EnergisticsStandard.EML2_0); |
| 96 | + |
| 97 | + // ****** Connect to ETP server through clientSession ****** |
| 98 | + String instanceUuid = UUID.randomUUID().toString(); |
| 99 | + InitializationParameters initializationParams = new InitializationParameters(instanceUuid, serverUrl); |
| 100 | + MapStringString additionalHeaderField = new MapStringString(); |
| 101 | + additionalHeaderField.put("data-partition-id", "osdu"); // Example for OSDU RDDMS |
| 102 | + initializationParams.setAdditionalHandshakeHeaderFields(additionalHeaderField); |
| 103 | + |
| 104 | + ClientSession clientSession = fetpapi.createClientSession(initializationParams, authorization); |
| 105 | + clientSession.setCoreProtocolHandlers(new CoreHandlers(clientSession)); |
| 106 | + clientSession.setDataspaceProtocolHandlers(new DataspaceHandlers(clientSession)); |
| 107 | + clientSession.setDiscoveryProtocolHandlers(new DiscoveryHandlers(clientSession)); |
| 108 | + clientSession.setStoreProtocolHandlers(new StoreHandlers(clientSession)); |
| 109 | + clientSession.setDataArrayProtocolHandlers(new DataArrayHandlers(clientSession)); |
| 110 | + new Thread(clientSession::run).start(); |
| 111 | + long start = System.currentTimeMillis(); |
| 112 | + while (clientSession.isEtpSessionClosed() && System.currentTimeMillis() - start < 5000) { |
| 113 | + TimeUnit.MILLISECONDS.sleep(1); |
| 114 | + } |
| 115 | + if (clientSession.isEtpSessionClosed()) { |
| 116 | + System.err.println("The ETP session cound not be establisehd in 5 seconds."); |
| 117 | + return; |
| 118 | + } |
| 119 | + System.out.println("Now connected to ETP Server"); |
| 120 | + // ****** We are now connected to ETP server through clientSession ****** |
| 121 | + // Set the HDF proxy factory in order to use one compliant with ETP |
| 122 | + repo.setHdfProxyFactory(new FesapiHdfProxyFactory(clientSession)); |
| 123 | + |
| 124 | + // ****** Get a dataspace content. This corresponds to getting the content of an EPC file ****** |
| 125 | + // Find an available ETP dataspace |
| 126 | + DataspaceVector allDataspaces = clientSession.getDataspaces(); |
| 127 | + Optional<Dataspace> dataspace = allDataspaces.stream().findAny(); |
| 128 | + if (dataspace.isEmpty()) { |
| 129 | + clientSession.close(); |
| 130 | + System.err.println("The ETP server has no dataspace."); |
| 131 | + return; |
| 132 | + } |
| 133 | + System.out.println("Working on dataspace " + dataspace.get().getUri()); |
| 134 | + // List resources of this ETP dataspace |
| 135 | + ContextInfo etpContext = new ContextInfo(); |
| 136 | + etpContext.setUri(dataspace.get().getUri()); |
| 137 | + etpContext.setDepth(1); |
| 138 | + ResourceVector allResources = clientSession.getResources(etpContext, ContextScopeKind.self); |
| 139 | + if (allResources.isEmpty()) { |
| 140 | + clientSession.close(); |
| 141 | + System.err.println("The ETP dataspace has no resource."); |
| 142 | + return; |
| 143 | + } |
| 144 | + // Get dataobjects from the resources to the DataObjectRepository |
| 145 | + MapStringString uriMap = new MapStringString(); |
| 146 | + long index = 0; |
| 147 | + for (Resource resource : allResources) { |
| 148 | + uriMap.put(Long.toString(index++), resource.getUri()); |
| 149 | + } |
| 150 | + MapStringDataObject allDataObjects = clientSession.getDataObjects(uriMap); |
| 151 | + for (DataObject dataObject : allDataObjects.values()) { |
| 152 | + repo.addOrReplaceGsoapProxy(dataObject.getData(), fetpapi.getDataObjectType(dataObject.getResource().getUri()), fetpapi.getDataspaceUri(dataObject.getResource().getUri())); |
| 153 | + } |
| 154 | + // ****** We have now in the DataObjectRepository the same content as if we would have deserialized and EPC file looking like the dataspace ****** |
| 155 | + |
| 156 | + // ****** Use the DataObjectRepository exactly as you are used to do with FESAPI ****** |
| 157 | + if (repo.getIjkGridRepresentationCount() > 0) { |
| 158 | + IjkGridExplicitRepresentation ijkGrid = repo.getIjkGridExplicitRepresentation(0); |
| 159 | + ijkGrid.loadSplitInformation(); |
| 160 | + long originIndex = ijkGrid.getXyzPointIndexFromCellCorner(0, 0, 0, 0); |
| 161 | + System.out.println("The index of the grid origin in XYZ points is : " + originIndex); |
| 162 | + ijkGrid.unloadSplitInformation(); |
| 163 | + if (ijkGrid.getValuesPropertyCount() > 0) { |
| 164 | + AbstractValuesProperty prop = ijkGrid.getValuesProperty(0); |
| 165 | + SWIGTYPE_p_double propValues = fesapi.new_DoubleArray(prop.getValuesCountOfPatch(0)); |
| 166 | + try { |
| 167 | + prop.getDoubleValuesOfPatch(0, propValues); |
| 168 | + System.out.println("The first cell value of prop " + prop.getTitle() + " is " + fesapi.DoubleArray_getitem(propValues, 0)); |
| 169 | + } |
| 170 | + finally { |
| 171 | + fesapi.delete_DoubleArray(propValues); |
| 172 | + } |
| 173 | + } |
| 174 | + } |
| 175 | + else { |
| 176 | + System.out.println("This dataspace has no IJK Grid"); |
| 177 | + } |
| 178 | + |
| 179 | + // Do not forget to close session once you have processed all the dataobject repository. |
| 180 | + System.out.println("Closing the session..."); |
| 181 | + clientSession.close(); |
| 182 | + } catch (InterruptedException e) { |
| 183 | + e.printStackTrace(); |
| 184 | + } |
| 185 | + |
| 186 | + System.out.println("FINISHED"); |
| 187 | + } |
| 188 | +} |
0 commit comments