Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
topcat: subscribe to new experimental table.get.stil SAMP MType
This allows external clients to get a URL for one of the tables currently loaded into topcat. This functionality was suggested by @taldcroft in the discussion astropy/astropy#1920. The MType (table.get.stil) resembles the existing table.load.stil. Possibly there should be a table.get.votable etc, but that should probably be decided with consultation from the SAMP community. The machinery is present to add those - it would be a one-liner.
- Loading branch information
Showing
4 changed files
with
224 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
topcat/src/main/uk/ac/starlink/topcat/interop/TablePullHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package uk.ac.starlink.topcat.interop; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
import javax.swing.ListModel; | ||
import org.astrogrid.samp.ErrInfo; | ||
import org.astrogrid.samp.Message; | ||
import org.astrogrid.samp.Response; | ||
import org.astrogrid.samp.SampUtils; | ||
import org.astrogrid.samp.client.AbstractMessageHandler; | ||
import org.astrogrid.samp.client.HubConnection; | ||
import org.astrogrid.samp.httpd.ServerResource; | ||
import uk.ac.starlink.table.StarTable; | ||
import uk.ac.starlink.table.StarTableOutput; | ||
import uk.ac.starlink.table.StarTableWriter; | ||
import uk.ac.starlink.topcat.ControlWindow; | ||
import uk.ac.starlink.topcat.TopcatModel; | ||
|
||
/** | ||
* Message handler for table pull messages. | ||
* These return a reponse giving the URL of a table held by topcat. | ||
* | ||
* @author Mark Taylor | ||
* @since 28 Feb 2014 | ||
*/ | ||
public abstract class TablePullHandler extends AbstractMessageHandler { | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param mtype SAMP Mtype | ||
*/ | ||
protected TablePullHandler( String mtype ) { | ||
super( mtype ); | ||
} | ||
|
||
/** | ||
* If it's a Response, use it as is, if not wrap it up as a success. | ||
*/ | ||
@Override | ||
protected Response createResponse( Map processOutput ) { | ||
return processOutput instanceof Response | ||
? (Response) processOutput | ||
: super.createResponse( processOutput ); | ||
} | ||
|
||
@Override | ||
public Map processCall( HubConnection conn, String senderId, Message msg ) | ||
throws IOException { | ||
StarTableWriter twriter = getTableWriter( msg ); | ||
|
||
/* Parameter "index" gives the ID number of the table in topcat. */ | ||
String indexObj = (String) msg.getParam( "index" ); | ||
int index = indexObj == null ? -1 : SampUtils.decodeInt( indexObj ); | ||
TopcatModel tcModel = getTableByID( index ); | ||
|
||
/* If there's no table, return an error response. */ | ||
if ( tcModel == null ) { | ||
String errtxt = index <= 0 ? "No current table" | ||
: "No table with ID " + index; | ||
return Response.createErrorResponse( new ErrInfo( errtxt ) ); | ||
} | ||
|
||
/* Otherwise turn it into an HTTP resource and send its URL as a | ||
* response value. */ | ||
else { | ||
StarTable table = tcModel.getApparentStarTable(); | ||
String fname = "t" + index; | ||
ServerResource resource = | ||
TableSendActionManager.createTableResource( table, twriter ); | ||
URL turl = | ||
TopcatServer.getInstance().addResource( fname, resource ); | ||
Map result = new LinkedHashMap(); | ||
result.put( "url", turl.toString() ); | ||
return Response.createSuccessResponse( result ); | ||
} | ||
} | ||
|
||
/** | ||
* Returns a table writer to use for the given message. | ||
* | ||
* @param msg message | ||
* @return table serializer | ||
*/ | ||
protected abstract StarTableWriter getTableWriter( Message msg ) | ||
throws IOException; | ||
|
||
/** | ||
* Returns a message handler for a given fixed table format. | ||
* | ||
* @param mtype SAMP Mtype | ||
* @param twriter serializer defining table format | ||
* @return new table pull handler | ||
*/ | ||
public static TablePullHandler | ||
createFormatTablePullHandler( String mtype, | ||
final StarTableWriter twriter ) { | ||
return new TablePullHandler( mtype ) { | ||
protected StarTableWriter getTableWriter( Message msg ) { | ||
return twriter; | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Returns a message handler where the table format is determined by | ||
* a "format" parameter in the incoming message. | ||
* | ||
* @param mtype SAMP Mtype | ||
* @return new table pull handler | ||
*/ | ||
public static TablePullHandler | ||
createGenericTablePullHandler( String mtype ) { | ||
final StarTableOutput sto = new StarTableOutput(); | ||
return new TablePullHandler( mtype ) { | ||
protected StarTableWriter getTableWriter( Message msg ) | ||
throws IOException { | ||
String fmtName = (String) msg.getRequiredParam( "format" ); | ||
return sto.getHandler( fmtName ); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* Returns the topcat table with a given ID, or the default one | ||
* if it's non-positive. | ||
* | ||
* @param id topcat table ID or non-positive value | ||
* @return topcat model or null if ID not used | ||
*/ | ||
private static TopcatModel getTableByID( int id ) throws IOException { | ||
ControlWindow cwin = ControlWindow.getInstance(); | ||
if ( id <= 0 ) { | ||
return cwin.getCurrentModel(); | ||
} | ||
else { | ||
ListModel listModel = cwin.getTablesListModel(); | ||
int nt = listModel.getSize(); | ||
for ( int it = 0; it < nt; it++ ) { | ||
TopcatModel tcModel = | ||
(TopcatModel) listModel.getElementAt( it ); | ||
if ( tcModel.getID() == id ) { | ||
return tcModel; | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters