将所有子路径映射到REST服务

时间:2015-10-16 10:08:14

标签: rest coldfusion

给出Coldfusion休息服务的最小示例(名为“FileStore”):

> agg_splits
$`1.a`
   sp area point abund
1 cat    1     a    13
2 dog    1     a    13

$`2.a`
    sp area point abund
1 bird    2     a     8
2  dog    2     a     2

$`1.b`
   sp area point abund
1 cat    1     b    13

$`2.b`
    sp area point abund
1 rabb    2     b    18

这将匹配路径:

component
  restpath = ""
  rest     = true
{
  remote void function getFile(
    required string path restargsource = "Path"
  )
    httpmethod = "GET"
    restpath   = "{path}"
  {
    var file = FileReadBinary( "/some/path/to/local/files/#path#" );
    var mimetype = customFunctionToGetMimeType( getFileFromPath( path ) );
    cfcontent( variable = file, type = mimetype );
  }
}

但是如果你尝试子目录 - 即

/rest/FileStore/file1.pdf
/rest/FileStore/file2.jpg

它返回HTTP状态/rest/FileStore/subdir1/file3.xml /rest/FileStore/subdir2/subsubdir1/file4.raw (因为,我假设它无法找到匹配的REST服务)。

有没有办法让剩余路径匹配所有子路径?

3 个答案:

答案 0 :(得分:0)

使用URI重写执行重定向从路径中删除斜杠。

Apache中的一个例子(取自this answer)将是:

RewriteEngine on
RewriteRule ^(/rest/FileStore/[^/]*)/([^/]*/.*)$ $1__$2  [N]
RewriteRule ^(/rest/FileStore/[^/]*)/([^/]*)$    $1__$2  [R=302]

如果路径中有多个斜杠(并重复),则第一个规则将替换第一个斜杠(使用__);第二条规则将替换最终斜杠并执行(临时)重定向。

在服务中,您可以重新重写路径以包含斜杠。

remote void function getFile(
  required string path restargsource = "Path"
)
  httpmethod = "GET"
  restpath   = "{path}"
{
  var actual_path = Replace( path, "__", "/", "ALL" );
  var file = FileReadBinary( "/some/path/to/local/files/#actual_path#" );
  var filename = getFileFromPath( actual_path );
  var mimetype = customFunctionToGetMimeType( filename );

  cfheader( name = "content-disposition", value = "inline; filename=#filename#" );
  cfcontent( variable = file, type = mimetype );
}

这有几个问题:

  1. 如果您浏览的文件包含到不同子目录中其他文件的相对路径链接,则URI重写会破坏这些链接。
  2. 如果原始路径包含用于替换斜杠的字符串,则会破坏文件路径。

答案 1 :(得分:0)

为子目录的每个级别编写REST服务,并在内部将其指回原始服务。

Formal class 'DataFrame' [package "SparkR"] with 2 slots 
..@ env: <environment: 0x000000000xxxxxxx>  
..@ sdf:Class 'jobj' <environment: 0x000000000xxxxxx>  

重复恶心,直至达到足够的深度,使用率达到99.9999%/足够用。

  1. 解决了URI重写的问题(允许文件中的相对路径链接和文件名使用所有非斜杠字符)。
  2. 不是DRY。
  3. 如果您在超出实施深度的目录中获得文件,则无法找到该文件。 (虽然它可以与URI重写相结合,以允许找到该文件,即使它确实会破坏这些有限情况的内部链接。)

答案 2 :(得分:0)

Jersey(JAX-RS),这就是ColdFusion似乎在其服务的底层使用的,它允许regular expressions in its @PATH notation

使用正则表达式匹配所有字符(restpath = "{path:.*}"),然后您可以简单地匹配所有子路径:

component
  restpath = ""
  rest     = true
{
  remote void function getFile(
    required string path restargsource = "Path"
  )
    httpmethod = "GET"
    restpath   = "{path:.*}"
  {
    var file = FileReadBinary( "/some/path/to/local/files/#path#" );
    var mimetype = customFunctionToGetMimeType( getFileFromPath( path ) );
    cfcontent( variable = file, type = mimetype );
  }
}

感谢this answer的灵感