// callRestfulApi - Calls restful API and returns results as a string public String callRestfulApi( String addr, HttpServletRequest request, HttpServletResponse response) { if (localCookie) CookieHandler.setDefault(cm); try { ByteArrayOutputStream output = new ByteArrayOutputStream(); URL url = new URL(API_ROOT + addr); URLConnection urlConnection = url.openConnection(); String cookieVal = getBrowserInfiniteCookie(request); if (cookieVal != null) { urlConnection.addRequestProperty("Cookie", "infinitecookie=" + cookieVal); urlConnection.setDoInput(true); urlConnection.setDoOutput(true); urlConnection.setRequestProperty("Accept-Charset", "UTF-8"); } IOUtils.copy(urlConnection.getInputStream(), output); String newCookie = getConnectionInfiniteCookie(urlConnection); if (newCookie != null && response != null) { setBrowserInfiniteCookie(response, newCookie, request.getServerPort()); } return output.toString(); } catch (IOException e) { System.out.println(e.getMessage()); return null; } } // TESTED
/** * Computes the Base64 encoding of a string * * @param s a string * @return the Base 64 encoding of s */ public static String base64Encode(String s) { ByteArrayOutputStream bOut = new ByteArrayOutputStream(); Base64OutputStream out = new Base64OutputStream(bOut); try { out.write(s.getBytes()); out.flush(); } catch (IOException e) { } return bOut.toString(); }
public String getData() { ByteArrayOutputStream out = new ByteArrayOutputStream(); try { getDrawing().getOutputFormats().get(0).write(out, getDrawing()); return out.toString("UTF8"); } catch (IOException e) { SVGTextFigure tf = new SVGTextFigure(); tf.setText(e.getMessage()); tf.setBounds(new Point2D.Double(10, 10), new Point2D.Double(100, 100)); getDrawing().add(tf); e.printStackTrace(); return ""; } }
public void testStoreOverwrite() throws Exception { AuState auState = new AuState( mau, 123, // lastCrawlTime 321, // lastCrawlAttempt -1, // lastCrawlResult null, // lastCrawlResultMsg, 321, // lastTopLevelPoll 333, // lastPollStart -1, // lastPollresult null, // lastPollresultMsg 0, // pollDuration -1, // lastTreeWalk null, // crawlUrls null, // accessType 1, // clockssSubscriptionState 1.0, // v3Agreement 1.0, // highestV3Agreement SubstanceChecker.State.Unknown, null, // substanceVersion null, // metadataVersion -1, // lastMetadataIndex 0, // lastContentChange 444, // lastPoPPoll 8, // lastPoPPollResult -1, // lastLocalHashScan 27, // numAgreePeersLastPoR 72, // numWillingRepirers 19, // numCurrentSuspectVersions ListUtil.list("http://foo/"), // cdnStems repository); repository.storeAuState(auState); String filePath = LockssRepositoryImpl.mapAuToFileLocation(tempDirPath, mau); filePath += HistoryRepositoryImpl.AU_FILE_NAME; File xmlFile = new File(filePath); FileInputStream fis = new FileInputStream(xmlFile); ByteArrayOutputStream baos = new ByteArrayOutputStream(); StreamUtil.copy(fis, baos); fis.close(); String expectedStr = baos.toString(); auState = new AuState( mau, 1234, // lastCrawlTime 4321, // lastCrawlAttempt -1, // lastCrawlResult null, // lastCrawlResultMsg, 4321, // lastTopLevelPoll 5555, // lastPollStart -1, // lastPollresult null, // lastPollresultMsg 0, // pollDuration -1, // lastTreeWalk null, // crawlUrls null, // accessType 1, // clockssSubscriptionState 1.0, // v3Agreement 1.0, // highestV3Agreement SubstanceChecker.State.Unknown, null, // substanceVersion null, // metadataVersion -1, // lastMetadataIndex 0, // lastContentChange -1, // lastPoPPoll -1, // lastPoPPollResult -1, // lastLocalHashScan 13, // numAgreePeersLastPoR 31, // numWillingRepairers 91, // numCurrentSuspectVersions ListUtil.list("http://foo/"), // cdnStems repository); repository.storeAuState(auState); assertEquals(1234, auState.getLastCrawlTime()); assertEquals(4321, auState.getLastCrawlAttempt()); assertEquals(4321, auState.getLastTopLevelPollTime()); assertEquals(5555, auState.getLastPollStart()); assertEquals(13, auState.getNumAgreePeersLastPoR()); assertEquals(31, auState.getNumWillingRepairers()); assertEquals(91, auState.getNumCurrentSuspectVersions()); assertEquals(mau.getAuId(), auState.getArchivalUnit().getAuId()); assertEquals(ListUtil.list("http://foo/"), auState.getCdnStems()); fis = new FileInputStream(xmlFile); baos = new ByteArrayOutputStream(expectedStr.length()); StreamUtil.copy(fis, baos); fis.close(); log.info(baos.toString()); auState = null; auState = repository.loadAuState(); assertEquals(1234, auState.getLastCrawlTime()); assertEquals(4321, auState.getLastCrawlAttempt()); assertEquals(4321, auState.getLastTopLevelPollTime()); assertEquals(5555, auState.getLastPollStart()); assertEquals(13, auState.getNumAgreePeersLastPoR()); assertEquals(31, auState.getNumWillingRepairers()); assertEquals(91, auState.getNumCurrentSuspectVersions()); assertEquals(mau.getAuId(), auState.getArchivalUnit().getAuId()); auState = new AuState( mau, 123, // lastCrawlTime 321, // lastCrawlAttempt -1, // lastCrawlResult null, // lastCrawlResultMsg, 321, // lastTopLevelPoll 333, // lastPollStart -1, // lastPollresult null, // lastPollresultMsg 0, // pollDuration -1, // lastTreeWalk null, // crawlUrls null, // accessType 1, // clockssSubscriptionState 1.0, // v3Agreement 1.0, // highestV3Agreement SubstanceChecker.State.Unknown, null, // substanceVersion null, // metadataVersion -1, // lastMetadataIndex 0, // lastContentChange 444, // lastPoPPoll 8, // lastPoPPollResult -1, // lastLocalHashScan 27, // numAgreePeersLastPoR 72, // numWillingRepairers 19, // numCurrentSuspectVersions ListUtil.list("http://foo/"), // cdnStems repository); repository.storeAuState(auState); fis = new FileInputStream(xmlFile); baos = new ByteArrayOutputStream(expectedStr.length()); StreamUtil.copy(fis, baos); fis.close(); assertEquals(expectedStr, baos.toString()); }