![JAR search and dependency download from the Maven repository](/logo.png)
com.gluonhq.impl.connect.gluoncloud.GluonCloudUpdateFieldReader Maven / Gradle / Ivy
/**
* Copyright (c) 2016 Gluon
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Gluon, any associated website, nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL GLUON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.gluonhq.impl.connect.gluoncloud;
import com.gluonhq.charm.down.Services;
import com.gluonhq.charm.down.plugins.StorageService;
import com.gluonhq.connect.GluonObservableObject;
import com.gluonhq.connect.gluoncloud.GluonClient;
import com.gluonhq.connect.provider.ObjectDataReader;
import com.gluonhq.connect.source.FileDataSource;
import com.gluonhq.connect.source.IODataSource;
import com.gluonhq.connect.source.RestDataSource;
import com.gluonhq.impl.connect.gluoncloud.metadata.ConnectMetadata;
import com.gluonhq.impl.connect.gluoncloud.metadata.SerializationUtils;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import javax.json.JsonBuilderFactory;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonReaderFactory;
import javax.json.JsonWriter;
import javax.json.JsonWriterFactory;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
public class GluonCloudUpdateFieldReader implements ObjectDataReader {
private static final Logger LOGGER = Logger.getLogger(GluonCloudUpdateFieldReader.class.getName());
private static final JsonBuilderFactory builderFactory = Json.createBuilderFactory(null);
private static final JsonReaderFactory readerFactory = Json.createReaderFactory(null);
private static final JsonWriterFactory writerFactory = Json.createWriterFactory(null);
private static File privateStorage;
static {
try {
privateStorage = Services.get(StorageService.class)
.flatMap(StorageService::getPrivateStorage)
.orElseThrow(() -> new IllegalAccessException());
} catch (IllegalAccessException ex) {
LOGGER.log(Level.SEVERE, "Could not get private storage from device.", ex);
}
}
private final GluonClient gluonClient;
private final String fieldName;
private final T object;
private final String objectIdentifier;
private final String listIdentifier;
private final ConnectMetadata metadata;
private final IODataSource dataSource;
public GluonCloudUpdateFieldReader(String fieldName, GluonObservableObjectImpl object) {
this.fieldName = fieldName;
this.gluonClient = object.getGluonClient();
this.objectIdentifier = object.getIdentifier();
this.object = object.get();
this.listIdentifier = null;
this.metadata = object.getMetadata();
dataSource = createDataSource();
}
public GluonCloudUpdateFieldReader(String fieldName, GluonObservableListImpl list, T object, String objectIdentifier) {
this.fieldName = fieldName;
this.gluonClient = list.getGluonClient();
this.objectIdentifier = objectIdentifier;
this.object = object;
this.listIdentifier = list.getIdentifier();
this.metadata = list.getMetadata();
dataSource = createDataSource();
}
@Override
public GluonObservableObject newGluonObservableObject() {
return new GluonObservableObject<>();
}
private IODataSource createDataSource() {
switch (gluonClient.getOperationMode()) {
case CLOUD_FIRST:
return createRestDataSource();
case LOCAL_ONLY:
return createLocalDataSource();
}
return null;
}
private IODataSource createRestDataSource() {
RestDataSource restDataSource = new RestDataSource();
restDataSource.setHost(gluonClient.getHost("data"));
restDataSource.setConsumerKey(gluonClient.getCredentials().getKey());
restDataSource.setConsumerSecret(gluonClient.getCredentials().getSecret());
restDataSource.setMethod("POST");
restDataSource.setPath("/client/updateField");
String fieldIdentifier = objectIdentifier + "/" + fieldName;
restDataSource.addFormParam("identifier", fieldIdentifier);
restDataSource.addFormParam("objectIdentifier", objectIdentifier);
if (listIdentifier != null) {
restDataSource.addFormParam("listIdentifier", listIdentifier);
}
restDataSource.addFormParam("target", SerializationUtils.serializeToString(metadata.serializeField(fieldName, object)));
restDataSource.addQueryParam("accept-encoding", "gzip");
return restDataSource;
}
private IODataSource createLocalDataSource() {
if (listIdentifier != null) {
File listContent = new File(privateStorage, listIdentifier);
if (listContent.exists()) {
return new FileDataSource(listContent);
}
} else {
File objectContent = new File(privateStorage, objectIdentifier);
if (objectContent.exists()) {
return new FileDataSource(objectContent);
}
}
return null;
}
@Override
public Void readObject() {
switch (gluonClient.getOperationMode()) {
case CLOUD_FIRST:
try (InputStream input = dataSource.getInputStream()) {
LOGGER.log(Level.FINE, "Ignoring response from input source.");
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Exception while reading input stream.", ex);
}
break;
case LOCAL_ONLY:
if (dataSource != null) {
if (listIdentifier != null) {
JsonArrayBuilder newItemsArray = builderFactory.createArrayBuilder();
try (JsonReader reader = readerFactory.createReader(new InputStreamReader(dataSource.getInputStream(), "UTF-8"))) {
JsonObject listObject = reader.readObject();
JsonArray itemsArray = listObject.getJsonArray(DataSkel.PROTOCOL_KEY_PAYLOAD);
if (itemsArray != null) {
for (JsonObject candidate : itemsArray.getValuesAs(JsonObject.class)) {
if (candidate.getString(DataSkel.PROTOCOL_KEY_UID).equals(objectIdentifier)) {
newItemsArray.add(builderFactory.createObjectBuilder().add(DataSkel.PROTOCOL_KEY_UID, objectIdentifier).add(DataSkel.PROTOCOL_KEY_PAYLOAD, SerializationUtils.serializeToString(metadata.serialize(object))));
} else {
newItemsArray.add(candidate);
}
}
}
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Exception while reading input stream.", ex);
}
try (JsonWriter writer = writerFactory.createWriter(new OutputStreamWriter(dataSource.getOutputStream(), "UTF-8"))) {
writer.write(builderFactory.createObjectBuilder().add(DataSkel.PROTOCOL_KEY_PAYLOAD, newItemsArray).build());
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Exception while writing output stream.", ex);
}
} else {
JsonObject toStore = Json.createObjectBuilder()
.add(DataSkel.PROTOCOL_KEY_PAYLOAD, SerializationUtils.serializeToString(metadata.serialize(object)))
.add(DataSkel.PROTOCOL_KEY_UID, objectIdentifier)
.build();
try (JsonWriter writer = writerFactory.createWriter(new OutputStreamWriter(dataSource.getOutputStream(), "UTF-8"))) {
writer.write(toStore);
} catch (IOException ex) {
LOGGER.log(Level.WARNING, "Exception while writing output stream.", ex);
}
}
}
break;
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy