Repository: syncope Updated Branches: refs/heads/master 87ac9bccb -> 736598fe5
[SYNCOPE-1095] Provide preview for json and xml binary field Project: http://git-wip-us.apache.org/repos/asf/syncope/repo Commit: http://git-wip-us.apache.org/repos/asf/syncope/commit/736598fe Tree: http://git-wip-us.apache.org/repos/asf/syncope/tree/736598fe Diff: http://git-wip-us.apache.org/repos/asf/syncope/diff/736598fe Branch: refs/heads/master Commit: 736598fe5d61500c7098579515dc934e4766761f Parents: 87ac9bc Author: Marco Di Sabatino Di Diodoro <marco.disabat...@tirasa.net> Authored: Wed May 24 15:11:36 2017 +0200 Committer: Marco Di Sabatino Di Diodoro <marco.disabat...@tirasa.net> Committed: Wed May 24 15:13:00 2017 +0200 ---------------------------------------------------------------------- .../markup/html/form/BinaryFieldPanel.java | 8 +- .../html/form/preview/BinaryTextPreviewer.java | 95 ++++++++++++++++++++ .../html/form/preview/BinaryTextPreviewer.html | 52 +++++++++++ 3 files changed, 151 insertions(+), 4 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/syncope/blob/736598fe/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/BinaryFieldPanel.java ---------------------------------------------------------------------- diff --git a/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/BinaryFieldPanel.java b/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/BinaryFieldPanel.java index cc34be5..c5504f5 100644 --- a/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/BinaryFieldPanel.java +++ b/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/BinaryFieldPanel.java @@ -61,6 +61,8 @@ import org.apache.wicket.util.lang.Bytes; public class BinaryFieldPanel extends FieldPanel<String> { private static final long serialVersionUID = 6264462604183088931L; + + private static final PreviewUtils PREVIEW_UTILS = PreviewUtils.getInstance(); private final String mimeType; @@ -74,15 +76,13 @@ public class BinaryFieldPanel extends FieldPanel<String> { private final BootstrapFileInputField fileUpload; - private final transient PreviewUtils previewUtils = PreviewUtils.getInstance(); - private final AbstractBinaryPreviewer previewer; public BinaryFieldPanel(final String id, final String name, final IModel<String> model, final String mimeType) { super(id, name, model); this.mimeType = mimeType; - previewer = previewUtils.getPreviewer(mimeType); + previewer = PREVIEW_UTILS.getPreviewer(mimeType); uploadForm = new StatelessForm<>("uploadForm"); uploadForm.setMultiPart(true); @@ -227,7 +227,7 @@ public class BinaryFieldPanel extends FieldPanel<String> { if (StringUtils.isNotBlank(model.getObject())) { final Component panelPreview; if (previewer == null) { - panelPreview = previewUtils.getDefaultPreviewer(mimeType); + panelPreview = PREVIEW_UTILS.getDefaultPreviewer(mimeType); } else { panelPreview = previewer.preview(model.getObject()); } http://git-wip-us.apache.org/repos/asf/syncope/blob/736598fe/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/preview/BinaryTextPreviewer.java ---------------------------------------------------------------------- diff --git a/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/preview/BinaryTextPreviewer.java b/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/preview/BinaryTextPreviewer.java new file mode 100644 index 0000000..c3685d1 --- /dev/null +++ b/client/console/src/main/java/org/apache/syncope/client/console/wicket/markup/html/form/preview/BinaryTextPreviewer.java @@ -0,0 +1,95 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.syncope.client.console.wicket.markup.html.form.preview; + +import static org.apache.syncope.client.console.wicket.markup.html.form.preview.AbstractBinaryPreviewer.LOG; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import javax.ws.rs.core.MediaType; +import org.apache.syncope.client.console.annotations.BinaryPreview; +import org.apache.wicket.Component; +import org.apache.wicket.markup.head.IHeaderResponse; +import org.apache.wicket.markup.head.OnLoadHeaderItem; +import org.apache.wicket.markup.html.WebMarkupContainer; +import org.apache.wicket.markup.html.form.TextArea; +import org.apache.wicket.markup.html.panel.Fragment; +import org.apache.wicket.model.Model; +import org.apache.wicket.util.io.IOUtils; + +@BinaryPreview(mimeTypes = { "application/json", "application/xml" }) +public class BinaryTextPreviewer extends AbstractBinaryPreviewer { + + private static final long serialVersionUID = 3808379310090668773L; + + public BinaryTextPreviewer(final String id, final String mimeType) { + super(id, mimeType); + } + + @Override + public Component preview(final byte[] uploadedBytes) { + + Fragment fragment = new Fragment("preview", "noPreviewFragment", this); + if (uploadedBytes.length > 0) { + try { + fragment = new Fragment("preview", "previewFragment", this); + InputStream stream = new ByteArrayInputStream(uploadedBytes); + TextArea<String> jsonEditor = + new TextArea<>("jsonEditorInfo", new Model<>(IOUtils.toString(stream))); + jsonEditor.setMarkupId("jsonEditorInfo").setOutputMarkupPlaceholderTag(true); + fragment.add(jsonEditor); + } catch (IOException e) { + LOG.error("Error evaluating text file", e); + } + } + + WebMarkupContainer previewContainer = new WebMarkupContainer("previewContainer"); + previewContainer.setOutputMarkupId(true); + previewContainer.add(fragment); + + return this.addOrReplace(previewContainer); + } + + @Override + public void renderHead(final IHeaderResponse response) { + super.renderHead(response); + String options; + switch (mimeType) { + case MediaType.APPLICATION_JSON: + options = "matchBrackets: true, autoCloseBrackets: true,"; + break; + case MediaType.APPLICATION_XML: + options = "autoCloseTags: true, mode: 'text/html',"; + break; + default: + options = "mode: 'text/html',"; + } + + response.render(OnLoadHeaderItem.forScript( + "var editor = CodeMirror.fromTextArea(document.getElementById('jsonEditorInfo'), {" + + " readOnly: true, " + + " lineNumbers: true, " + + " lineWrapping: true, " + + options + + " autoRefresh: true" + + "});" + + "editor.setSize('100%', 100)")); + } +} http://git-wip-us.apache.org/repos/asf/syncope/blob/736598fe/client/console/src/main/resources/org/apache/syncope/client/console/wicket/markup/html/form/preview/BinaryTextPreviewer.html ---------------------------------------------------------------------- diff --git a/client/console/src/main/resources/org/apache/syncope/client/console/wicket/markup/html/form/preview/BinaryTextPreviewer.html b/client/console/src/main/resources/org/apache/syncope/client/console/wicket/markup/html/form/preview/BinaryTextPreviewer.html new file mode 100644 index 0000000..63e71c7 --- /dev/null +++ b/client/console/src/main/resources/org/apache/syncope/client/console/wicket/markup/html/form/preview/BinaryTextPreviewer.html @@ -0,0 +1,52 @@ +<!-- +Licensed to the Apache Software Foundation (ASF) under one +or more contributor license agreements. See the NOTICE file +distributed with this work for additional information +regarding copyright ownership. The ASF licenses this file +to you 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. +--> +<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wicket="http://wicket.apache.org"> + <wicket:head> + <link rel="stylesheet" type="text/css" href="webjars/codemirror/${codemirror.version}/lib/codemirror.css"/> + <script type="text/javascript" src="webjars/codemirror/${codemirror.version}/lib/codemirror.js"></script> + <script type="text/javascript" src="webjars/codemirror/${codemirror.version}/mode/javascript/javascript.js"></script> + <script type="text/javascript" src="webjars/codemirror/${codemirror.version}/addon/display/autorefresh.js"></script> + <script type="text/javascript" src="webjars/codemirror/${codemirror.version}/addon/search/search.js"></script> + <script type="text/javascript" src="webjars/codemirror/${codemirror.version}/addon/search/searchcursor.js"></script> + <script type="text/javascript" src="webjars/codemirror/${codemirror.version}/addon/edit/matchbrackets.js"></script> + <script type="text/javascript" src="webjars/codemirror/${codemirror.version}/addon/edit/closebrackets.js"></script> + <style> + .w_content_3 { + padding: 0; + color: #333333; + font-family: Verdana,Tahoma,sans-serif; + font-size: 100%; + border: 1px solid #BBBBBB; + padding: 1%; + } + </style> + </wicket:head> + <wicket:extend> + <div wicket:id="previewContainer"> + <span wicket:id="preview"/> + </div> + <wicket:fragment wicket:id="previewFragment"> + <div> + <textarea wicket:id="jsonEditorInfo" id="consoleLayoutInfo" name="jsonEditorInfo" style="width: 100%; height: 100px;"></textarea> + </div> + </wicket:fragment> + <wicket:fragment wicket:id="noPreviewFragment"> + </wicket:fragment> + </wicket:extend> +</html>