diff --git a/src/main/java/org/opencadc/tap/impl/RubinFormatFactory.java b/src/main/java/org/opencadc/tap/impl/RubinFormatFactory.java new file mode 100644 index 0000000..630a2ac --- /dev/null +++ b/src/main/java/org/opencadc/tap/impl/RubinFormatFactory.java @@ -0,0 +1,34 @@ +package org.opencadc.tap.impl; + +import ca.nrc.cadc.dali.util.Format; +import ca.nrc.cadc.tap.TapSelectItem; +import ca.nrc.cadc.tap.writer.format.OracleFormatFactory; +import org.apache.log4j.Logger; + +import org.opencadc.tap.impl.RubinURLFormat; + + +public class RubinFormatFactory extends OracleFormatFactory { + + private static Logger log = Logger.getLogger(RubinFormatFactory.class); + + public RubinFormatFactory() { + super(); + } + + @Override + public Format getClobFormat(TapSelectItem columnDesc) { + // function with CLOB argument + if (columnDesc != null) { + // ivoa.ObsCore + if ("ivoa.ObsCore".equalsIgnoreCase(columnDesc.tableName)) { + if ("access_url".equalsIgnoreCase(columnDesc.getColumnName())) { + log.info("getClobFormat called for access_url"); + return new RubinURLFormat(); + } + } + } + + return super.getClobFormat(columnDesc); + } +} diff --git a/src/main/java/org/opencadc/tap/impl/RubinURLFormat.java b/src/main/java/org/opencadc/tap/impl/RubinURLFormat.java new file mode 100644 index 0000000..0118771 --- /dev/null +++ b/src/main/java/org/opencadc/tap/impl/RubinURLFormat.java @@ -0,0 +1,43 @@ +package org.opencadc.tap.impl; + +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/src/main/resources/PluginFactory.properties b/src/main/resources/PluginFactory.properties index 0436817..bd7b92d 100644 --- a/src/main/resources/PluginFactory.properties +++ b/src/main/resources/PluginFactory.properties @@ -13,7 +13,7 @@ ca.nrc.cadc.tap.MaxRecValidator.impl = org.opencadc.tap.impl.MaxRecValidatorImpl ca.nrc.cadc.tap.TableWriter = org.opencadc.tap.impl.RubinTableWriter -ca.nrc.cadc.tap.writer.format.FormatFactory = ca.nrc.cadc.tap.writer.format.OracleFormatFactory +ca.nrc.cadc.tap.writer.format.FormatFactory = org.opencadc.tap.impl.RubinFormatFactory ca.nrc.cadc.tap.upload.datatype.DatabaseDataType = ca.nrc.cadc.tap.upload.datatype.OracleDataType # the package name here could be changed now that we have a config file