@@ -118,6 +118,16 @@ public class DeviceSimulatorUpdater {
|
||||
}
|
||||
|
||||
private static final class DeviceSimulatorUpdateThread implements Runnable {
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final String BUT_GOT_LOG_MESSAGE = " but got: ";
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final String DOWNLOAD_LOG_MESSAGE = "Download ";
|
||||
|
||||
private static final int MINIMUM_TOKENLENGTH_FOR_HINT = 6;
|
||||
|
||||
private static final Random rndSleep = new SecureRandom();
|
||||
@@ -212,60 +222,73 @@ public class DeviceSimulatorUpdater {
|
||||
LOGGER.debug("Downloading {} with token {}, expected sha1 hash {} and size {}", url,
|
||||
hideTokenDetails(targetToken), sha1Hash, size);
|
||||
|
||||
long overallread = 0;
|
||||
try {
|
||||
final CloseableHttpClient httpclient = createHttpClientThatAcceptsAllServerCerts();
|
||||
final HttpGet request = new HttpGet(url);
|
||||
request.addHeader(HttpHeaders.AUTHORIZATION, "TargetToken " + targetToken);
|
||||
|
||||
final String sha1HashResult;
|
||||
try (final CloseableHttpResponse response = httpclient.execute(request)) {
|
||||
|
||||
if (response.getStatusLine().getStatusCode() != HttpStatus.OK.value()) {
|
||||
final String message = wrongStatusCode(url, response);
|
||||
return new UpdateStatus(ResponseStatus.ERROR, message);
|
||||
}
|
||||
|
||||
if (response.getEntity().getContentLength() != size) {
|
||||
final String message = wrongContentLength(url, size, response);
|
||||
return new UpdateStatus(ResponseStatus.ERROR, message);
|
||||
}
|
||||
|
||||
// Exception squid:S2070 - not used for hashing sensitive
|
||||
// data
|
||||
@SuppressWarnings("squid:S2070")
|
||||
final MessageDigest md = MessageDigest.getInstance("SHA-1");
|
||||
|
||||
try (final BufferedOutputStream bdos = new BufferedOutputStream(
|
||||
new DigestOutputStream(ByteStreams.nullOutputStream(), md))) {
|
||||
try (BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent())) {
|
||||
overallread = ByteStreams.copy(bis, bdos);
|
||||
}
|
||||
}
|
||||
|
||||
if (overallread != size) {
|
||||
final String message = incompleteRead(url, size, overallread);
|
||||
return new UpdateStatus(ResponseStatus.ERROR, message);
|
||||
}
|
||||
|
||||
sha1HashResult = BaseEncoding.base16().lowerCase().encode(md.digest());
|
||||
}
|
||||
|
||||
if (!sha1Hash.equalsIgnoreCase(sha1HashResult)) {
|
||||
final String message = wrongHash(url, sha1Hash, overallread, sha1HashResult);
|
||||
return new UpdateStatus(ResponseStatus.ERROR, message);
|
||||
}
|
||||
|
||||
return readAndCheckDownloadUrl(url, targetToken, sha1Hash, size);
|
||||
} catch (IOException | KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
|
||||
LOGGER.error("Failed to download" + url, e);
|
||||
return new UpdateStatus(ResponseStatus.ERROR, "Failed to download " + url + ": " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static UpdateStatus readAndCheckDownloadUrl(final String url, final String targetToken,
|
||||
final String sha1Hash, final long size)
|
||||
throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException, IOException {
|
||||
long overallread;
|
||||
final CloseableHttpClient httpclient = createHttpClientThatAcceptsAllServerCerts();
|
||||
final HttpGet request = new HttpGet(url);
|
||||
request.addHeader(HttpHeaders.AUTHORIZATION, "TargetToken " + targetToken);
|
||||
|
||||
final String sha1HashResult;
|
||||
try (final CloseableHttpResponse response = httpclient.execute(request)) {
|
||||
|
||||
if (response.getStatusLine().getStatusCode() != HttpStatus.OK.value()) {
|
||||
final String message = wrongStatusCode(url, response);
|
||||
return new UpdateStatus(ResponseStatus.ERROR, message);
|
||||
}
|
||||
|
||||
if (response.getEntity().getContentLength() != size) {
|
||||
final String message = wrongContentLength(url, size, response);
|
||||
return new UpdateStatus(ResponseStatus.ERROR, message);
|
||||
}
|
||||
|
||||
// Exception squid:S2070 - not used for hashing sensitive
|
||||
// data
|
||||
@SuppressWarnings("squid:S2070")
|
||||
final MessageDigest md = MessageDigest.getInstance("SHA-1");
|
||||
|
||||
overallread = getOverallRead(response, md);
|
||||
|
||||
if (overallread != size) {
|
||||
final String message = incompleteRead(url, size, overallread);
|
||||
return new UpdateStatus(ResponseStatus.ERROR, message);
|
||||
}
|
||||
|
||||
sha1HashResult = BaseEncoding.base16().lowerCase().encode(md.digest());
|
||||
}
|
||||
|
||||
if (!sha1Hash.equalsIgnoreCase(sha1HashResult)) {
|
||||
final String message = wrongHash(url, sha1Hash, overallread, sha1HashResult);
|
||||
return new UpdateStatus(ResponseStatus.ERROR, message);
|
||||
}
|
||||
|
||||
final String message = "Downloaded " + url + " (" + overallread + " bytes)";
|
||||
LOGGER.debug(message);
|
||||
return new UpdateStatus(ResponseStatus.SUCCESSFUL, message);
|
||||
}
|
||||
|
||||
private static long getOverallRead(final CloseableHttpResponse response, final MessageDigest md)
|
||||
throws IOException {
|
||||
long overallread;
|
||||
try (final BufferedOutputStream bdos = new BufferedOutputStream(
|
||||
new DigestOutputStream(ByteStreams.nullOutputStream(), md))) {
|
||||
try (BufferedInputStream bis = new BufferedInputStream(response.getEntity().getContent())) {
|
||||
overallread = ByteStreams.copy(bis, bdos);
|
||||
}
|
||||
}
|
||||
return overallread;
|
||||
}
|
||||
|
||||
private static String hideTokenDetails(final String targetToken) {
|
||||
if (targetToken == null) {
|
||||
return "<NULL!>";
|
||||
@@ -285,29 +308,30 @@ public class DeviceSimulatorUpdater {
|
||||
|
||||
private static String wrongHash(final String url, final String sha1Hash, final long overallread,
|
||||
final String sha1HashResult) {
|
||||
final String message = "Download " + url + " failed with SHA1 hash missmatch (Expected: " + sha1Hash
|
||||
+ " but got: " + sha1HashResult + ") (" + overallread + " bytes)";
|
||||
final String message = DOWNLOAD_LOG_MESSAGE + url + " failed with SHA1 hash missmatch (Expected: "
|
||||
+ sha1Hash + BUT_GOT_LOG_MESSAGE + sha1HashResult + ") (" + overallread + " bytes)";
|
||||
LOGGER.error(message);
|
||||
return message;
|
||||
}
|
||||
|
||||
private static String incompleteRead(final String url, final long size, final long overallread) {
|
||||
final String message = "Download " + url + " is incomplete (Expected: " + size + " but got: " + overallread
|
||||
+ ")";
|
||||
final String message = DOWNLOAD_LOG_MESSAGE + url + " is incomplete (Expected: " + size
|
||||
+ BUT_GOT_LOG_MESSAGE + overallread + ")";
|
||||
LOGGER.error(message);
|
||||
return message;
|
||||
}
|
||||
|
||||
private static String wrongContentLength(final String url, final long size,
|
||||
final CloseableHttpResponse response) {
|
||||
final String message = "Download " + url + " has wrong content length (Expected: " + size + " but got: "
|
||||
+ response.getEntity().getContentLength() + ")";
|
||||
final String message = DOWNLOAD_LOG_MESSAGE + url + " has wrong content length (Expected: " + size
|
||||
+ BUT_GOT_LOG_MESSAGE + response.getEntity().getContentLength() + ")";
|
||||
LOGGER.error(message);
|
||||
return message;
|
||||
}
|
||||
|
||||
private static String wrongStatusCode(final String url, final CloseableHttpResponse response) {
|
||||
final String message = "Download " + url + " failed (" + response.getStatusLine().getStatusCode() + ")";
|
||||
final String message = DOWNLOAD_LOG_MESSAGE + url + " failed (" + response.getStatusLine().getStatusCode()
|
||||
+ ")";
|
||||
LOGGER.error(message);
|
||||
return message;
|
||||
}
|
||||
@@ -339,4 +363,5 @@ public class DeviceSimulatorUpdater {
|
||||
*/
|
||||
void updateFinished(AbstractSimulatedDevice device, final Long actionId);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -67,8 +67,6 @@ public class SpReceiverService extends ReceiverService {
|
||||
* the incoming message
|
||||
* @param type
|
||||
* the action type
|
||||
* @param contentType
|
||||
* the content type in message header
|
||||
* @param thingId
|
||||
* the thing id in message header
|
||||
*/
|
||||
@@ -82,14 +80,11 @@ public class SpReceiverService extends ReceiverService {
|
||||
private void delegateMessage(final Message message, final String type, final String thingId) {
|
||||
final MessageType messageType = MessageType.valueOf(type);
|
||||
|
||||
switch (messageType) {
|
||||
case EVENT:
|
||||
if (MessageType.EVENT.equals(messageType)) {
|
||||
handleEventMessage(message, thingId);
|
||||
break;
|
||||
default:
|
||||
LOGGER.info("No valid message type property.");
|
||||
break;
|
||||
return;
|
||||
}
|
||||
LOGGER.info("No valid message type property.");
|
||||
}
|
||||
|
||||
private void handleEventMessage(final Message message, final String thingId) {
|
||||
|
||||
@@ -33,9 +33,10 @@ import com.vaadin.ui.Window;
|
||||
* Popup dialog window for setting the values of generating the simulated
|
||||
* devices, e.g. the amount.
|
||||
*
|
||||
* @author Michael Hirsch
|
||||
*
|
||||
*/
|
||||
// Vaadin Inheritance
|
||||
@SuppressWarnings("squid:MaximumInheritanceDepth")
|
||||
public class GenerateDialog extends Window {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
@@ -49,8 +50,8 @@ public class GenerateDialog extends Window {
|
||||
private final TextField pollDelayTextField;
|
||||
private final TextField pollUrlTextField;
|
||||
private final TextField gatewayTokenTextField;
|
||||
private final OptionGroup protocolGroup;
|
||||
private final Button buttonOk;
|
||||
private OptionGroup protocolGroup;
|
||||
private Button buttonOk;
|
||||
|
||||
/**
|
||||
* Creates a new pop window for setting the configuration of simulating
|
||||
@@ -87,8 +88,8 @@ public class GenerateDialog extends Window {
|
||||
gatewayTokenTextField.setColumns(50);
|
||||
gatewayTokenTextField.setVisible(false);
|
||||
|
||||
protocolGroup = createProtocolGroup();
|
||||
buttonOk = createOkButton(callback);
|
||||
createProtocolGroup();
|
||||
createOkButton(callback);
|
||||
|
||||
namePrefixTextField.addValueChangeListener(event -> checkValid());
|
||||
amountTextField.addValueChangeListener(event -> checkValid());
|
||||
@@ -181,9 +182,9 @@ public class GenerateDialog extends Window {
|
||||
final URL basePollURL, final String gatewayToken, final Protocol protocol);
|
||||
}
|
||||
|
||||
private OptionGroup createProtocolGroup() {
|
||||
private void createProtocolGroup() {
|
||||
|
||||
final OptionGroup protocolGroup = new OptionGroup("Simulated Device Protocol");
|
||||
this.protocolGroup = new OptionGroup("Simulated Device Protocol");
|
||||
protocolGroup.addItem(Protocol.DMF_AMQP);
|
||||
protocolGroup.addItem(Protocol.DDI_HTTP);
|
||||
protocolGroup.setItemCaption(Protocol.DMF_AMQP, "Device Management Federation API (AMQP push)");
|
||||
@@ -195,12 +196,11 @@ public class GenerateDialog extends Window {
|
||||
pollUrlTextField.setVisible(directDeviceOptionSelected);
|
||||
gatewayTokenTextField.setVisible(directDeviceOptionSelected);
|
||||
});
|
||||
return protocolGroup;
|
||||
}
|
||||
|
||||
private Button createOkButton(final GenerateDialogCallback callback) {
|
||||
private void createOkButton(final GenerateDialogCallback callback) {
|
||||
|
||||
final Button buttonOk = new Button("generate");
|
||||
this.buttonOk = new Button("generate");
|
||||
buttonOk.setImmediate(true);
|
||||
buttonOk.setIcon(FontAwesome.GEARS);
|
||||
buttonOk.addClickListener(event -> {
|
||||
@@ -210,14 +210,11 @@ public class GenerateDialog extends Window {
|
||||
Integer.valueOf(pollDelayTextField.getValue().replace(".", "")),
|
||||
new URL(pollUrlTextField.getValue()), gatewayTokenTextField.getValue(),
|
||||
(Protocol) protocolGroup.getValue());
|
||||
} catch (final NumberFormatException e) {
|
||||
LOGGER.info(e.getMessage(), e);
|
||||
} catch (final MalformedURLException e) {
|
||||
} catch (final NumberFormatException | MalformedURLException e) {
|
||||
LOGGER.info(e.getMessage(), e);
|
||||
}
|
||||
GenerateDialog.this.close();
|
||||
});
|
||||
return buttonOk;
|
||||
}
|
||||
|
||||
private TextField createRequiredTextfield(final String caption, final String value, final Resource icon,
|
||||
@@ -226,7 +223,7 @@ public class GenerateDialog extends Window {
|
||||
return addTextFieldValues(textField, icon, validator);
|
||||
}
|
||||
|
||||
private TextField createRequiredTextfield(final String caption, final Property dataSource, final Resource icon,
|
||||
private TextField createRequiredTextfield(final String caption, final Property<?> dataSource, final Resource icon,
|
||||
final Validator validator) {
|
||||
final TextField textField = new TextField(caption, dataSource);
|
||||
return addTextFieldValues(textField, icon, validator);
|
||||
|
||||
@@ -57,6 +57,11 @@ import com.vaadin.ui.renderers.ProgressBarRenderer;
|
||||
@SuppressWarnings("squid:MaximumInheritanceDepth")
|
||||
public class SimulatorView extends VerticalLayout implements View {
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private static final String HTML_SPAN = ";</span>";
|
||||
|
||||
private static final String NEXT_POLL_COUNTER_SEC_COL = "nextPollCounterSec";
|
||||
|
||||
private static final String RESPONSE_STATUS_COL = "updateStatus";
|
||||
@@ -266,89 +271,90 @@ public class SimulatorView extends VerticalLayout implements View {
|
||||
}));
|
||||
}
|
||||
|
||||
private Converter<String, Protocol> createProtocolConverter() {
|
||||
|
||||
return new Converter<String, Protocol>() {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Protocol convertToModel(final String value, final Class<? extends Protocol> targetType,
|
||||
final Locale locale) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertToPresentation(final Protocol value, final Class<? extends String> targetType,
|
||||
final Locale locale) {
|
||||
switch (value) {
|
||||
case DDI_HTTP:
|
||||
return "DDI API (http)";
|
||||
case DMF_AMQP:
|
||||
return "DMF API (amqp)";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Protocol> getModelType() {
|
||||
return Protocol.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<String> getPresentationType() {
|
||||
return String.class;
|
||||
}
|
||||
};
|
||||
|
||||
private ProtocolConverter createProtocolConverter() {
|
||||
return new ProtocolConverter();
|
||||
}
|
||||
|
||||
private Converter<String, Status> createStatusConverter() {
|
||||
return new Converter<String, Status>() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
private StatusConverter createStatusConverter() {
|
||||
return new StatusConverter();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Status convertToModel(final String value, final Class<? extends Status> targetType,
|
||||
final Locale locale) {
|
||||
return null;
|
||||
}
|
||||
public static final class ProtocolConverter implements Converter<String, Protocol> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public String convertToPresentation(final Status value, final Class<? extends String> targetType,
|
||||
final Locale locale) {
|
||||
switch (value) {
|
||||
case UNKNWON:
|
||||
return "<span class=\"v-icon grayicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY
|
||||
+ ";\"color\":\"gray\";\">&#x"
|
||||
+ Integer.toHexString(FontAwesome.QUESTION_CIRCLE.getCodepoint()) + ";</span>";
|
||||
case PEDNING:
|
||||
return "<span class=\"v-icon yellowicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY
|
||||
+ ";\"color\":\"yellow\";\">&#x" + Integer.toHexString(FontAwesome.REFRESH.getCodepoint())
|
||||
+ ";</span>";
|
||||
case FINISH:
|
||||
return "<span class=\"v-icon greenicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY
|
||||
+ ";\"color\":\"green\";\">&#x"
|
||||
+ Integer.toHexString(FontAwesome.CHECK_CIRCLE.getCodepoint()) + ";</span>";
|
||||
case ERROR:
|
||||
return "<span class=\"v-icon redicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY
|
||||
+ ";\"color\":\"red\";\">&#x"
|
||||
+ Integer.toHexString(FontAwesome.EXCLAMATION_CIRCLE.getCodepoint()) + ";</span>";
|
||||
default:
|
||||
throw new IllegalStateException("unknown value");
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public Protocol convertToModel(final String value, final Class<? extends Protocol> targetType,
|
||||
final Locale locale) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Status> getModelType() {
|
||||
return Status.class;
|
||||
@Override
|
||||
public String convertToPresentation(final Protocol value, final Class<? extends String> targetType,
|
||||
final Locale locale) {
|
||||
switch (value) {
|
||||
case DDI_HTTP:
|
||||
return "DDI API (http)";
|
||||
case DMF_AMQP:
|
||||
return "DMF API (amqp)";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<String> getPresentationType() {
|
||||
return String.class;
|
||||
@Override
|
||||
public Class<Protocol> getModelType() {
|
||||
return Protocol.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<String> getPresentationType() {
|
||||
return String.class;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class StatusConverter implements Converter<String, Status> {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
public Status convertToModel(final String value, final Class<? extends Status> targetType,
|
||||
final Locale locale) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertToPresentation(final Status value, final Class<? extends String> targetType,
|
||||
final Locale locale) {
|
||||
switch (value) {
|
||||
case UNKNWON:
|
||||
return "<span class=\"v-icon grayicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY
|
||||
+ ";\"color\":\"gray\";\">&#x" + Integer.toHexString(FontAwesome.QUESTION_CIRCLE.getCodepoint())
|
||||
+ HTML_SPAN;
|
||||
case PEDNING:
|
||||
return "<span class=\"v-icon yellowicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY
|
||||
+ ";\"color\":\"yellow\";\">&#x" + Integer.toHexString(FontAwesome.REFRESH.getCodepoint())
|
||||
+ HTML_SPAN;
|
||||
case FINISH:
|
||||
return "<span class=\"v-icon greenicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY
|
||||
+ ";\"color\":\"green\";\">&#x" + Integer.toHexString(FontAwesome.CHECK_CIRCLE.getCodepoint())
|
||||
+ HTML_SPAN;
|
||||
case ERROR:
|
||||
return "<span class=\"v-icon redicon\" style=\"font-family: " + FontAwesome.FONT_FAMILY
|
||||
+ ";\"color\":\"red\";\">&#x"
|
||||
+ Integer.toHexString(FontAwesome.EXCLAMATION_CIRCLE.getCodepoint()) + HTML_SPAN;
|
||||
default:
|
||||
throw new IllegalStateException("unknown value");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<Status> getModelType() {
|
||||
return Status.class;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<String> getPresentationType() {
|
||||
return String.class;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user