/** * Get timestamp token - communications layer * @return - byte[] - TSA response, raw bytes (RFC 3161 encoded) */ protected byte[] getTSAResponse(byte[] requestBytes) throws Exception { // Setup the TSA connection URL url = new URL(tsaURL); URLConnection tsaConnection; tsaConnection = (URLConnection) url.openConnection(); tsaConnection.setDoInput(true); tsaConnection.setDoOutput(true); tsaConnection.setUseCaches(false); tsaConnection.setRequestProperty("Content-Type", "application/timestamp-query"); //tsaConnection.setRequestProperty("Content-Transfer-Encoding", "base64"); tsaConnection.setRequestProperty("Content-Transfer-Encoding", "binary"); if ((tsaUsername != null) && !tsaUsername.equals("") ) { String userPassword = tsaUsername + ":" + tsaPassword; tsaConnection.setRequestProperty("Authorization", "Basic " + Base64.encodeBytes(userPassword.getBytes())); } OutputStream out = tsaConnection.getOutputStream(); out.write(requestBytes); out.close(); // Get TSA response as a byte array InputStream inp = tsaConnection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = inp.read(buffer, 0, buffer.length)) >= 0) { baos.write(buffer, 0, bytesRead); } byte[] respBytes = baos.toByteArray(); String encoding = tsaConnection.getContentEncoding(); if (encoding != null && encoding.equalsIgnoreCase("base64")) { respBytes = Base64.decode(new String(respBytes)); } return respBytes; }
public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException { final String key = "x3JJHMbDL1EzLkh9GBhXDw=="; final String combined = key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; final byte[] sha1 = MessageDigest.getInstance("SHA-1").digest(combined.getBytes("UTF8")); System.out.println("Number of bytes: " + sha1.length); final String encoded = Base64.encodeBytes(sha1); System.out.println("encoded string: " + encoded); }
/** * Get timestamp token - communications layer * @return - byte[] - TSA response, raw bytes (RFC 3161 encoded) */ protected byte[] getTSAResponse(byte[] requestBytes) throws Exception { // Setup the TSA connection URL url = new URL(tsaURL); URLConnection tsaConnection; tsaConnection = (URLConnection) url.openConnection(); tsaConnection.setDoInput(true); tsaConnection.setDoOutput(true); tsaConnection.setUseCaches(false); tsaConnection.setRequestProperty("Content-Type", "application/timestamp-query"); //tsaConnection.setRequestProperty("Content-Transfer-Encoding", "base64"); tsaConnection.setRequestProperty("Content-Transfer-Encoding", "binary"); if ((tsaUsername != null) && !tsaUsername.equals("") ) { String userPassword = tsaUsername + ":" + tsaPassword; tsaConnection.setRequestProperty("Authorization", "Basic " + new String(Base64.encodeBytes(userPassword.getBytes()))); } OutputStream out = tsaConnection.getOutputStream(); out.write(requestBytes); out.close(); // Get TSA response as a byte array InputStream inp = tsaConnection.getInputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = inp.read(buffer, 0, buffer.length)) >= 0) { baos.write(buffer, 0, bytesRead); } byte[] respBytes = baos.toByteArray(); String encoding = tsaConnection.getContentEncoding(); if (encoding != null && encoding.equalsIgnoreCase("base64")) { respBytes = Base64.decode(new String(respBytes)); } return respBytes; }
private String saveAndInsertIntoQueue( FaxConfig faxConfig, FaxJob receivedFax, FaxJob faxFile ) { String filename = receivedFax.getFile_name(); filename = filename.replace("|", "-"); if( filename.isEmpty() ) { filename = System.currentTimeMillis() + ".pdf"; } filename = filename.replace(".tif", ".pdf"); if( ! filename.endsWith(".pdf") || ! filename.endsWith(".PDF") ) { filename = filename + ".pdf"; } filename = filename.trim(); log.info("Saving the Fax file " + filename + " meta data to Oscar's eDoc system."); EDoc newDoc = new EDoc("Recieved Fax", "Recieved Fax", filename, "", DEFAULT_USER, DEFAULT_USER, "", 'A', DateFormatUtils.format(receivedFax.getStamp(), "yyyy-MM-dd"), "", "", "demographic", DEFAULT_USER, receivedFax.getNumPages() ); newDoc.setDocPublic("0"); filename = newDoc.getFileName(); log.info("Saving the Fax file " + filename + " to the local file system at " + DOCUMENT_DIR ); if( Base64.decodeToFile( faxFile.getDocument(), DOCUMENT_DIR + "/" + filename) ) { newDoc.setContentType("application/pdf"); newDoc.setNumberOfPages(receivedFax.getNumPages()); String doc_no = EDocUtil.addDocumentSQL(newDoc); Integer queueId = faxConfig.getQueue(); Integer docNum = Integer.parseInt(doc_no); queueDocumentLinkDao.addActiveQueueDocumentLink(queueId, docNum); log.debug( "Saved file " + filename + " to filesystem at " + DOCUMENT_DIR ); return filename; } log.debug( "Failed to save file " + filename + " to filesystem at " + DOCUMENT_DIR ); return null; }