Skip to content

Commit

Permalink
Reenable REST interface (without Jersey)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Børlum committed Nov 25, 2011
1 parent 5fca03e commit 7c73d0b
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 142 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/**
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Contributor(s): Contributors are attributed in the source code
* where applicable.
*
* The Original Code is "Stamdata".
*
* The Initial Developer of the Original Code is Trifork Public A/S.
*
* Portions created for the Original Code are Copyright 2011,
* Lægemiddelstyrelsen. All Rights Reserved.
*
* Portions created for the FMKi Project are Copyright 2011,
* National Board of e-Health (NSI). All Rights Reserved.
*/
package dk.nsi.stamdata;

import cucumber.junit.Cucumber;
Expand Down
24 changes: 24 additions & 0 deletions common/integration-test/src/test/java/dk/nsi/stamdata/Steps.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
/**
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Contributor(s): Contributors are attributed in the source code
* where applicable.
*
* The Original Code is "Stamdata".
*
* The Initial Developer of the Original Code is Trifork Public A/S.
*
* Portions created for the Original Code are Copyright 2011,
* Lægemiddelstyrelsen. All Rights Reserved.
*
* Portions created for the FMKi Project are Copyright 2011,
* National Board of e-Health (NSI). All Rights Reserved.
*/
package dk.nsi.stamdata;

import junit.framework.Assert;
Expand Down
18 changes: 0 additions & 18 deletions dodi/data-manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,23 +152,5 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.9.1</version>
</dependency>

<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-guice</artifactId>
<version>1.9.1</version>
</dependency>

<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.9.1</version>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Contributor(s): Contributors are attributed in the source code
* where applicable.
*
* The Original Code is "Stamdata".
*
* The Initial Developer of the Original Code is Trifork Public A/S.
*
* Portions created for the Original Code are Copyright 2011,
* Lægemiddelstyrelsen. All Rights Reserved.
*
* Portions created for the FMKi Project are Copyright 2011,
* National Board of e-Health (NSI). All Rights Reserved.
*/
package com.trifork.stamdata.importer.webinterface;

import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.trifork.stamdata.importer.parsers.ParserState;
import com.trifork.stamdata.importer.parsers.annotations.InboxRootPath;
import org.hibernate.mapping.Collection;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Type;
import java.util.Map;
import java.util.Set;

@Singleton
public class ParserServlet extends HttpServlet
{
private final Set<ParserState> parsers;
private final File inboxRoot;

@Inject
ParserServlet(Set<ParserState> parsers, @InboxRootPath String inboxRoot)
{
this.parsers = parsers;
this.inboxRoot = new File(inboxRoot);
}

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
String id = getIdFromQuery(request);
ParserState parser = getParserById(id);

String info = getInfoFromQuery(request);

if (info == null)
{
show(parser, response.getWriter());
}
else if (info.equals("inbox"))
{
inbox(parser, response.getWriter());
}
}

public void show(ParserState parser, Writer writer) throws IOException
{
Map<String, Object> values = Maps.newHashMap();

values.put("name", parser.name());
values.put("id", parser.identifier());
values.put("inProgress", parser.isInProgress());
values.put("locked", parser.isLocked());

String json = new Gson().toJson(values);

writer.write(json);
}

public void inbox(ParserState parser, Writer writer) throws IOException
{
String inboxPath = new File(inboxRoot, parser.identifier()).getAbsolutePath();
writer.write(inboxPath);
}

//
// Helpers
//

protected String getIdFromQuery(HttpServletRequest request)
{
return request.getParameter("id");
}

protected String getInfoFromQuery(HttpServletRequest request)
{
return request.getParameter("info");
}

protected ParserState getParserById(String id)
{
for (ParserState parser : parsers)
{
if (parser.identifier().equals(id))
{
return parser;
}
}

return null;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,29 @@
package com.trifork.stamdata.importer.webinterface;


import com.google.common.collect.Maps;
import com.sun.jersey.api.json.JSONConfiguration;
import com.sun.jersey.guice.JerseyServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
import com.google.inject.servlet.ServletModule;
import com.trifork.stamdata.ComponentMonitor;
import com.trifork.stamdata.MonitoringModule;

import java.util.Map;

/**
* Wrapper that routes requests to Jersey.
* Binds servlets to paths.
*/
public class WebInterfaceModule extends JerseyServletModule
public class WebInterfaceModule extends ServletModule
{
@Override
protected void configureServlets()
{
// Must configure at least one JAX-RS resource or the
// server will fail to start.
//
bind(ParsersController.class);

Map<String, String> initParams = Maps.newHashMap();
initParams.put(JSONConfiguration.FEATURE_POJO_MAPPING, "true");
serve("/parsers/*").with(GuiceContainer.class, initParams);

// Legacy
//
// Serve the status servlet.
//
bind(ComponentMonitor.class).to(DataManagerComponentMonitor.class);
install(new MonitoringModule());

// Serve a REST API for the parsers (mainly for testing).
//
serve("/parsers").with(ParserServlet.class);

// Serve the GUI.
//
serve("/").with(MonitorGuiServlet.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,13 @@ protected void placeInInbox(File inboxToCopy, boolean useSubdirectory) throws IO

private File getInboxPath()
{
String inboxPath = get(getParserURL() + "/inbox").andReturn().print();
String inboxPath = get(getParserURL() + "&info=inbox").andReturn().print();
return new File(inboxPath);
}

private String getParserURL()
{
return "http://127.0.0.1:" + port + "/parsers/" + parserId;
return "http://127.0.0.1:" + port + "/parsers?id=" + parserId;
}

protected File getDirectory(String path)
Expand Down

0 comments on commit 7c73d0b

Please sign in to comment.