List of usage examples for java.net URL toExternalForm
public String toExternalForm()
From source file:org.jboss.as.test.integration.web.security.servlet3.ServletSecurityRoleNamesCommon.java
/** * Method that needs to be overridden with the HTTPClient code. * * @param user username// ww w .j a v a 2 s.c om * @param pass password * @param expectedCode http status code * @throws Exception */ protected void makeCall(String user, String pass, int expectedCode, URL url) throws Exception { CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(user, pass)); try (CloseableHttpClient httpclient = HttpClients.custom() .setDefaultCredentialsProvider(credentialsProvider).build()) { HttpGet httpget = new HttpGet(url.toExternalForm()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); StatusLine statusLine = response.getStatusLine(); assertEquals(expectedCode, statusLine.getStatusCode()); EntityUtils.consume(entity); } }
From source file:org.picketbox.http.test.authentication.jetty.DelegatingSecurityFilterHTTPBasicUnitTestCase.java
@Test public void testBasicAuth() throws Exception { URL url = new URL(this.urlStr); DefaultHttpClient httpclient = null; try {/* ww w . java2 s . c om*/ String user = "Aladdin"; String pass = "Open Sesame"; httpclient = new DefaultHttpClient(); httpclient.getCredentialsProvider().setCredentials(new AuthScope(url.getHost(), url.getPort()), new UsernamePasswordCredentials(user, pass)); HttpGet httpget = new HttpGet(url.toExternalForm()); System.out.println("executing request" + httpget.getRequestLine()); HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); System.out.println("----------------------------------------"); StatusLine statusLine = response.getStatusLine(); System.out.println(statusLine); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } assertEquals(200, statusLine.getStatusCode()); EntityUtils.consume(entity); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:org.picketbox.http.test.config.ProtectedResourceManagerUnitTestCase.java
@Test public void testNotAuthorizedResource() throws Exception { URL url = new URL(this.urlStr + "confidentialResource"); DefaultHttpClient httpclient = null; try {//from www .jav a 2 s . c o m String user = "Aladdin"; String pass = "Open Sesame"; httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url.toExternalForm()); HttpResponse response = httpclient.execute(httpget); assertEquals(401, response.getStatusLine().getStatusCode()); Header[] headers = response.getHeaders(PicketBoxConstants.HTTP_WWW_AUTHENTICATE); HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); Header header = headers[0]; String value = header.getValue(); value = value.substring(7).trim(); String[] tokens = HTTPDigestUtil.quoteTokenize(value); Digest digestHolder = HTTPDigestUtil.digest(tokens); DigestScheme digestAuth = new DigestScheme(); digestAuth.overrideParamter("algorithm", "MD5"); digestAuth.overrideParamter("realm", digestHolder.getRealm()); digestAuth.overrideParamter("nonce", digestHolder.getNonce()); digestAuth.overrideParamter("qop", "auth"); digestAuth.overrideParamter("nc", "0001"); digestAuth.overrideParamter("cnonce", DigestScheme.createCnonce()); digestAuth.overrideParamter("opaque", digestHolder.getOpaque()); httpget = new HttpGet(url.toExternalForm()); Header auth = digestAuth.authenticate(new UsernamePasswordCredentials(user, pass), httpget); System.out.println(auth.getName()); System.out.println(auth.getValue()); httpget.setHeader(auth); System.out.println("executing request" + httpget.getRequestLine()); response = httpclient.execute(httpget); entity = response.getEntity(); System.out.println("----------------------------------------"); StatusLine statusLine = response.getStatusLine(); System.out.println(statusLine); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } assertEquals(403, statusLine.getStatusCode()); EntityUtils.consume(entity); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:com.esri.geoportal.harvester.agp.AgpOutputBroker.java
@Override public PublishingStatus publish(DataReference ref) throws DataOutputException { try {//from w w w. ja va 2 s . co m // extract map of attributes (normalized names) MapAttribute attributes = extractMapAttributes(ref); if (attributes == null) { throw new DataOutputException(this, String.format("Error extracting attributes from data.")); } // build typeKeywords array String src_source_type_s = URLEncoder.encode(ref.getBrokerUri().getScheme(), "UTF-8"); String src_source_uri_s = URLEncoder.encode(ref.getBrokerUri().toASCIIString(), "UTF-8"); String src_source_name_s = URLEncoder.encode(ref.getBrokerName(), "UTF-8"); String src_uri_s = URLEncoder.encode(ref.getSourceUri().toASCIIString(), "UTF-8"); String src_lastupdate_dt = ref.getLastModifiedDate() != null ? URLEncoder.encode(fromatDate(ref.getLastModifiedDate()), "UTF-8") : null; String[] typeKeywords = { String.format("src_source_type_s=%s", src_source_type_s), String.format("src_source_uri_s=%s", src_source_uri_s), String.format("src_source_name_s=%s", src_source_name_s), String.format("src_uri_s=%s", src_uri_s), String.format("src_lastupdate_dt=%s", src_lastupdate_dt) }; try { // generate token if (token == null) { token = generateToken(); } // find resource URL URL resourceUrl = new URL(getAttributeValue(attributes, "resource.url", null)); ItemType itemType = ItemType.matchPattern(resourceUrl.toExternalForm()).stream().findFirst() .orElse(null); if (itemType == null || itemType.getDataType() != DataType.URL) { return PublishingStatus.SKIPPED; } // find thumbnail URL String sThumbnailUrl = StringUtils.trimToNull(getAttributeValue(attributes, "thumbnail.url", null)); URL thumbnailUrl = sThumbnailUrl != null ? new URL(sThumbnailUrl) : null; // check if item exists QueryResponse search = client.search( String.format("typekeywords:%s", String.format("src_uri_s=%s", src_uri_s)), 0, 0, token); ItemEntry itemEntry = search != null && search.results != null && search.results.length > 0 ? search.results[0] : null; if (itemEntry == null) { // add item if doesn't exist ItemResponse response = addItem(getAttributeValue(attributes, "title", null), getAttributeValue(attributes, "description", null), resourceUrl, thumbnailUrl, itemType, extractEnvelope(getAttributeValue(attributes, "bbox", null)), typeKeywords); if (response == null || !response.success) { throw new DataOutputException(this, String.format("Error adding item: %s", ref.getSourceUri())); } client.share(definition.getCredentials().getUserName(), definition.getFolderId(), response.id, true, true, null, token); return PublishingStatus.CREATED; } else if (itemEntry.owner.equals(definition.getCredentials().getUserName())) { itemEntry = client.readItem(itemEntry.id, token); if (itemEntry == null) { throw new DataOutputException(this, String.format("Unable to read item entry.")); } // update item if does exist ItemResponse response = updateItem(itemEntry.id, itemEntry.owner, itemEntry.ownerFolder, getAttributeValue(attributes, "title", null), getAttributeValue(attributes, "description", null), resourceUrl, thumbnailUrl, itemType, extractEnvelope(getAttributeValue(attributes, "bbox", null)), typeKeywords); if (response == null || !response.success) { throw new DataOutputException(this, String.format("Error updating item: %s", ref.getSourceUri())); } existing.remove(itemEntry.id); return PublishingStatus.UPDATED; } else { return PublishingStatus.SKIPPED; } } catch (MalformedURLException ex) { return PublishingStatus.SKIPPED; } } catch (MetaException | IOException | ParserConfigurationException | SAXException | URISyntaxException ex) { throw new DataOutputException(this, String.format("Error publishing data"), ex); } }
From source file:com.jaeksoft.searchlib.crawler.web.spider.HtmlArchiver.java
final private String getLocalPath(URL parentUrl, String fileName) { if (parentUrl == null || urlFileMap.get(parentUrl.toExternalForm()) != null) return fileName; StringBuilder sb = new StringBuilder("./"); sb.append(filesDir.getName());/*from w w w .j a va 2 s . com*/ sb.append('/'); sb.append(fileName); return sb.toString(); }
From source file:com.adito.replacementproxy.RequestProcessor.java
public void processRequest() throws Exception { // Create our own map of headers so they can be edited headerMap = new HeaderMap(); for (Enumeration e = request.getFieldNames(); e.hasMoreElements();) { String n = (String) e.nextElement(); for (Enumeration e2 = request.getFieldValues(n); e2.hasMoreElements();) { String v = (String) e2.nextElement(); headerMap.putHeader(n, v);//from www. ja v a 2s .co m } } // Build up the parameter map requestParameters = new RequestParameterMap(request); proxyURIDetails = requestParameters.getProxiedURIDetails(); VariableReplacement r = new VariableReplacement(); r.setRequest(request); r.setSession(launchSession.getSession()); r.setPolicy(launchSession.getPolicy()); String actualURL = r.replace(webForward.getDestinationURL()); if (proxyURIDetails.getProxiedURL() == null) { throw new Exception("No sslex_url parameter provided."); } if (log.isDebugEnabled()) log.debug("Proxying [" + request.getMethod() + "] " + proxyURIDetails.getProxiedURL()); URL proxiedURLBase = proxyURIDetails.getProxiedURLBase(); if (log.isDebugEnabled()) log.debug("Proxied URL base " + proxiedURLBase.toExternalForm()); // The web forward may restrict access to the target URL PropertyList restrictTo = webForward.getRestrictToHosts(); if (!restrictTo.isEmpty()) { boolean found = proxiedURLBase.getHost().equals(new URL(actualURL).getHost()); for (Iterator i = restrictTo.iterator(); !found && i.hasNext();) { String host = (String) i.next(); if (proxiedURLBase.getHost().matches(Util.parseSimplePatternToRegExp(host))) { found = true; } } if (!found) { throw new Exception("This resource (" + proxiedURLBase.toExternalForm() + ") is restricted to a list of target hosts. This host is not in the list."); } } // Determine if the page can be retrieved from cache getFromCache = false; Date expiryDate = null; if (cache != null && HttpConstants.METHOD_GET.equals(request.getMethod()) && cache.contains(proxyURIDetails.getProxiedURL())) { getFromCache = true; // HTTP 1.0 String cacheControl = request.getField(HttpConstants.HDR_PRAGMA); if (cacheControl != null && cacheControl.equalsIgnoreCase("no-cache")) { getFromCache = false; } else { String ifModifiedSince = request.getField(HttpConstants.HDR_IF_MODIFIED_SINCE); if (ifModifiedSince != null) { try { // Dont get from cache if getFromCache = false; } catch (Exception e) { } } } // HTTP 1.1 if (getFromCache) { cacheControl = request.getField(HttpConstants.HDR_CACHE_CONTROL); if (cacheControl != null) { StringTokenizer tok = new StringTokenizer(cacheControl, ";"); while (tok.hasMoreTokens()) { String t = tok.nextToken().trim(); String tl = t.toLowerCase(); if (t.startsWith("no-cache") || t.startsWith("no-store")) { getFromCache = false; } else if (tl.startsWith("max-age")) { expiryDate = new Date(); try { expiryDate.setTime( expiryDate.getTime() - (Integer.parseInt(Util.valueOfNameValuePair(tl)))); } catch (Exception e) { } } } } } // Check expiry if (getFromCache) { CacheingOutputStream cos = (CacheingOutputStream) cache.retrieve(proxyURIDetails.getProxiedURL()); if (expiryDate == null || (expiryDate != null && cos.getCachedDate().after(expiryDate))) { // Still ok } else { if (log.isDebugEnabled()) log.debug("Page expired"); getFromCache = false; } } else { if (log.isDebugEnabled()) log.debug("Not using cached page."); } } }
From source file:com.dtolabs.rundeck.core.execution.commands.ScriptURLCommandInterpreter.java
public InterpreterResult interpretCommand(ExecutionContext context, ExecutionItem item, INodeEntry node) throws InterpreterException { if (!cacheDir.isDirectory() && !cacheDir.mkdirs()) { throw new RuntimeException("Unable to create cachedir: " + cacheDir.getAbsolutePath()); }/* w ww . java 2s.c o m*/ final ScriptURLCommandExecutionItem script = (ScriptURLCommandExecutionItem) item; final ExecutionService executionService = framework.getExecutionService(); //create node context for node and substitute data references in command final Map<String, Map<String, String>> nodeDataContext = DataContextUtils.addContext("node", DataContextUtils.nodeData(node), context.getDataContext()); final String finalUrl = expandUrlString(script.getURLString(), nodeDataContext); final URL url; try { url = new URL(finalUrl); } catch (MalformedURLException e) { throw new InterpreterException(e); } if (null != context.getExecutionListener()) { context.getExecutionListener().log(4, "Requesting URL: " + url.toExternalForm()); } String cleanUrl = url.toExternalForm().replaceAll("^(https?://)([^:@/]+):[^@/]*@", "$1$2:****@"); String tempFileName = hashURL(url.toExternalForm()) + ".temp"; File destinationTempFile = new File(cacheDir, tempFileName); File destinationCacheData = new File(cacheDir, tempFileName + ".cache.properties"); //update from URL if necessary final URLFileUpdaterBuilder urlFileUpdaterBuilder = new URLFileUpdaterBuilder().setUrl(url) .setAcceptHeader("*/*").setTimeout(DEFAULT_TIMEOUT); if (USE_CACHE) { urlFileUpdaterBuilder.setCacheMetadataFile(destinationCacheData).setCachedContent(destinationTempFile) .setUseCaching(true); } final URLFileUpdater updater = urlFileUpdaterBuilder.createURLFileUpdater(); try { if (null != interaction) { //allow mock updater.setInteraction(interaction); } UpdateUtils.update(updater, destinationTempFile); logger.debug("Updated nodes resources file: " + destinationTempFile); } catch (UpdateUtils.UpdateException e) { if (!destinationTempFile.isFile() || destinationTempFile.length() < 1) { throw new InterpreterException("Error requesting URL Script: " + cleanUrl + ": " + e.getMessage(), e); } else { logger.error("Error requesting URL script: " + cleanUrl + ": " + e.getMessage(), e); } } final String filepath; //result file path try { filepath = executionService.fileCopyFile(context, destinationTempFile, node); } catch (FileCopierException e) { throw new InterpreterException(e); } try { /** * TODO: Avoid this horrific hack. Discover how to get SCP task to preserve the execute bit. */ if (!"windows".equalsIgnoreCase(node.getOsFamily())) { //perform chmod+x for the file final NodeExecutorResult nodeExecutorResult = framework.getExecutionService() .executeCommand(context, new String[] { "chmod", "+x", filepath }, node); if (!nodeExecutorResult.isSuccess()) { return nodeExecutorResult; } } final String[] args = script.getArgs(); //replace data references String[] newargs = null; if (null != args && args.length > 0) { newargs = new String[args.length + 1]; final String[] replargs = DataContextUtils.replaceDataReferences(args, nodeDataContext); newargs[0] = filepath; System.arraycopy(replargs, 0, newargs, 1, replargs.length); } else { newargs = new String[] { filepath }; } return framework.getExecutionService().executeCommand(context, newargs, node); } catch (ExecutionException e) { throw new InterpreterException(e); } }
From source file:org.jboss.as.test.manualmode.web.valve.authenticator.DescriptorValveAuthenticatorTestCase.java
@Test @InSequence(2)//from w ww .j av a 2s . co m public void testValveGlobal(@ArquillianResource URL url, @ArquillianResource ManagementClient client) throws Exception { // adding authenticator valve based on the created module Map<String, String> params = new HashMap<String, String>(); params.put(PARAM_NAME, GLOBAL_PARAM_VALUE); ValveUtil.addValve(client, CUSTOM_AUTHENTICATOR_1, MODULENAME, AUTHENTICATOR.getName(), params); ValveUtil.reload(client); String appUrl = url.toExternalForm() + WEB_APP_URL; log.debug("Testing url " + appUrl + " against two authenticators - one defined in web descriptor other is defined globally in server configuration and checking which one was used"); Header[] valveHeaders = ValveUtil.hitValve(new URL(appUrl)); assertEquals("There were two valves defined (but detected these valve headers: " + Arrays.toString(valveHeaders) + ")", 1, valveHeaders.length); assertEquals(WEB_PARAM_VALUE, valveHeaders[0].getValue()); // testing that it is used authenticator defined in web descriptors valve }
From source file:org.picketbox.test.config.ProtectedResourceManagerUnitTestCase.java
@Test public void testDigestAuth() throws Exception { URL url = new URL(urlStr + "onlyManagers"); DefaultHttpClient httpclient = null; try {//from w ww . j av a 2s. c om String user = "Aladdin"; String pass = "Open Sesame"; httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url.toExternalForm()); HttpResponse response = httpclient.execute(httpget); assertEquals(401, response.getStatusLine().getStatusCode()); Header[] headers = response.getHeaders(PicketBoxConstants.HTTP_WWW_AUTHENTICATE); HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); Header header = headers[0]; String value = header.getValue(); value = value.substring(7).trim(); String[] tokens = HTTPDigestUtil.quoteTokenize(value); DigestHolder digestHolder = HTTPDigestUtil.digest(tokens); DigestScheme digestAuth = new DigestScheme(); digestAuth.overrideParamter("algorithm", "MD5"); digestAuth.overrideParamter("realm", digestHolder.getRealm()); digestAuth.overrideParamter("nonce", digestHolder.getNonce()); digestAuth.overrideParamter("qop", "auth"); digestAuth.overrideParamter("nc", "0001"); digestAuth.overrideParamter("cnonce", DigestScheme.createCnonce()); digestAuth.overrideParamter("opaque", digestHolder.getOpaque()); httpget = new HttpGet(url.toExternalForm()); Header auth = digestAuth.authenticate(new UsernamePasswordCredentials(user, pass), httpget); System.out.println(auth.getName()); System.out.println(auth.getValue()); httpget.setHeader(auth); System.out.println("executing request" + httpget.getRequestLine()); response = httpclient.execute(httpget); entity = response.getEntity(); System.out.println("----------------------------------------"); StatusLine statusLine = response.getStatusLine(); System.out.println(statusLine); if (entity != null) { System.out.println("Response content length: " + entity.getContentLength()); } assertEquals(404, statusLine.getStatusCode()); EntityUtils.consume(entity); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }