diff --git a/tap/src/main/java/ca/nrc/cadc/sample/RubinFormatFactory.java b/tap/src/main/java/ca/nrc/cadc/sample/RubinFormatFactory.java new file mode 100644 index 0000000..b5d80d6 --- /dev/null +++ b/tap/src/main/java/ca/nrc/cadc/sample/RubinFormatFactory.java @@ -0,0 +1,33 @@ +package ca.nrc.cadc.sample; + +import ca.nrc.cadc.dali.util.Format; +import ca.nrc.cadc.tap.TapSelectItem; +import ca.nrc.cadc.tap.writer.format.PostgreSQLFormatFactory; +import org.apache.log4j.Logger; + +import ca.nrc.cadc.sample.RubinURLFormat; + + +public class RubinFormatFactory extends PostgreSQLFormatFactory { + + private static Logger log = Logger.getLogger(RubinFormatFactory.class); + + public RubinFormatFactory() { + super(); + } + + @Override + public Format getClobFormat(TapSelectItem columnDesc) { + // function with CLOB argument + if (columnDesc != null) { + // oga.ObsCore + if ("oga.ObsCore".equalsIgnoreCase(columnDesc.tableName)) { + if ("access_url".equalsIgnoreCase(columnDesc.getColumnName())) { + return new RubinURLFormat(); + } + } + } + + return super.getClobFormat(columnDesc); + } +} diff --git a/tap/src/main/java/ca/nrc/cadc/sample/RubinURLFormat.java b/tap/src/main/java/ca/nrc/cadc/sample/RubinURLFormat.java new file mode 100644 index 0000000..85c985b --- /dev/null +++ b/tap/src/main/java/ca/nrc/cadc/sample/RubinURLFormat.java @@ -0,0 +1,43 @@ +package ca.nrc.cadc.sample; + +import java.net.MalformedURLException; +import java.net.URL; + +import ca.nrc.cadc.dali.util.Format; +import org.apache.log4j.Logger; + + +public class RubinURLFormat implements Format { + + private static Logger log = Logger.getLogger(RubinURLFormat.class); + + private static final String BASE_URL = System.getProperty("base_url"); + + public RubinURLFormat() { + super(); + } + + @Override + public Object parse(String s) { + throw new UnsupportedOperationException("TAP Formats cannot parse strings."); + } + + @Override + public String format(Object o) { + if (o == null) { + return ""; + } + + String s = (String) o; + + try { + URL orig = new URL((String) o); + URL base_url = new URL(BASE_URL); + URL rewritten = new URL(orig.getProtocol(), base_url.getHost(), orig.getFile()); + + return rewritten.toExternalForm(); + } catch (MalformedURLException ex) { + throw new RuntimeException("BUG: Failed to rewrite URL: " + s, ex); + } + } +} diff --git a/tap/src/main/resources/PluginFactory.properties b/tap/src/main/resources/PluginFactory.properties index 56e18f5..9251e69 100644 --- a/tap/src/main/resources/PluginFactory.properties +++ b/tap/src/main/resources/PluginFactory.properties @@ -12,10 +12,10 @@ ca.nrc.cadc.tap.MaxRecValidator.impl = ca.nrc.cadc.sample.MaxRecValidatorImpl ca.nrc.cadc.tap.db.DatabaseDataType=ca.nrc.cadc.tap.pg.PostgresDataTypeMapper +ca.nrc.cadc.tap.writer.format.FormatFactory = ca.nrc.cadc.sample.RubinFormatFactory + ca.nrc.cadc.tap.UploadManager = ca.nrc.cadc.sample.RubinUploadManagerImpl #ca.nrc.cadc.tap.TableWriter = ca.nrc.cadc.tap.DefaultTableWriter -ca.nrc.cadc.tap.writer.format.FormatFactory = ca.nrc.cadc.tap.writer.format.PostgreSQLFormatFactory - ca.nrc.cadc.tap.ResultStore = ca.nrc.cadc.sample.ResultStoreImpl