Changeset 5618


Ignore:
Timestamp:
Apr 28, 2011, 12:59:18 PM (11 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #1596: Force host and port on file server level for external files

Location:
trunk
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/FileServer.java

    r5599 r5618  
    196196 
    197197  /**
     198    The maximum length of the host name:port that can be stored in
     199    the database.
     200    @see #setHost(String)
     201    @since 3.0
     202  */
     203  public static final int MAX_HOST_LENGTH = FileServerData.MAX_HOST_LENGTH;
     204 
     205  /**
     206    Get the name:port of the host that should override the host:port
     207    in the file url for files using this file server.
     208
     209    @return A string or null if no override should be done.
     210    @since 3.0
     211  */
     212  public String getHost()
     213  {
     214    return getData().getHost();
     215  }
     216 
     217  /**
     218    Set the host:port that should overrride the host:port
     219    specified in file urls.
     220     
     221    @param host The host:port or null to not override
     222    @throws PermissionDeniedException If the logged in user doesn't have
     223      write permission
     224    @throws InvalidDataException If the host is longer than {@link #MAX_HOST_LENGTH} or
     225      is an invalid host name + port
     226    @since 3.0
     227  */
     228  public void setHost(String host)
     229    throws PermissionDeniedException, InvalidDataException
     230  {
     231    checkPermission(Permission.WRITE);
     232    if (host != null)
     233    {
     234      if (!host.matches("[\\p{L}\\d_-]+(\\.[\\p{L}\\d_-]+)*(\\:\\d+)?"))
     235      {
     236        throw new InvalidDataException("Not a valid host name: " + host);
     237      }
     238    }
     239    getData().setHost(StringUtil.setNullableString(host, "host", MAX_HOST_LENGTH));
     240  }
     241 
     242  /**
    198243    The maximum length of the username that can be stored in the database.
    199244    @see #setUsername(String)
  • trunk/src/core/net/sf/basedb/core/data/FileServerData.java

    r5599 r5618  
    5858  {
    5959    this.connectionManagerFactory = connectionManagerFactory;
     60  }
     61 
     62  public static final int MAX_HOST_LENGTH = 255;
     63  private String host;
     64  /**
     65    Get the name:port of the actual host to use for all files
     66    using this server (overrides the host specified in the file url)
     67    @since 3.0
     68    @hibernate.property column="`host`" type="string" length="255" not-null="false"
     69  */
     70  public String getHost()
     71  {
     72    return host;
     73  }
     74  public void setHost(String host)
     75  {
     76    this.host = host;
    6077  }
    6178 
  • trunk/src/core/net/sf/basedb/util/uri/ConnectionManagerUtil.java

    r5606 r5618  
    180180    // Use a filter if a specific factory should be used
    181181    ExtensionsFilter filter = null;
    182     if (parameters != null && parameters.getConnectionManagerFactoryId() != null)
    183     {
    184       filter = new SingleExtensionFilter(parameters.getConnectionManagerFactoryId());
     182    if (parameters != null)
     183    {
     184      uri = parameters.changeHost(uri);
     185      if (parameters.getConnectionManagerFactoryId() != null)
     186      {
     187        filter = new SingleExtensionFilter(parameters.getConnectionManagerFactoryId());
     188      }
    185189    }
    186190   
  • trunk/src/core/net/sf/basedb/util/uri/ConnectionParameters.java

    r5599 r5618  
    2222package net.sf.basedb.util.uri;
    2323
     24import java.net.URI;
     25import java.net.URISyntaxException;
     26
    2427import net.sf.basedb.core.FileServer;
    2528import net.sf.basedb.core.data.FileServerData;
     29import net.sf.basedb.util.Values;
    2630
    2731/**
     
    4650    ConnectionParameters parameters = new ConnectionParameters();
    4751    parameters.setConnectionManagerFactoryId(fileServer.getConnectionManagerFactory());
     52    parameters.setHost(fileServer.getHost());
    4853    parameters.setUsername(fileServer.getUsername());
    4954    parameters.setPassword(fileServer.getPassword());
     
    5560 
    5661  private String factoryId;
     62  private String host;
    5763  private String username;
    5864  private String password;
     
    7884  {
    7985    this.factoryId = factoryId;
     86  }
     87 
     88  /**
     89    Get the host:port that should override the host:port
     90    specified by the file URI.
     91  */
     92  public String getHost()
     93  {
     94    return host;
     95  }
     96  public void setHost(String host)
     97  {
     98    this.host = host;
    8099  }
    81100 
     
    156175  }
    157176
     177  /**
     178    If the connection parameters include a specific host:port a new URI
     179    is created were the host:port is replaced. Otherwise the original URI
     180    is returned unmodified.
     181    @param uri The URI to change
     182    @return The new URI
     183  */
     184  public URI changeHost(URI uri)
     185  {
     186    String host = getHost();
     187    if (host == null) return uri;
     188   
     189    int port = -1;
     190    int colon = host.indexOf(':');
     191    if (colon > 0)
     192    {
     193      port = Values.getInt(host.substring(colon+1), -1);
     194      host = host.substring(0, colon);
     195    }
     196    try
     197    {
     198      uri = new URI(uri.getScheme(), uri.getUserInfo(), host, port, uri.getPath(), uri.getQuery(), uri.getFragment());
     199    }
     200    catch (URISyntaxException ex)
     201    {} // Shouldn't happen if the original URI is ok
     202    return uri;
     203  }
     204 
    158205}
  • trunk/www/filemanager/fileservers/edit_fileserver.jsp

    r5599 r5618  
    4242<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
    4343<%@ taglib prefix="t" uri="/WEB-INF/tab.tld" %>
     44<%@ taglib prefix="ext" uri="/WEB-INF/extensions.tld" %>
    4445<%
    4546final Item itemType = Item.FILESERVER;
     
    204205        <td></td>
    205206        <td>
    206           <div class="helpmessage" style="margin: 0px 10px 8px 0px; height: <%=(int)(scale*50)%>px; overflow: auto;">
     207          <div class="helpmessage" style="margin: 0px 10px 8px 0px; height: <%=(int)(scale*70)%>px; overflow: auto;">
    207208          <div id="cmf.auto.description" style="display: none;">
    208209            Automatically select a connection manager among those
     
    224225      </tr>
    225226      <tr>
     227        <td class="prompt">Host</td>
     228        <td><input <%=clazz%> type="text" name="host"
     229          value="<%=HTML.encodeTags(server == null ? Values.getString(cc.getPropertyValue("host")) : server.getHost())%>"
     230          size="40" maxlength="<%=FileServer.MAX_HOST_LENGTH%>"></td>
     231      </tr>
     232      <tr>
    226233        <td class="prompt">Username</td>
    227234        <td><input <%=clazz%> type="text" name="username"
  • trunk/www/filemanager/fileservers/index.jsp

    r5599 r5618  
    6060<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
    6161<%!
    62   private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,username,description");
     62  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,host,username,description");
    6363  private static final Item itemType = Item.FILESERVER;
    6464%>
     
    174174      server.setDescription(Values.getStringOrNull(upload.getParameter("description")));
    175175      server.setConnectionManagerFactoryId(Values.getStringOrNull(upload.getParameter("connectionManager")));
     176      server.setHost(Values.getStringOrNull(upload.getParameter("host")));
    176177      server.setUsername(Values.getStringOrNull(upload.getParameter("username")));
    177178      String password = Values.getStringOrNull(upload.getParameter("password"));
  • trunk/www/filemanager/fileservers/list_fileservers.jsp

    r5599 r5618  
    252252        exportable="true"
    253253        enumeration="<%=cm%>"
     254      />
     255      <tbl:columndef
     256        id="host"
     257        property="host"
     258        datatype="string"
     259        title="Host"
     260        sortable="true"
     261        filterable="true"
     262        exportable="true"
    254263      />
    255264      <tbl:columndef
     
    457466                  /></tbl:cell>
    458467                <tbl:cell column="connectionManager"><%=cmf == null ? "<i>- auto -</i>" : HTML.encodeTags(cmf.getDisplayName())%></tbl:cell>
     468                <tbl:cell column="host"><%=HTML.encodeTags(item.getHost())%></tbl:cell>
    459469                <tbl:cell column="username"><%=HTML.encodeTags(item.getUsername())%></tbl:cell>
    460470                <tbl:cell column="description"><%=HTML.encodeTags(item.getDescription())%></tbl:cell>
  • trunk/www/filemanager/fileservers/view_fileserver.jsp

    r5599 r5618  
    2020  ------------------------------------------------------------------
    2121--%>
    22 <%@page import="net.sf.basedb.util.uri.ConnectionManagerFactory"%>
    2322<%@ page pageEncoding="UTF-8" session="false"
    2423  import="net.sf.basedb.core.SessionControl"
     
    4847  import="net.sf.basedb.util.MD5"
    4948  import="net.sf.basedb.util.formatter.Formatter"
     49  import="net.sf.basedb.util.uri.ConnectionManagerFactory"
    5050  import="net.sf.basedb.clients.web.formatter.FormatterFactory"
    5151  import="net.sf.basedb.clients.web.extensions.ExtensionsControl"
     
    171171    <p>
    172172    <p:path>
    173       <p:pathelement title="File server" href="<%="index.jsp?ID="+ID%>" />
     173      <p:pathelement title="File servers" href="<%="index.jsp?ID="+ID%>" />
    174174      <p:pathelement title="<%=HTML.encodeTags(server.getName())%>" />
    175175    </p:path>
     
    276276        <td class="prompt">ConnectionManager</td>
    277277        <td><%=cmf == null ? "<i>- auto -</i>" : HTML.encodeTags(cmf.getDisplayName())%></td>
     278      </tr>
     279      <tr>
     280        <td class="prompt">Host</td>
     281        <td><%=HTML.encodeTags(server.getHost())%></td>
    278282      </tr>
    279283      <tr>
Note: See TracChangeset for help on using the changeset viewer.