Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1845,7 +1845,7 @@ public void componentShown(ComponentEvent evt) {
nameLabel = new JLabel("Name:");

nameField = new MirthTextField();
nameField.setDocument(new MirthFieldConstraints(40, false, true, true));
nameField.setDocument(new MirthFieldConstraints(Channel.MAX_NAME_LENGTH, false, true, true));
nameField.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent evt) {
Expand Down
3 changes: 3 additions & 0 deletions server/src/main/java/com/mirth/connect/model/Channel.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@

@XStreamAlias("channel")
public class Channel implements Serializable, Auditable, Migratable, Purgable, Cacheable<Channel>, ExportClearable {
/** The CHANNEL.NAME column is VARCHAR(40) in every supported database. */
public static final int MAX_NAME_LENGTH = 40;

private String id;
private Integer nextMetaDataId;
private String name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,20 @@ public synchronized void setChannelInitialState(Set<String> channelIds, ServerEv
}
}

/**
* Rejects a channel name that does not fit the CHANNEL.NAME column, before anything is written.
* Otherwise the insert fails in the database, and that error cannot be returned to the client.
*/
static void validateChannelName(String name) throws ControllerException {
if (StringUtils.length(name) > Channel.MAX_NAME_LENGTH) {
throw new ControllerException("Channel name must be at most " + Channel.MAX_NAME_LENGTH + " characters");
}
}

@Override
public synchronized boolean updateChannel(Channel channel, ServerEventContext context, boolean override, Calendar dateStartEdit) throws ControllerException {
validateChannelName(channel.getName());

// Extract metadata and then clear it from the channel model so it won't be stored in the database
ChannelExportData exportData = channel.getExportData();
channel.clearExportData();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: Open Integration Engine

package com.mirth.connect.server.controllers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import com.mirth.connect.client.core.ControllerException;
import com.mirth.connect.model.Channel;

public class DefaultChannelControllerNameTest {

@Test
public void acceptsANameAtTheColumnLimit() throws Exception {
DefaultChannelController.validateChannelName(StringUtils.repeat('a', Channel.MAX_NAME_LENGTH));
}

@Test
public void rejectsANameOverTheColumnLimitWithAClearMessage() {
// A clone of "TEST credentials via Configuration Map" (38 characters), from the issue.
String cloned = "Copy of TEST credentials via Configuration Map";
try {
DefaultChannelController.validateChannelName(cloned);
fail("A " + cloned.length() + " character name was accepted");
} catch (ControllerException e) {
assertEquals("Channel name must be at most 40 characters", e.getMessage());
}
}

@Test
public void leavesAMissingNameToTheExistingChecks() throws Exception {
DefaultChannelController.validateChannelName(null);
}
}
Loading