001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 */ 019package org.nuxeo.ecm.automation.core.operations.blob; 020 021import java.net.URL; 022 023import org.nuxeo.ecm.automation.core.Constants; 024import org.nuxeo.ecm.automation.core.annotations.Operation; 025import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 026import org.nuxeo.ecm.automation.core.annotations.Param; 027import org.nuxeo.ecm.core.api.Blob; 028import org.nuxeo.ecm.core.api.impl.blob.URLBlob; 029 030/** 031 * TODO: detect mine? 032 * 033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 034 */ 035@Operation(id = CreateBlob.ID, category = Constants.CAT_FETCH, label = "File From URL", description = "Creates a file from a given URL. The file parameter specifies how to retrieve the file content. It should be an URL to the file you want to use as the source. You can also use an expression to get an URL from the context. Returns the created file.", aliases = { "Blob.Create" }) 036public class CreateBlob { 037 038 public static final String ID = "Blob.CreateFromURL"; 039 040 /** For tests. */ 041 public static boolean skipProtocolCheck; 042 043 @Param(name = "file") 044 protected URL file; 045 046 @Param(name = "mime-type", required = false) 047 protected String mimeType; 048 049 @Param(name = "filename", required = false) 050 protected String fileName; 051 052 @Param(name = "encoding", required = false) 053 protected String encoding; 054 055 @OperationMethod 056 public Blob run() { 057 String protocol = file.getProtocol(); 058 if (!"http".equals(protocol) && !"https".equals(protocol) && !"ftp".equals(protocol)) { 059 // don't let file: through 060 if (!skipProtocolCheck) { 061 return null; 062 } 063 } 064 if (fileName == null) { 065 fileName = file.getPath(); 066 int i = fileName.lastIndexOf('/'); 067 if (i > -1) { 068 fileName = fileName.substring(i + 1); 069 } 070 } 071 if (mimeType == null) { // TODO detect mime type 072 073 } 074 URLBlob blob = new URLBlob(file, mimeType, encoding); 075 blob.setFilename(fileName); 076 return blob; 077 } 078 079}