001/** 002 * Copyright (C) 2014 Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/ 003 * 004 * This file is part of Dicoogle/dicoogle-sdk-ext. 005 * 006 * Dicoogle/dicoogle-sdk-ext is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * Dicoogle/dicoogle-sdk-ext is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with Dicoogle. If not, see <http://www.gnu.org/licenses/>. 018 */ 019package pt.ua.dicoogle.sdk.p2p.Messages.Builders; 020 021import pt.ua.dicoogle.sdk.p2p.Messages.MessageFields; 022import pt.ua.dicoogle.sdk.p2p.Messages.MessageI; 023import pt.ua.dicoogle.sdk.p2p.Messages.MessageType; 024import pt.ua.dicoogle.sdk.p2p.Messages.MessageXML; 025import pt.ua.dicoogle.sdk.p2p.Messages.SearchResultFields; 026import java.io.ByteArrayOutputStream; 027import java.io.IOException; 028import java.io.UnsupportedEncodingException; 029import java.util.Collection; 030import java.util.HashMap; 031import java.util.List; 032import java.util.Set; 033import org.dom4j.Document; 034import org.dom4j.DocumentFactory; 035import org.dom4j.Element; 036import org.dom4j.io.OutputFormat; 037import org.dom4j.io.XMLWriter; 038import pt.ua.dicoogle.sdk.datastructs.SearchResult; 039 040/** 041 * 042 * @author Carlos Ferreira 043 * @author Pedro Bento 044 */ 045public class MessageBuilder 046{ 047 public MessageI buildQueryMessage(String query, Collection<String> extrafields, String pluginName, Integer queryNumber) throws IOException 048 { 049 DocumentFactory dfactory = new DocumentFactory(); 050 Document document = dfactory.createDocument("UTF-8"); 051 052 /** 053 * Creation of the root element of the document 054 */ 055 Element root = document.addElement(MessageFields.MESSAGE); 056 057 Element temp; 058 temp = root.addElement(MessageFields.MESSAGE_TYPE); 059 temp.setText(MessageType.QUERY); 060 061 temp = root.addElement(MessageFields.QUERY_NUMBER); 062 063 temp.setText(queryNumber.toString()); 064 065 temp = root.addElement(MessageFields.QUERY); 066 temp.setText(query); 067 068 for (String extrafield : extrafields) 069 { 070 temp = root.addElement(MessageFields.EXTRAFIELD); 071 temp.setText(extrafield); 072 } 073 074 OutputFormat format = new OutputFormat(" ", true, "UTF-8"); 075 ByteArrayOutputStream a = new ByteArrayOutputStream(); 076 077 XMLWriter output = null; 078 try 079 { 080 output = new XMLWriter(a, format); 081 } catch (UnsupportedEncodingException ex) 082 { 083 ex.printStackTrace(System.out); 084 } 085 086 output.write(document); 087 088 output.close(); 089 090 //just for tests 091 //System.out.println(a.toString()); 092 093 return new MessageXML(a.toByteArray()); 094 } 095 096 public MessageI buildQueryResponse(List<SearchResult> results, String queryNumber, String pluginName) throws IOException 097 { 098 DocumentFactory dfactory = new DocumentFactory(); 099 Document document = dfactory.createDocument("UTF-8"); 100 Element root = document.addElement(MessageFields.MESSAGE); 101 Element temp, temp2, temp3; 102 temp = root.addElement(MessageFields.MESSAGE_TYPE); 103 temp.setText(MessageType.QUERY_RESP); 104 105 temp = root.addElement(MessageFields.QUERY_NUMBER); 106 temp.setText(queryNumber); 107 temp = root.addElement(MessageFields.SEARCH_RESULTS); 108 for (SearchResult result : results) 109 { 110 temp2 = temp.addElement(MessageFields.SEARCH_RESULT); 111 112 temp3 = temp2.addElement(SearchResultFields.FILE_NAME); 113 temp3.setText(result.getURI().toString()); 114 115 temp3 = temp2.addElement(SearchResultFields.FILE_HASH); 116 temp3.setText(result.get("filehash").toString()); 117 //temp3 = temp2.addElement(SearchResultFields.FILE_PATH); 118 //temp3.setText(result.getPath()); 119 temp3 = temp2.addElement(SearchResultFields.FILE_SIZE); 120 temp3.setText(result.get("size").toString()); 121 122 temp3 = temp2.addElement(SearchResultFields.EXTRAFIELDS); 123 HashMap<String, Object> extrafields = result.getExtraData(); 124 Set<String> keys = extrafields.keySet(); 125 for (String key : keys) 126 { 127 temp2 = temp3.addElement(key); 128 /** FIXME: Probably need to replace other fields like \14 ? */ 129 temp2.setText(extrafields.get(key).toString().replaceAll("\0", " ")); 130 } 131 } 132 OutputFormat format = new OutputFormat(" ", true, "UTF-8"); 133 ByteArrayOutputStream a = new ByteArrayOutputStream(); 134 135 XMLWriter output = null; 136 try 137 { 138 output = new XMLWriter(a, format); 139 } catch (UnsupportedEncodingException ex) 140 { 141 ex.printStackTrace(System.out); 142 } 143 144 output.write(document); 145 146 output.close(); 147 return new MessageXML(a.toByteArray()); 148 } 149 150 public MessageI buildFileRequest(String filename, String fileHash, String pluginName) throws IOException 151 { 152 DocumentFactory dfactory = new DocumentFactory(); 153 Document document = dfactory.createDocument("UTF-8"); 154 Element root = document.addElement(MessageFields.MESSAGE); 155 Element temp; 156 temp = root.addElement(MessageFields.MESSAGE_TYPE); 157 temp.setText(MessageType.FILE_REQ); 158 temp = root.addElement(MessageFields.FILE_REQUESTED); 159 Element temp2 = temp.addElement(SearchResultFields.FILE_NAME); 160 temp2.setText(filename); 161 temp2 = temp.addElement(SearchResultFields.FILE_HASH); 162 temp2.setText(fileHash); 163 OutputFormat format = new OutputFormat(" ", true, "UTF-8"); 164 ByteArrayOutputStream a = new ByteArrayOutputStream(); 165 166 XMLWriter output = null; 167 try 168 { 169 output = new XMLWriter(a, format); 170 } catch (UnsupportedEncodingException ex) 171 { 172 ex.printStackTrace(System.out); 173 } 174 175 output.write(document); 176 177 output.close(); 178 return new MessageXML(a.toByteArray()); 179 } 180}