Hello and thank you for reading this question:
I will explain the use case first and the approach I have tried:
I have an ArrayList which holds Courses'data and it is being created in the FrontServlet and it is being stored in the session.
I would like to convert the ArrayList to XML to show it as HTML transforming it with XSL.
Mi doubt is: how could we convert an ArrayList to XML?
I have studied how to annotate classes to transform them: https://www.mkyong.com/java/jaxb-hello-world-example/
I have used an static XML to convert it and show it: Java: Implementing Transform View pattern, to convert XML to HTML with an XSL file
I have read a good post about extending ArrayList<> to be able to transform it: Why my ArrayList is not marshalled with JAXB?
I first tried with:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* #author YonePC
*/
#XmlRootElement
public class Curso {
#XmlElement
public void setTitulo(String titulo) {
this.titulo = titulo;
}
#XmlElement
public void setAutor(String autor) {
this.autor = autor;
}
#XmlElement
public void setAsignatura(String asignatura) {
this.asignatura = asignatura;
}
#XmlElement
public void setDuracion(String duracion) {
this.duracion = duracion;
}
#XmlElement
public void setVideo(String video) {
this.video = video;
}
String titulo, autor, asignatura, duracion, video;
public Curso(String titulo, String autor, String asignatura, String duracion, String video) {
this.titulo = titulo;
this.autor = autor;
this.asignatura = asignatura;
this.duracion = duracion;
this.video = video;
}
public String getTitulo() {
return titulo;
}
public String getAutor() {
return autor;
}
public String getAsignatura() {
return asignatura;
}
public String getDuracion() {
return duracion;
}
public String getVideo() {
return video;
}
}
But I do not need the Curso class itself, I need to convert the ArrayList which stores them.
In addition, I currently stores the courses being created in the session. How could we get the session's info in a servlet? I mean, we can access them from the servlet where it has being currently created, and from JSP pages, but how do we do access into servlets?
I ask the previous question because in all the examples I found about converting Java to XML, they showed new instances and not how to convert previous stored ones.
The code I have tried:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package frontController;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.Curso;
/**
*
* #author YonePC
*/
#WebServlet(name = "CourseInfoCommand", urlPatterns = {"/CourseInfoCommand"})
public class CourseInfoCommand extends FrontCommand {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
*
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* #return a String containing servlet description
*/
public String getServletInfo() {
return "Short description";
}// </editor-fold>
#Override
public void process(HttpServletRequest request) {
try {
File file = new File("C:\\Users\\YonePC\\Videos\\ASAPLICACIONCURSOSPRACTICA1\\src\\java\\frontController\\Cursos.xml");
ArrayList courses = (ArrayList) session.getAttribute("cursos");
JAXBContext jaxbContext = JAXBContext.newInstance(ArrayListCourses.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.marshal(courses, file);
TransformerFactory factory = TransformerFactory.newInstance();
StreamSource xsl = new StreamSource(new File(""));
Transformer newTransformer = factory.newTransformer(xsl);
StreamSource xml = new StreamSource(new File(""));
PrintWriter writer = response.getWriter();
Result result = new StreamResult(writer);
newTransformer.transform(xml, result);
writer.println(writer.toString());
forward("/CourseInfo.jsp");
} catch (ServletException ex) {
Logger.getLogger(CourseInfoCommand.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CourseInfoCommand.class.getName()).log(Level.SEVERE, null, ex);
} catch (TransformerConfigurationException ex) {
Logger.getLogger(CourseInfoCommand.class.getName()).log(Level.SEVERE, null, ex);
} catch (TransformerException ex) {
Logger.getLogger(CourseInfoCommand.class.getName()).log(Level.SEVERE, null, ex);
} catch (JAXBException ex) {
Logger.getLogger(CourseInfoCommand.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Thank you for your help.
You can use xstream to transform the object to xml.
Maven dependency:
<dependency>
<groupId>xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.2.2</version>
</dependency>
XMLTransformer:
public class XMLTransformer {
private XStream xstream = null;
private XMLTransformer() {
xstream = new XStream();
}
public static XMLTransformer getInstance() {
return new XMLTransformer();
}
public String toXMLString(Object object) {
return xstream.toXML(object);
}
public Object toObject(String xml) {
return (Object) xstream.fromXML(xml);
}
}
Main class
public class Main {
public static void main(String[] args) {
XMLTransformer transformer=XMLTransformer.getInstance();
Employee e1=new Employee("gati","id");
Employee e2=new Employee("gati","id");
Employee e3=new Employee("gati","id");
List<Employee> employeeList =new ArrayList<>();
employeeList.add(e1);
employeeList.add(e2);
employeeList.add(e3);
String str=transformer.toXMLString(employeeList);
System.out.println(str);
}
}
public class Employee {
private String name;
private String email;
public Employee(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
Related
I have been trying out to invoke and querying transactions from the Java SDK for the BYFN network in Hyperledger Fabric v1.4.4
So far i have started the network (which has two peers in two orgs, an orderer and chaincode mycc installed).
The network starts successfully, and scripts and tests complete (As per the end of execution the values of a and b after the byfn.sh scripts runs is 90 and 210 respectively)
Now I have my Java SDK, through which the query proposal response works correctly (returns 90 and 210), but after moving (doing the invoke) request and then querying the value returned is still 90, there is no change, I am not sure what I am doing wrong here.
My code:
public class sdksample {
// Main program to simply call the client, Am i making some mistake here ?? persistency ?
public static void main(String[] args) throws Exception {
// create fabric-ca client
BlockChainHFClient.getInstance().setupCryptoMaterialsForClient();
BlockChainHFClient.getInstance().initChannel();
// get HFC client instance
BlockChainHFClient client = BlockChainHFClient.getInstance();
System.out.println(client);
}
}
HFClient
import static java.lang.String.format;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.io.IOUtils;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.hyperledger.fabric.protos.ledger.rwset.kvrwset.KvRwset;
import org.hyperledger.fabric.sdk.BlockInfo;
import org.hyperledger.fabric.sdk.BlockInfo.EnvelopeType;
import org.hyperledger.fabric.sdk.ChaincodeID;
import org.hyperledger.fabric.sdk.Channel;
import org.hyperledger.fabric.sdk.Enrollment;
import org.hyperledger.fabric.sdk.HFClient;
import org.hyperledger.fabric.sdk.Orderer;
import org.hyperledger.fabric.sdk.Peer;
import org.hyperledger.fabric.sdk.ProposalResponse;
import org.hyperledger.fabric.sdk.QueryByChaincodeRequest;
import org.hyperledger.fabric.sdk.SDKUtils;
import org.hyperledger.fabric.sdk.TransactionProposalRequest;
import org.hyperledger.fabric.sdk.TxReadWriteSetInfo;
import org.hyperledger.fabric.sdk.User;
import org.hyperledger.fabric.sdk.exception.BaseException;
import org.hyperledger.fabric.sdk.exception.CryptoException;
import org.hyperledger.fabric.sdk.exception.InvalidArgumentException;
import org.hyperledger.fabric.sdk.exception.InvalidProtocolBufferRuntimeException;
import org.hyperledger.fabric.sdk.exception.ProposalException;
import org.hyperledger.fabric.sdk.exception.TransactionException;
import org.hyperledger.fabric.sdk.security.CryptoSuite;
import com.google.protobuf.InvalidProtocolBufferException;
public class BlockChainHFClient {
private static BlockChainHFClient instance;
/**
* Client instance constructor
*/
private BlockChainHFClient() {
}
/**
* Returns an instance of the Fabric client
*
* #return instance
*/
public static synchronized BlockChainHFClient getInstance() {
if (instance == null) {
instance = new BlockChainHFClient();
}
return instance;
}
/**
* Fabric client object
*/
final HFClient hfClient = HFClient.createNewInstance();
/**
* Crypto config folder location . keep crypto-config folder in user/home
*/
final String CRYPTO_CONFIG_HOME_DIR = System.getProperty("user.home");
/**
* Grpcs URL
*/
final String GRPCS = "grpcs://";
/**
* Dot for utility
*/
final String DOT = ".";
/**
* MSP ID Root
*/
final String ROOT_MSP_ID = "Org1MSP";
/**
* Admin user
*/
final String PEER_ADMIN = "PeerAdmin";
/**
* Channel object
*/
Channel channel;
/**
* Channel initialize timeout values
*/
final Long ChannelBuilderOptionkeepAliveMinutes = 15L;
final Long ChannelBuilderOptionkeepAliveSeconds = 15L;
/**
* Get channel instance
*
* #return channel
*/
public Channel getChannel() {
return channel;
}
/**
* Get HF client
*
* #return HF client
*/
public HFClient getClient() {
return hfClient;
}
/**
* Set up User contexts by using Crypto materials - Private key and cert files
*
* #throws CryptoException
* #throws InvalidArgumentException
* #throws IllegalAccessException
* #throws InstantiationException
* #throws ClassNotFoundException
* #throws NoSuchMethodException
* #throws InvocationTargetException
*/
public void setupCryptoMaterialsForClient()
throws CryptoException, InvalidArgumentException, IllegalAccessException, InstantiationException,
ClassNotFoundException, NoSuchMethodException, InvocationTargetException {
hfClient.setCryptoSuite(CryptoSuite.Factory.getCryptoSuite());
hfClient.setUserContext(new User() {
public String getName() {
return PEER_ADMIN;
}
public Set<String> getRoles() {
return null;
}
public String getAccount() {
return null;
}
public String getAffiliation() {
return null;
}
public Enrollment getEnrollment() {
return new Enrollment() {
public PrivateKey getKey() {
PrivateKey privateKey = null;
File privateKeyFile = findFileSk(
"D:\\Hyperledger Fabric_Research_Blockchain\\Fabric1.4.4\\fabric-samples\\first-network\\crypto-config\\peerOrganizations\\org1.example.com\\users\\Admin#org1.example.com\\msp\\keystore");
try {
privateKey = getPrivateKeyFromBytes(
IOUtils.toByteArray(new FileInputStream(privateKeyFile)));
} catch (IOException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidKeySpecException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return privateKey;
}
public String getCert() {
String certificate = null;
try {
File certificateFile = new File(
"D:\\Hyperledger Fabric_Research_Blockchain\\Fabric1.4.4\\fabric-samples\\first-network\\crypto-config\\peerOrganizations\\org1.example.com\\users\\Admin#org1.example.com\\msp\\signcerts\\Admin#org1.example.com-cert.pem");
certificate = new String(IOUtils.toByteArray(new FileInputStream(certificateFile)),
"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return certificate;
}
};
}
public String getMspId() {
return ROOT_MSP_ID;
}
});
}
public void initChannel() throws Exception {
Properties peerProperties = new Properties();
peerProperties.setProperty("pemFile",
"D:\\Hyperledger Fabric_Research_Blockchain\\Fabric1.4.4\\fabric-samples\\first-network\\crypto-config\\peerOrganizations\\org1.example.com\\peers\\peer0.org1.example.com\\tls\\server.crt");
peerProperties.setProperty("trustServerCertificate", "true"); // testing environment only NOT FOR PRODUCTION!
peerProperties.setProperty("hostnameOverride", "peer0.org1.example.com");
peerProperties.setProperty("sslProvider", "openSSL");
peerProperties.setProperty("negotiationType", "TLS");
peerProperties.put("grpc.NettyChannelBuilderOption.maxInboundMessageSize", 9000000);
Peer peer = hfClient.newPeer("peer0.org1.example.com", "grpc://localhost:7051");
Properties ordererProperties = new Properties();
ordererProperties.setProperty("pemFile",
"D:\\Hyperledger Fabric_Research_Blockchain\\Fabric1.4.4\\fabric-samples\\first-network\\crypto-config\\ordererOrganizations\\example.com\\orderers\\orderer.example.com\\tls\\server.crt");
ordererProperties.setProperty("trustServerCertificate", "true"); // testing environment only NOT FOR PRODUCTION!
ordererProperties.setProperty("hostnameOverride", "orderer.example.com");
ordererProperties.setProperty("sslProvider", "openSSL");
ordererProperties.setProperty("negotiationType", "TLS");
ordererProperties.put("grpc.NettyChannelBuilderOption.keepAliveTime", new Object[] { 5L, TimeUnit.MINUTES });
ordererProperties.put("grpc.NettyChannelBuilderOption.keepAliveTimeout", new Object[] { 8L, TimeUnit.SECONDS });
Orderer orderer = hfClient.newOrderer("orderer.example.com", "grpc://localhost:7050");
Channel channel = hfClient.newChannel("mychannel");
channel.addPeer(peer);
channel.addOrderer(orderer);
channel.initialize();
moveUnits(hfClient);
queryBlockChain(hfClient);
}
private String printableString(String string) {
int maxLogStringLength = 10000;
if (string == null || string.length() == 0) {
return string;
}
String ret = string.replaceAll("[^\\p{Print}]", "\n");
ret = ret.substring(0, Math.min(ret.length(), maxLogStringLength))
+ (ret.length() > maxLogStringLength ? "..." : "");
return ret;
}
void queryBlockChain(HFClient client) throws ProposalException, InvalidArgumentException {
// get channel instance from client
Channel channel = client.getChannel("mychannel");
// create chaincode request
QueryByChaincodeRequest qpr = client.newQueryProposalRequest();
// build cc id providing the chaincode name. Version is omitted here.
ChaincodeID cid = ChaincodeID.newBuilder().setName("mycc").build();
qpr.setChaincodeID(cid);
// CC function to be called
qpr.setFcn("query");
qpr.setArgs(new String[] { "a" });
Collection<ProposalResponse> res = channel.queryByChaincode(qpr);
// display response
for (ProposalResponse pres : res) {
String stringResponse = new String(pres.getChaincodeActionResponsePayload());
System.out.println(stringResponse);
}
}
void moveUnits(HFClient client)
throws Exception {
Channel channel = client.getChannel("mychannel");
TransactionProposalRequest req = client.newTransactionProposalRequest();
ChaincodeID cid = ChaincodeID.newBuilder().setName("mycc").setVersion("1.0").build();
req.setChaincodeID(cid);
req.setFcn("invoke");
req.setArgs(new String[] {"a","b","50"});
Collection<ProposalResponse> resps = channel.sendTransactionProposal(req);
channel.sendTransaction(resps);
}
/**
* Utility method to get private key from bytes using Bouncy Castle Security
* Provider
*
* #param data
* #return privateKey
* #throws IOException
* #throws NoSuchProviderException
* #throws NoSuchAlgorithmException
* #throws InvalidKeySpecException
*/
private PrivateKey getPrivateKeyFromBytes(byte[] data)
throws IOException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException {
final Reader pemReader = new StringReader(new String(data));
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
final PrivateKeyInfo pemPair;
try (PEMParser pemParser = new PEMParser(pemReader)) {
pemPair = (PrivateKeyInfo) pemParser.readObject();
}
PrivateKey privateKey = new JcaPEMKeyConverter().setProvider(BouncyCastleProvider.PROVIDER_NAME)
.getPrivateKey(pemPair);
return privateKey;
}
/**
* Find files ending with _sk
*
* #param directorys
* #return file
*/
private File findFileSk(String directorys) {
File directory = new File(directorys);
File[] matches = directory.listFiles((dir, name) -> name.endsWith("_sk"));
if (null == matches) {
throw new RuntimeException(
format("Matches returned null does %s directory exist?", directory.getAbsoluteFile().getName()));
}
if (matches.length != 1) {
throw new RuntimeException(format("Expected in %s only 1 sk file but found %d",
directory.getAbsoluteFile().getName(), matches.length));
}
return matches[0];
}
}
There seems to be no errors, the moveUnits function seems to work.
But the query returns the same values.
Suggestions? Thanks !
Worked. apparently i had to add another peer (peer0.org2.example.com) to the channel so as to satisfy the endorsement policy that two peers had to validate. The invoke then worked !
So my Problem is that I have to create a Singleton pattern counter for numeric name giving. For example "1", "2", "3" etc. The idea is that every time i start the application and the Server(tomcat), it gets the last number and when I upload another image it should continue from there. Lets say the last one was "43", so the the next time I start the application it should know it and put "44" for the next image upload.
I'm not that good in Java so please give me some patience :)
This is my FileUploadServlet. It handles the request from the fileUploadForm.jsp by taking the file from the submit.
package upload;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import utils.FormatChecker;
import utils.UnzipFile;
//Servlet for handling the Upload request from the Index.jsp
#MultipartConfig
public class FileUploadServlet extends HttpServlet {
// Instace of the FileUpload object
private FileUploader uploader = new FileUploader();
// Instance of the FormatChecker object
private FormatChecker checker = new FormatChecker();
// Instance of the UnzipFile object
private UnzipFile unzip = new UnzipFile();
private static final long serialVersionUID = 1L;
private static final String SAVE_FOLDER = "C:\\Users\\cuche\\Desktop\\tomcat\\apache-tomcat-7.0.47\\webapps\\files";
/**
* #see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("error.jsp");
}
/**
* #see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String contentType;
boolean isFormatValid;
Part filePart = request.getPart("file");
contentType = filePart.getContentType();
ServletContext context = getServletContext();
String appPath = context.getRealPath("/");
String fileNameOld = getFileName(filePart);
String fileNameNew = appPath + fileNameOld;
isFormatValid = checker.check(contentType);
pleas ignore the part with the FileUnziper
if (isFormatValid == true) {
if (contentType == ("application/x-zip-compressed")) {
unzip.FileUnziper(fileNameNew, SAVE_FOLDER);
} else {
//gets the content and saves in form of a stream
InputStream fileContent = filePart.getInputStream();
//using the uploadImage method of uploader class
uploader.uploadImage(fileNameNew, fileContent);
}
try {
response.sendRedirect("result.jsp");
} catch (IOException ex) {
response.getWriter().append(ex.getLocalizedMessage());
}
} else {
response.getWriter().append("Format is wrong");
}
}
// method for removing header for proper file upload
private String getFileName(Part part) {
for (String cd : part.getHeader("content-disposition").split(";")) {
if (cd.trim().startsWith("filename")) {
String filename = cd.substring(cd.indexOf('=') + 1).trim()
.replace("\"", "");
return filename.substring(filename.lastIndexOf('/') + 1)
.substring(filename.lastIndexOf('\\') + 1); // MSIE fix.
}
}
return null;
}
}
This is my FileUploader class
package upload;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
* #author Iwan Cuche
* #date:
*/
public class FileUploader {
/**
* This method reads a File
*
* #param fileName
* #param stream
*/
public void uploadImage(String fileName, InputStream stream)
throws IOException {
try {
File file = new File(fileName);
OutputStream os = new FileOutputStream(file);
int data;
while ((data = stream.read()) != -1) {
os.write(data);
}
os.flush();
os.close();
System.out.println("Uploaded file successfully saved in "
+ file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
throw e;
}
}
}
This is my Singleton class
package utils;
public class ServerCounter {
private static ServerCounter INSTANCE = new ServerCounter();
private ServerCounter() {};
public static ServerCounter getInstance() {
return INSTANCE;
}
}
I hope someone can help me because I'm not sure how to go at it.
In ServerCounter, add
private final AtomicLong counter = new AtomicLong();
public String nextval() { return String.valueOf(counter.incrementAndGet()); }
Each time you call INSTANCE.nextval() you'll get a fresh numeric string.
Clearly, each time you restart your application, the counter will restart.
ok, first you have to persist your counter if you want to get it after tomcat shutdown. we need listener for tomcat:
package utils;
public class ContextListener implements ServletContextListener{
void contextInitialized(ServletContextEvent sce){ // we could call loadFromFile here as well
}
//will be executed at tomcat shutdown
void contextDestroyed(ServletContextEvent sce){
ServerCounter .getInstance().writeToFile();
}
}
now the singleton(like in Marko's answer:)):
package utils;
public class ServerCounter {
private static ServerCounter INSTANCE = new ServerCounter();
private final AtomicLong counter;
private ServerCounter() {
//load value from file, do you need help by it?
long value = this.loadCounterFromFile();
counter = new AtomicLong(value);
};
private long loadCounterFromFile(){
BufferedReader br = null;
try {
//no problem if there is no file, we will return 0 in this case
br = new BufferedReader(new FileReader("C:\\Test\\counter.txt"));
String line = br.readLine();
if(line != null && line.length() > 0)
return Long.parseLong(line);
return 0;
//catch all exceptionse, because we could get NumberFormatException or FileNotFound from parseLong
} catch (Exception e) {
return 0;
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public static ServerCounter getInstance() {
return INSTANCE;
}
public String nextval() { return String.valueOf(counter.incrementAndGet()); }
//will be executed by listener
public void writeToFile(){
//write the counter to file
writeToFile(counter.get());
}
private void writeToFile(long value){
try{
//you need folder c:\Test, file will be created automatically if there is no file, it will override the old file
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Test\\counter.txt"));
//need "" to create String
bw.write("" + value);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
now you can use ServerCounter.getInstance.nextval() to increment the counter and get the value
last thing is, you need to put the listener to your webApplication:
<web-app>
...
<listener>
<listener-class>utils.ContextListener </listener-class>
</listener>
</web-app>
EDIT: ServerCounter was implementing ServletContextListener by mistake
EDIT2: added read/write file
I have a JSP file, there backend helper class to it. From the back end helper I need to send PDF file to the JSP as an attachment. How can I achieve that?
I would suggest you to use Apache Commons File Upload component. That's probably the best way rather than reinventing the wheel. ;)
Since you haven't told us if you're using any MVC framework or just plain Servlet, I'll go for the basics.
If you want to upload a file, Apache Commons File Upload is the best library that will translate your multipart/form-data encoded HttpServletRequest message and provide you with files uploaded (in InputStream format, mostly preferred).
It's up to you, the developer, to write the data back to a persistent storage of your choice.
The reverse, it's to take the file, get the appropriate MIME-Type, Content-Length (file size), and file data (InputStream, if possible) and render it back to the HttpServletResponse).
This code (fully functional and written by me) does put the file as attachment/inline.
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
/**
* #author Buhake Sindi (The Elite Gentleman)
* #since 01 September 2011
*/
public class FileServletRenderer implements ServletRenderer {
private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB
private static final String OCTECT_STREAM_MIME_TYPE = "application/octect-stream";
private String contentType = OCTECT_STREAM_MIME_TYPE;
private long contentLength;
private String contentDisposition = "inline";
private String fileName;
private InputStream inputStream;
/**
* #return the contentType
*/
public String getContentType() {
return contentType;
}
/**
* #param contentType
* the contentType to set
*/
public void setContentType(String contentType) {
this.contentType = contentType;
}
/**
* #return the contentLength
*/
public long getContentLength() {
return contentLength;
}
/**
* #param contentLength
* the contentLength to set
*/
public void setContentLength(long contentLength) {
this.contentLength = contentLength;
}
/**
* #return the contentDisposition
*/
public String getContentDisposition() {
return contentDisposition;
}
/**
* #param contentDisposition
* the contentDisposition to set
*/
public void setContentDisposition(String contentDisposition) {
this.contentDisposition = contentDisposition;
}
/**
* #return the fileName
*/
public String getFileName() {
return fileName;
}
/**
* #param fileName
* the fileName to set
*/
public void setFileName(String fileName) {
this.fileName = fileName;
}
/**
* #return the inputStream
*/
public InputStream getInputStream() {
return inputStream;
}
/**
* #param inputStream
* the inputStream to set
*/
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
public void setFile(File file) throws IOException {
if (file == null) {
throw new IOException("file is null.");
}
setInputStream(new BufferedInputStream(new FileInputStream(file)));
setContentLength(file.length());
}
/*
* (non-Javadoc)
*
* #see org.bfs.bayweb.util.renderer.ServletViewRenderer#render(javax.servlet.
* ServletRequest, javax.servlet.ServletResponse)
*/
public void render(ServletRequest request, ServletResponse response) throws IOException {
// TODO Auto-generated method stub
BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
try {
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int inputStreamLength = 0;
int length = 0;
if (contentType == null) {
contentType = request.getServletContext().getMimeType(getFileName());
}
//We couldn't determine Content-Type
if (contentType == null) {
contentType = OCTECT_STREAM_MIME_TYPE;
}
while ((length = getInputStream().read(buffer)) > 0) {
inputStreamLength += length;
bos.write(buffer, 0, length);
}
if (inputStreamLength != getContentLength()) {
setContentLength(inputStreamLength);
}
if (response instanceof HttpServletResponse) {
HttpServletResponse httpResponse = (HttpServletResponse) response;
httpResponse.reset();
httpResponse.setHeader("Content-Type", getContentType());
httpResponse.setHeader("Content-Length", String.valueOf(getContentLength()));
httpResponse.setHeader("Content-Disposition", "\"" + getContentDisposition() + "\""
+ ((getFileName() != null && !getFileName().isEmpty()) ? "; filename=\"" + getFileName() + "\"" : ""));
httpResponse.setHeader("Content-Type", getContentType());
}
// finally
bos.flush();
// clear
} finally {
// TODO Auto-generated catch block
close(bos);
close(getInputStream());
}
}
private void close(Closeable resource) throws IOException {
if (resource != null) {
resource.close();
}
}
}
The most important method is render(HttpServletRequest, HttpServletResponse).
I am having xsd schema..How can I generate the xml by using this schema programmatically in java..? and it should be dynamic ,means I can give any schema.
Is there any library available to do same.?
I have alrady seen the other post also,but unfortunately it did not suit me.. please give tour ideas....???
I am not finding any approach to do same.
XPath is not a tool for generating XML. I'm afraid you're trying to accomplish your goal with the wrong tools. So, I think the answer to your question is: you can't.
Take a look at this link :
JAVA: Build XML document using XPath expressions
And go through this link under the XPath heading :
http://www.vogella.de/articles/JavaXML/article.html
I think this should help you out...:)
The following code performs validation as per your need,
and also tells you the line number where the error occurred during parsing
You will need following jars to run this code
Only code change needed is at validateSchema() method where request response args need to be replaced with a string representation of your xml and String representation of your XSD.
resolver.jar, xml-apis.jar, serializer.jar, xercesImpl.jar
import org.apache.xerces.parsers.SAXParser;
import org.apache.xerces.xni.Augmentations;
import org.apache.xerces.xni.NamespaceContext;
import org.apache.xerces.xni.XMLLocator;
import org.apache.xerces.xni.XNIException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Performs validation on XML based on the XSD. Overrides the
* {#link SAXParser}.
*
*
*/
public class ValidateSchema extends SAXParser {
/**
* Container for current line and coloum number being parsed in the XML.
*/
private XMLLocator locator;
/**
* Default public constructor.
*/
public ValidateSchema() {
super();
}
/**
* Used for obtaining the {#link XMLLocator} locator object.
*/
#Override
public void startDocument(
XMLLocator locator, String encoding, NamespaceContext namespaceContext, Augmentations augs)
throws XNIException {
this.locator = locator;
super.startDocument(locator, encoding, namespaceContext, augs);
}
/**
* Validates the XML against the provided XSD.
*
* #param req HttpServletRequest object.
* #param resp HttpServletResponse object.
* #throws IOException
*/
public void validateSchema(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
String content = req.getParameter("data");
String selectbox = req.getParameter("selectbox");
content = content.trim();
// Convert the XML string to byte stream.
InputStream is = new ByteArrayInputStream(content.getBytes());
try {
this.setFeature(Constants.VALIDATION_PROP, true);
this.setFeature(Constants.SCHEMA_PROP, true);
this.setFeature(Constants.DYNAMIC_PROP, true);
this.setFeature(Constants.SCHEMA_CHECKING_PROP, true);
if("1".equalsIgnoreCase(selectbox)) {
this.setProperty(Constants.SCHEMA_LOC,"oem.xsd" );
} else if("2".equalsIgnoreCase(selectbox)) {
this.setProperty(Constants.SCHEMA_LOC,"carrier.xsd" );
}
Validator handler = new Validator();
this.setErrorHandler(handler);
InputSource isp = new InputSource();
isp.setByteStream(is);
isp.setEncoding("UTF-8");
this.parse(isp);
if (handler.validationError == true) {
StringBuffer errorMessage = new StringBuffer(512);
errorMessage
.append("<div style='background: #ffebe6;border: 0px solid #ffe0d7;color:#c10000;height:60px;padding: 5px;'>")
.append(Constants.INVALID_XML_DOCUMENT)
.append(" LineNo: ")
.append(handler.saxParseException.getLineNumber())
.append(" ColumnNo: ")
.append(handler.saxParseException.getColumnNumber())
.append("<br/>")
.append(handler.validationError)
.append(handler.saxParseException.getMessage())
.append("</div>");
System.out.println( errorMessage );
} else {
StringBuffer validMsg = new StringBuffer(512);
validMsg.append("<div style='background: #ebeff9;border: 0px solid #6b90da;height:60px;padding: 5px;'>")
.append(Constants.VALID_XML_DOCUMENT).append("</div>");
System.out.println( validMsg );
}
} catch (SAXException e) {
StringBuffer errorMessage = new StringBuffer(512);
errorMessage
.append("<div style='background: #ffebe6;border: 0px solid #ffe0d7;color:#c10000;height:60px;padding: 5px;'>")
.append(Constants.INVALID_XML_DOCUMENT)
.append(" LineNo: ")
.append(this.locator.getLineNumber())
.append(" ColumnNo: ")
.append(this.locator.getColumnNumber())
.append(" <br/>")
.append(e.getMessage())
.append("</div>");
System.out.println( errorMessage );
} catch (Exception e) {
StringBuffer errorMessage = new StringBuffer(512);
errorMessage
.append("<div style='background: #ffebe6;border: 1px solid #ffe0d7;color:#c10000;height:60px;padding: 5px;'>")
.append(Constants.INVALID_XML_DOCUMENT)
.append(" <br/>")
.append(e.getMessage())
.append("</div>");
System.out.println( errorMessage );
}
}
/**
* Writes back the response to client.
*
* #param msg Response message.
* #param resp HttpServletResponse object.
* #throws IOException
*/
private void responseWrite(
String msg, HttpServletResponse resp) throws IOException {
resp.setContentType("text/html");
resp.getWriter().write(msg);
}
/**
* Custom handler for Errors while parsing documents.
*/
private class Validator extends DefaultHandler {
public boolean validationError = false;
public SAXParseException saxParseException = null;
/**
* #throws SAXException
*/
#Override
public void error(SAXParseException exception) throws SAXException {
validationError = true;
saxParseException = exception;
}
/**
* #throws SAXException
*/
#Override
public void fatalError(SAXParseException exception) throws SAXException {
validationError = true;
saxParseException = exception;
}
/**
* #throws SAXException
*/
#Override
public void warning(SAXParseException exception) throws SAXException {
}
}
}
Constants.Java contains the validation properties that you need to specify
public final class Constants {
public static final String VALIDATION_PROP = "http://xml.org/sax/features/validation";
public static final String SCHEMA_PROP = "http://apache.org/xml/features/validation/schema";
public static final String DYNAMIC_PROP = "http://apache.org/xml/features/validation/dynamic";
public static final String SCHEMA_CHECKING_PROP =
"http://apache.org/xml/features/validation/schema-full-checking";
public static final String SCHEMA_LOC =
"http://apache.org/xml/properties/schema/external-noNamespaceSchemaLocation";
public static final String VALID_XML_DOCUMENT = "The above XML is valid.";
public static final String INVALID_XML_DOCUMENT = "The Document has error at:";
}
I'm trying to get the output from a servlet on an Android phone.
This is my servlet:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package main;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* #author Bert Verhelst <verhelst_bert#hotmail.com>
*/
public class servlet1 extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet servlet1</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>processing...</h1>");
out.println("</body>");
out.println("</html>");
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
#Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
+ "Transitional//EN\">\n"
+ "<html>\n"
+ "<head><title>Hello WWW</title></head>\n"
+ "<body>\n"
+ "<h1>doget...</h1>\n"
+ "</body></html>");
}
/**
* Handles the HTTP <code>POST</code> method.
* #param request servlet request
* #param response servlet response
* #throws ServletException if a servlet-specific error occurs
* #throws IOException if an I/O error occurs
*/
#Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
+ "Transitional//EN\">\n"
+ "<html>\n"
+ "<head><title>Hello WWW</title></head>\n"
+ "<body>\n"
+ "<h1>dopost...</h1>\n"
+ "</body></html>");
}
/**
* Returns a short description of the servlet.
* #return a String containing servlet description
*/
#Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
This is my Android main page:
package be.smarttelecom.MyTest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView output = (TextView) findViewById(R.id.output);
try {
output.append("starting\n");
RestClient client = new RestClient("http://10.0.0.188:8084/Servlet_1/servlet1");
try {
client.Execute(RequestMethod.GET);
} catch (Exception e) {
e.printStackTrace();
}
output.append("after execute\n");
String response = client.getResponse();
output.append("class - " + response + "\n" );
output.append(response);
output.append("done\n");
}
catch (Exception ex) {
output.append("error: " + ex.getMessage() + "\n" + ex.toString() + "\n");
}
}
}
And finally we have the RestClient:
package be.smarttelecom.MyTest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
public class RestClient {
private ArrayList <NameValuePair> params;
private ArrayList <NameValuePair> headers;
private String url;
private int responseCode;
private String message;
private String response;
public String getResponse() {
return response;
}
public String getErrorMessage() {
return message;
}
public int getResponseCode() {
return responseCode;
}
public RestClient(String url)
{
this.url = url;
params = new ArrayList<NameValuePair>();
headers = new ArrayList<NameValuePair>();
}
public void AddParam(String name, String value)
{
params.add(new BasicNameValuePair(name, value));
}
public void AddHeader(String name, String value)
{
headers.add(new BasicNameValuePair(name, value));
}
public void Execute(RequestMethod method) throws Exception
{
switch(method) {
case GET:
{
//add parameters
String combinedParams = "";
if(!params.isEmpty()){
combinedParams += "?";
for(NameValuePair p : params)
{
String paramString = p.getName() + "=" + p.getValue();
if(combinedParams.length() > 1)
{
combinedParams += "&" + paramString;
}
else
{
combinedParams += paramString;
}
}
}
HttpGet request = new HttpGet(url + combinedParams);
//add headers
for(NameValuePair h : headers)
{
request.addHeader(h.getName(), h.getValue());
}
executeRequest(request, url);
break;
}
case POST:
{
HttpPost request = new HttpPost(url);
//add headers
for(NameValuePair h : headers)
{
request.addHeader(h.getName(), h.getValue());
}
if(!params.isEmpty()){
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
}
executeRequest(request, url);
break;
}
}
}
private void executeRequest(HttpUriRequest request, String url)
{
HttpClient client = new DefaultHttpClient();
HttpResponse httpResponse;
try {
httpResponse = client.execute(request);
responseCode = httpResponse.getStatusLine().getStatusCode();
message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
response = convertStreamToString(instream);
// Closing the input stream will trigger connection release
instream.close();
}
}
catch (ClientProtocolException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
} catch (IOException e) {
client.getConnectionManager().shutdown();
e.printStackTrace();
}
}
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
}
Unfortunately this doesn't work. Here is what I get for output (null),
What am I doing wrong?
I request the DoGet method of my servlet and convert the output to a string, but it appears to be empty.
I allowed the Internet connection in the manifest file just after the closing bracket of application,
<uses-permission android:name="android.permission.INTERNET" />
Romain Hippeau wrote in a comment:
Does the call ever get to the servlet? What does the server see is being sent? What is the server sending back?
That was the problem! I disabled my firewall and now it works :)