Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PMPI Client for Java #1

Merged
merged 1 commit into from
May 24, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions PMPIConnector/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.logicoy.pdmp.pmpi</groupId>
<artifactId>PMPIConnector</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>PMPIConnector</name>
<url>http://maven.apache.org</url>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.3</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.47</version>
</dependency>
<dependency>
<groupId>org.glassfish.metro</groupId>
<artifactId>webservices-rt</artifactId>
<version>2.1.1</version>
</dependency>
</dependencies>
</project>

Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.logicoy.pdmp.pmpi;


import com.sun.xml.bind.marshaller.NamespacePrefixMapper;
import java.io.StringReader;
import java.io.StringWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.nabp.pmpi.DataElementsType;
import org.nabp.pmpi.DisclosingPmpRequestType;
import org.nabp.pmpi.DisclosingPmpsType;
import org.nabp.pmpi.SearchParametersType;
import org.nabp.pmpi.SearchRequestType;
import org.nabp.pmpi.SearchingPmpType;
import org.nabp.pmpi.UserRoleTypeEnumeration;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

/**
*
* @author Vishnu
*/
public class PMIX2PMPIQueryConstructor {

private static final Logger logger = Logger.getLogger(PMIX2PMPIQueryConstructor.class.getName());

public static String constructSearchRequest(String AESEncryptedRequest,
String RsaEncryptedAesKey,
String Sender,
String Receiver,
String UnencryptedData_for_table_of_contents){

List<String> dataElementsList = getXpathsFromPMIX(UnencryptedData_for_table_of_contents);


SearchRequestType searchRequest = new SearchRequestType(); //Top Element


SearchingPmpType searchingPmp = new SearchingPmpType();
searchingPmp.setInterconnectPmpId(new BigInteger(Sender));
searchingPmp.setPmpUserRole(UserRoleTypeEnumeration.PHARMACIST);
searchingPmp.setPmpSearchId("YOURPMPSIDFORTHEREQUEST");
searchRequest.setSearchingPmp(searchingPmp); //SearchingPmp

DisclosingPmpsType disclosingPmps = new DisclosingPmpsType();
DisclosingPmpRequestType disclosingPmp = new DisclosingPmpRequestType();
disclosingPmp.getInterconnectPmpId().add(new BigInteger(Receiver));
disclosingPmp.setRsaEncryptedAesKey(RsaEncryptedAesKey);
disclosingPmps.getDisclosingPmp().add(disclosingPmp);
searchRequest.setDisclosingPmps(disclosingPmps);

SearchParametersType searchParameters = new SearchParametersType();
DataElementsType dataElements = new DataElementsType();

for(String dataElement : dataElementsList){
dataElements.getDataElement().add(dataElement);
}
searchParameters.setDataElements(dataElements);
searchParameters.setAesEncryptedData(AESEncryptedRequest);
searchRequest.setSearchParameters(searchParameters);



StringWriter strWriter = new StringWriter();
try {
JAXBContext ctx = JAXBContext.newInstance(searchRequest.getClass());
Marshaller marshaller = ctx.createMarshaller();
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",new MyNamespacePrefixMapper());
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

marshaller.marshal(new JAXBElement(new QName("http://pmpi.nabp.org/", "SearchRequest","pmpi"), SearchRequestType.class, searchRequest),strWriter);



//JAXB.marshal(searchRequest, strWriter);
//JAXB.marshal(new JAXBElement(new QName("http://pmpi.nabp.org/", "SearchRequest","pmpi"), SearchRequestType.class, searchRequest),strWriter);
} catch (JAXBException ex) {
Logger.getLogger(PMIX2PMPIQueryConstructor.class.getName()).log(Level.SEVERE, null, ex);
}
//JAXB.marshal(searchRequest, strWriter);
//JAXB.marshal(new JAXBElement(new QName("http://pmpi.nabp.org/", "SearchRequest","pmpi"), SearchRequestType.class, searchRequest),strWriter);

return strWriter.toString();


}
public static List<String> getXpathsFromPMIX(String pmixXml){

try{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
MyDefaultHandler handler = new MyDefaultHandler();
saxParser.parse(new InputSource(new StringReader(pmixXml)),handler);
return handler.getXPaths();
}catch(Exception e){
throw new RuntimeException(e);
}

}

}

class MyDefaultHandler extends DefaultHandler{

private static final Logger logger = Logger.getLogger(MyDefaultHandler.class.getName());
ArrayList<String> xpaths = new ArrayList<String>();
HashMap<String,String> prefixMap = new HashMap<String,String>();

String currentXPath = "";

private int elementNo = 0;

public void startElement(String uri, String localName,String qName,
Attributes attributes) throws SAXException {

logger.finest("Started Element : " + qName);
elementNo++;
this.currentXPath = this.currentXPath + "/" + qName;
if(elementNo == 1){
return;
}

xpaths.add(this.currentXPath);


}

public void startPrefixMapping(String prefix, String uri)
throws SAXException{
logger.finest("Started Prefix Mapping : " + prefix + ":" + uri);
prefixMap.put(uri, prefix);
}

public void endElement(String uri, String localName, String qName)
throws SAXException{
logger.finest("Ended Element : " + qName);
this.currentXPath = this.currentXPath.replace("/" + qName,"");
}

public List<String> getXPaths(){
return this.xpaths;
}

}

class MyNamespacePrefixMapper extends NamespacePrefixMapper{

HashMap<String,String> nsMap = new HashMap<String,String>();

public MyNamespacePrefixMapper(){
nsMap.put("http://pmpi.nabp.org/","pmpi");
}


@Override
public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requiredPrefix) {
return nsMap.get(namespaceUri);
}

}

Loading