Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable frame server #1334

Merged
merged 5 commits into from May 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions help/en/html/web/FrameServlet.shtml
Expand Up @@ -31,8 +31,7 @@ which by itself will return a page listing available frames.
It uses the facilities of the <code>jmri.util.JmriJFrame</code> class
to locate windows and create an image of their current appearance.
The window does not have to be maximized, but it does have to be open;
it's OK for the window to have been minimized to the Start Bar (Windows)
or converted to an icon (Mac OS X and Linux).
it's OK for the window to have been minimized.

<p>
The <a href="index.shtml#Configure">Web Server Preferences</a> can be used to adjust the default delay after
Expand Down Expand Up @@ -69,7 +68,7 @@ To display any <em>already open</em> JMRI window as a <b>clickable</b> image tha

<p>
The <code>jmri/web/servlet/frameimage/JmriJFrameServlet.properties</code>
<a href="https://svn.code.sf.net/p/jmri/code/trunk/jmri/java/src/jmri/web/servlet/frameimage/JmriJFrameServlet.properties" target=_new>
<a href="https://github.com/JMRI/JMRI/blob/master/java/src/jmri/web/servlet/frameimage/JmriJFrameServlet.properties">
properties file</a> can be used to configure the HTML that's returned when
a frame is requested.</p>

Expand Down
6 changes: 6 additions & 0 deletions help/en/html/web/index.shtml
Expand Up @@ -75,6 +75,12 @@
</dd>
<dt><a href="FrameServlet.shtml">Frame Server</a></dt>
<dd><dl>
<dt>Disable Frames</dt>
<dd>Disable the frame server. Disabling the frame server
disables most other preferences.</dd>
<dt>Use Panels Instead</dt>
<dd>If the frame server is disabled, requests for frames can be
redirected to <a href="PanelServlet.shtml">panels</a>.</dd>
<dt>Click Delay</dt>
<dd>Wait the specified number of seconds after a click
on the window (frame) image before refreshing the image.</dd>
Expand Down
4 changes: 4 additions & 0 deletions java/src/jmri/web/server/Bundle.properties
Expand Up @@ -26,6 +26,10 @@ LabelReadonlyPower = Disable power control in menus
ToolTipReadonlyPowerTrue = Power control in web pages will not function
ToolTipReadonlyPowerFalse = Power control in web pages will display a dialog allowing power to be set
LabelStartup = Start automatically with application
LabelDisableFrames = Disable frames
ToolTipDisableFrames = Disable frames in the web server
LabelRedirectFramesToPanels = Use panels instead
ToolTipRedirectFramesToPanels = Redirect web clients to inline panels when frames are disabled

ButtonSave = Save
ButtonApply = Apply
Expand Down
47 changes: 47 additions & 0 deletions java/src/jmri/web/server/WebServerPreferences.java
Expand Up @@ -40,6 +40,8 @@ public class WebServerPreferences extends Bean {
public static final String RailRoadName = "railRoadName"; // NOI18N
public static final String AllowRemoteConfig = "allowRemoteConfig"; // NOI18N
public static final String ReadonlyPower = "readonlyPower"; // NOI18N
public static final String DISABLE_FRAME_SERVER = "disableFrames"; // NOI18N
public static final String REDIRECT_FRAMES = "redirectFramesToPanels"; // NOI18N

// Flag that prefs have not been saved:
private boolean isDirty = false;
Expand All @@ -53,6 +55,8 @@ public class WebServerPreferences extends Bean {
private boolean allowRemoteConfig = false;
private boolean readonlyPower = true;
private int port = 12080;
private boolean disableFrames = false;
private boolean redirectFramesToPanels = true;
private static Logger log = LoggerFactory.getLogger(WebServerPreferences.class.getName());

public WebServerPreferences(String fileName) {
Expand Down Expand Up @@ -109,6 +113,8 @@ private void readPreferences(Preferences sharedPreferences) {
this.readonlyPower = sharedPreferences.getBoolean(ReadonlyPower, this.readonlyPower);
this.refreshDelay = sharedPreferences.getInt(RefreshDelay, this.refreshDelay);
this.useAjax = sharedPreferences.getBoolean(UseAjax, this.useAjax);
this.disableFrames = sharedPreferences.getBoolean(DISABLE_FRAME_SERVER, this.disableFrames);
this.redirectFramesToPanels = sharedPreferences.getBoolean(REDIRECT_FRAMES, this.redirectFramesToPanels);
try {
Preferences frames = sharedPreferences.node(DisallowedFrames);
if (frames.keys().length != 0) {
Expand Down Expand Up @@ -243,6 +249,8 @@ public void save() {
sharedPreferences.putBoolean(AllowRemoteConfig, this.allowRemoteConfig());
sharedPreferences.putBoolean(ReadonlyPower, this.isReadonlyPower());
sharedPreferences.put(RailRoadName, getRailRoadName());
sharedPreferences.putBoolean(DISABLE_FRAME_SERVER, this.isDisableFrames());
sharedPreferences.putBoolean(REDIRECT_FRAMES, this.redirectFramesToPanels);
Preferences node = sharedPreferences.node(DisallowedFrames);
this.getDisallowedFrames().stream().forEach((frame) -> {
node.put(Integer.toString(this.disallowedFrames.indexOf(frame)), frame);
Expand Down Expand Up @@ -376,6 +384,45 @@ public String getDefaultRailroadName() {
return Bundle.getMessage("DefaultRailroadName");
}

/**
* @return true if displaying frames in web pages is disabled, false
* otherwise
*/
public boolean isDisableFrames() {
return disableFrames;
}

/**
* Set whether or not frames are returned when requests for frames are made
* from web pages.
*
* @param disableFrames true to prevent frames from being displayed in web
* pages
*/
public void setDisableFrames(boolean disableFrames) {
this.disableFrames = disableFrames;
}

/**
* Are requests for frames redirected to panels when frames are disabled?
*
* @return true if frames should be redirected to panels, false otherwise
*/
public boolean isRedirectFramesToPanels() {
return redirectFramesToPanels;
}

/**
* Set whether or not requests for frames should be redirected to panels
* when frames are disabled.
*
* @param redirectFramesToPanels true if frames should be redirected to
* panels, false otherwise
*/
public void setRedirectFramesToPanels(boolean redirectFramesToPanels) {
this.redirectFramesToPanels = redirectFramesToPanels;
}

private static class WebServerPreferencesXml extends XmlFile {
}
}
73 changes: 47 additions & 26 deletions java/src/jmri/web/server/WebServerPreferencesPanel.java
Expand Up @@ -2,8 +2,7 @@

/**
* @author Steve Todd Copyright (C) 2011
* @author Randall Wood Copyright (C) 2012, 2014
* @version $Revision$
* @author Randall Wood Copyright (C) 2012, 2014, 2016
*/
import apps.PerformActionModel;
import apps.StartupActionsManager;
Expand Down Expand Up @@ -38,7 +37,6 @@

public class WebServerPreferencesPanel extends JPanel implements ListDataListener, PreferencesPanel {

private static final long serialVersionUID = 6907436730813458420L;
private JSpinner clickDelaySpinner;
private JSpinner refreshDelaySpinner;
private EditableList<String> disallowedFrames;
Expand All @@ -48,6 +46,11 @@ public class WebServerPreferencesPanel extends JPanel implements ListDataListene
private final WebServerPreferences preferences;
private boolean restartRequired = false;
private JCheckBox startup;
private final JCheckBox disableFrames = new JCheckBox();
private final JCheckBox redirectFramesToPanels = new JCheckBox();
private final JLabel clickDelayLabel = new JLabel(Bundle.getMessage("LabelClickDelay"));
private final JLabel refreshDelayLabel = new JLabel(Bundle.getMessage("LabelRefreshDelay"));
private final JLabel disallowedFramesLabel = new JLabel(Bundle.getMessage("LabelDisallowedFrames"));
private ItemListener startupItemListener;
private int startupActionPosition = -1;

Expand All @@ -64,20 +67,10 @@ private void initGUI() {
add(powerPanel());
add(startupPanel());
add(new JTitledSeparator(Bundle.getMessage("TitleDelayPanel")));
add(this.disableFramesPanel());
add(delaysPanel());
}

/*
private Group webServerPreferences(GroupLayout layout) {
railroadName = new JTextField(preferences.getRailRoadName());
railroadName.setToolTipText(Bundle.getMessage("ToolTipRailRoadName"));
railroadName.setColumns(30);
ParallelGroup group = layout.createParallelGroup(GroupLayout.Alignment.CENTER);
group.addComponent(new JLabel(Bundle.getMessage("LabelRailRoadName")), GroupLayout.Alignment.TRAILING);
group.addComponent(this.railroadName, GroupLayout.Alignment.LEADING);
return group;
}
*/
private void setGUI() {
clickDelaySpinner.setValue(preferences.getClickDelay());
refreshDelaySpinner.setValue(preferences.getRefreshDelay());
Expand All @@ -91,9 +84,12 @@ private void setGUI() {
useAjaxCB.setSelected(preferences.useAjax());
port.setValue(preferences.getPort());
readonlyPower.setSelected(preferences.isReadonlyPower());
this.disableFrames.setSelected(preferences.isDisableFrames());
this.redirectFramesToPanels.setSelected(preferences.isRedirectFramesToPanels());
InstanceManager.getDefault(StartupActionsManager.class).addPropertyChangeListener((PropertyChangeEvent evt) -> {
this.startup.setSelected(this.isStartupAction());
});
this.enableFrameControls(this.disableFrames.isSelected());
}

/**
Expand All @@ -117,7 +113,7 @@ private boolean setValues() {
preferences.setUseAjax(useAjaxCB.isSelected());
int portNum;
try {
portNum = (Integer)port.getValue();
portNum = (Integer) port.getValue();
} catch (NumberFormatException NFE) { // Not a number
portNum = 0;
}
Expand All @@ -132,6 +128,8 @@ private boolean setValues() {
preferences.setPort(portNum);
}
preferences.setReadonlyPower(readonlyPower.isSelected());
preferences.setDisableFrames(this.disableFrames.isSelected());
preferences.setRedirectFramesToPanels(this.redirectFramesToPanels.isSelected());
return didSet;
}

Expand Down Expand Up @@ -165,14 +163,14 @@ private JPanel delaysPanel() {
((JSpinner.DefaultEditor) clickDelaySpinner.getEditor()).getTextField().setEditable(false);
clickDelaySpinner.setToolTipText(Bundle.getMessage("ToolTipClickDelay"));
panel.add(clickDelaySpinner);
panel.add(new JLabel(Bundle.getMessage("LabelClickDelay")));
panel.add(this.clickDelayLabel);

spinMod = new SpinnerNumberModel(5, 1, 999, 1);
refreshDelaySpinner = new JSpinner(spinMod);
((JSpinner.DefaultEditor) refreshDelaySpinner.getEditor()).getTextField().setEditable(false);
refreshDelaySpinner.setToolTipText(Bundle.getMessage("ToolTipRefreshDelay"));
panel.add(refreshDelaySpinner);
panel.add(new JLabel(Bundle.getMessage("LabelRefreshDelay")));
panel.add(this.refreshDelayLabel);

useAjaxCB = new JCheckBox(Bundle.getMessage("LabelUseAjax"));
useAjaxCB.setToolTipText(Bundle.getMessage("ToolTipUseAjax"));
Expand All @@ -184,7 +182,7 @@ private JPanel delaysPanel() {
tf.setBorder(BorderFactory.createLineBorder(Color.black));
disallowedFrames.setListCellEditor(new DefaultListCellEditor<>(tf));
dfPanel.add(new JScrollPane(disallowedFrames));
dfPanel.add(new JLabel(Bundle.getMessage("LabelDisallowedFrames")));
dfPanel.add(this.disallowedFramesLabel);
dfPanel.setToolTipText(Bundle.getMessage("ToolTipDisallowedFrames"));

panel.add(dfPanel);
Expand Down Expand Up @@ -218,6 +216,23 @@ private JPanel powerPanel() {
return panel;
}

private JPanel disableFramesPanel() {
JPanel panel = new JPanel();

this.disableFrames.setText(Bundle.getMessage("LabelDisableFrames"));
this.disableFrames.setToolTipText(Bundle.getMessage("ToolTipDisableFrames"));
this.disableFrames.addChangeListener((ChangeEvent e) -> {
this.enableFrameControls(this.disableFrames.isSelected());
});
panel.add(this.disableFrames);

this.redirectFramesToPanels.setText(Bundle.getMessage("LabelRedirectFramesToPanels"));
this.redirectFramesToPanels.setToolTipText(Bundle.getMessage("ToolTipRedirectFramesToPanels"));
panel.add(this.redirectFramesToPanels);

return panel;
}

private JPanel startupPanel() {
JPanel panel = new JPanel();
this.startup = new JCheckBox(Bundle.getMessage("LabelStartup"), this.isStartupAction());
Expand Down Expand Up @@ -325,13 +340,19 @@ public boolean isPreferencesValid() {
private boolean isStartupAction() {
return InstanceManager.getDefault(StartupActionsManager.class).getActions(PerformActionModel.class).stream()
.anyMatch((model) -> (model.getClassName().equals(WebServerAction.class.getName())));
// The above is what NetBeans recommended the following be condenced to
// It's readable, but different, so including alternate form
//for (PerformActionModel model : InstanceManager.getDefault(StartupActionsManager.class).getActions(PerformActionModel.class)) {
// if (model.getClassName().equals(WebServerAction.class.getName())) {
// return true;
// }
//}
//return false;
}

private void enableFrameControls(boolean framesDisabled) {
// enabled if frames are disabled
this.redirectFramesToPanels.setEnabled(framesDisabled);

// enabled if frames are enabled
this.clickDelaySpinner.setEnabled(!framesDisabled);
this.clickDelayLabel.setEnabled(!framesDisabled);
this.refreshDelaySpinner.setEnabled(!framesDisabled);
this.refreshDelayLabel.setEnabled(!framesDisabled);
this.useAjaxCB.setEnabled(!framesDisabled);
this.disallowedFrames.setEnabled(!framesDisabled);
this.disallowedFramesLabel.setEnabled(!framesDisabled);
}
}
12 changes: 12 additions & 0 deletions java/src/jmri/web/servlet/frameimage/JmriJFrameServlet.java
Expand Up @@ -201,6 +201,18 @@ private void sendClickSequence(MouseListener m, Component c, int x, int y) {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
if (WebServerPreferences.getDefault().isDisableFrames()) {
if (WebServerPreferences.getDefault().isRedirectFramesToPanels()) {
if (JSON.JSON.equals(request.getParameter("format"))) {
response.sendRedirect("/panel?format=json");
} else {
response.sendRedirect("/panel");
}
} else {
response.sendError(HttpServletResponse.SC_FORBIDDEN, Bundle.getMessage(request.getLocale(), "FramesAreDisabled"));
}
return;
}
JmriJFrame frame = null;
String name = getFrameName(request.getRequestURI());
List<String> disallowedFrames = WebServerPreferences.getDefault().getDisallowedFrames();
Expand Down
Expand Up @@ -75,3 +75,5 @@ ListFooter = <DIV class="frame-footer"><A class="home" href="/">[Home]</A>\n\
<A class="help" href="/help/en/html/web/FrameServlet.shtml">[Help]</A>\n\
<SPAN class="info">Served by JMRIJFrameServlet</SPAN>\n\
</DIV></DIV>\n</BODY>\n</HTML>\n

FramesAreDisabled = Frames are disabled.
16 changes: 14 additions & 2 deletions web/js/home.js
Expand Up @@ -8,9 +8,14 @@
* By removing "&panels=true" from the url, shows panel editors and displays
* panels as screen scrapes.
*/
function listFrames() {
function listFrames(path) {
var framePath = "/frame/list?format=json&panels=true";
var panelPath = "/panel?format=json";
if (typeof path === "undefined") {
path = framePath;
}
$.ajax({
url: "/frame/list?format=json&panels=true",
url: path,
data: {},
success: function (data, textStatus, jqXHR) {
if (data.length !== 0) {
Expand All @@ -28,6 +33,13 @@ function listFrames() {
$("#no-open-frames").addClass("show").removeClass("hidden");
$("#frame-list").addClass("hidden").removeClass("show");
}
},
statusCode: {
403: function() {
if (path !== panelPath) {
listFrames(panelPath);
}
}
}
});
}
Expand Down