Best SeLion code snippet using com.paypal.selion.grid.servlets.transfer.DefaultManagedArtifact
Source:Closure_12_e0.java
...30import com.paypal.selion.grid.servlets.transfer.UploadRequestProcessor.RequestHeaders;31import com.paypal.selion.logging.SeLionGridLogger;32import com.paypal.selion.utils.ConfigParser;33/**34 * <code>DefaultManagedArtifact</code> represents an artifact that is successfully saved to SeLion grid by an HTTP POST35 * method call. This artifact mostly represents binary file types rather than text files. The MIME type for this36 * artifact is set to 'application/zip'. Expiry of the artifact is based on TTL (Time To Live) specified in milli37 * seconds. The configuration is read from Grid configuration system.38 */39public class DefaultManagedArtifact implements ManagedArtifact {40 private static final Logger logger = SeLionGridLogger.getLogger();41 private static final String EXPIRY_CONFIG_PROPERTY = "artifactExpiryInMilliSec";42 private static final String HTTP_CONTENT_TYPE = "application/zip";43 private String filePath = null;44 private File artifactFile = null;45 private String artifactName = null;46 private String folderName = null;47 private String parentFolderName = null;48 private byte[] contents = null;49 private final long timeToLiveInMillis;50 public DefaultManagedArtifact(String pathName) {51 this.filePath = pathName;52 artifactFile = new File(this.filePath);53 timeToLiveInMillis = ConfigParser.getInstance().getLong(EXPIRY_CONFIG_PROPERTY);54 if (logger.isLoggable(Level.FINE)) {55 logger.log(Level.FINE, "Time To Live configured in Grid: " + timeToLiveInMillis + " milli seconds.");56 }57 }58 public String getArtifactName() {59 if (artifactName == null) {60 artifactName = artifactFile.getName();61 }62 return artifactName;63 }64 public String getFolderName() {65 if (folderName == null) {66 folderName = artifactFile.getParentFile().getName();67 }68 return folderName;69 }70 public String getParentFolderName() {71 if (parentFolderName == null) {72 parentFolderName = artifactFile.getParentFile().getParentFile().getName();73 }74 return parentFolderName;75 }76 @Override77 public byte[] getArtifactContents() {78 if (contents == null) {79 readContents();80 }81 return contents;82 }83 @Override84 public <T extends Criteria> boolean matches(T criteria) {85 SeLionGridLogger.entering(criteria);86 if (!criteria.getArtifactName().equals(getArtifactName())) {87 SeLionGridLogger.exiting(false);88 return false;89 }90 if (isApplicationFolderRequested(criteria) && applicationFolderAndUserIdMatches(criteria)) {91 SeLionGridLogger.exiting(true);92 return true;93 }94 boolean matches = !isApplicationFolderRequested(criteria) && userIdMatches(criteria);95 SeLionGridLogger.exiting(matches);96 return matches;97 }98 @Override99 public boolean isExpired() {100 boolean expired = (System.currentTimeMillis() - artifactFile.lastModified()) > timeToLiveInMillis;101 if (expired) {102 if (logger.isLoggable(Level.INFO)) {103 logger.log(104 Level.INFO,105 "Artifact: " + getArtifactName() + " expired, time(now): "106 + FileTime.fromMillis(System.currentTimeMillis()) + ", created: "107 + FileTime.fromMillis(artifactFile.lastModified()));108 }109 }110 return expired;111 }112 @Override113 public String getHttpContentType() {114 return HTTP_CONTENT_TYPE;115 }116 @Override117 public boolean equals(Object other) {118 if (this == other) {119 return true;120 }121 if (!(other instanceof DefaultManagedArtifact)) {122 return false;123 }124 DefaultManagedArtifact otherManagedArtifact = DefaultManagedArtifact.class.cast(other);125 if (!getArtifactName().equals(otherManagedArtifact.getArtifactName())) {126 return false;127 }128 if (!getFolderName().equals(otherManagedArtifact.getFolderName())) {129 return false;130 }131 if (!getParentFolderName().equals(otherManagedArtifact.getParentFolderName())) {132 return false;133 }134 return true;135 }136 @Override137 public int hashCode() {138 int result = 17;139 result = 31 * result + getArtifactName().hashCode();140 result = 31 * result + getFolderName().hashCode();141 result = 31 * result + getParentFolderName().hashCode();142 return result;143 }144 @Override145 public String toString() {146 return "[ Artifact Name: " + getArtifactName() + ", Folder: " + getFolderName() + ", ParentFolder: "147 + getParentFolderName() + "]";148 }149 private void readContents() {150 try {151 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(artifactFile));152 ByteArrayOutputStream bos = new ByteArrayOutputStream((int) artifactFile.length());153 IOUtils.copy(bis, bos);154 contents = bos.toByteArray();155 } catch (FileNotFoundException exe) {156 throw new ArtifactDownloadException("FileNotFoundException in reading bytes", exe);157 } catch (IOException exe) {158 throw new ArtifactDownloadException("IOException in reading bytes", exe);159 }160 }161 private <T extends Criteria> boolean isApplicationFolderRequested(T criteria) {162 return !StringUtils.isBlank(criteria.getApplicationFolder());163 }164 private <T extends Criteria> boolean applicationFolderAndUserIdMatches(T criteria) {165 return criteria.getApplicationFolder().equals(getFolderName())166 && criteria.getUserId().equals(getParentFolderName());167 }168 private <T extends Criteria> boolean userIdMatches(T criteria) {169 return criteria.getUserId().equals(getFolderName());170 }171 /**172 * {@link Criteria} to match a {@link DefaultManagedArtifact} uniquely. Criteria uses artifact name, user id and173 * application folder to uniquely identify a {@link DefaultManagedArtifact}. Parameters artifactName, userId and174 * applicationFolder match artifact name, folder name and parent folder name of some {@link DefaultManagedArtifact}175 * respectively.176 */177 public static class DefaultCriteria implements Criteria {178 protected String artifactName;179 protected String userId;180 protected String applicationFolder;181 public DefaultCriteria(EnumMap<RequestHeaders, String> parametersMap) {182 validateParametersMap(parametersMap);183 this.artifactName = parametersMap.get(RequestHeaders.FILENAME);184 this.userId = parametersMap.get(RequestHeaders.USERID);185 this.applicationFolder = parametersMap.get(RequestHeaders.APPLICATIONFOLDER);186 }187 private void validateParametersMap(EnumMap<RequestHeaders, String> parametersMap) {188 if (!parametersMap.containsKey(RequestHeaders.FILENAME)...
Source:TransferServlet.java
...22import javax.servlet.http.HttpServletRequest;23import javax.servlet.http.HttpServletResponse;24import com.paypal.selion.grid.servlets.transfer.ArtifactDownloadException;25import com.paypal.selion.grid.servlets.transfer.ArtifactUploadException;26import com.paypal.selion.grid.servlets.transfer.DefaultManagedArtifact;27import com.paypal.selion.grid.servlets.transfer.DownloadRequestProcessor;28import com.paypal.selion.grid.servlets.transfer.DownloadResponder;29import com.paypal.selion.grid.servlets.transfer.TransferContext;30import com.paypal.selion.grid.servlets.transfer.UploadRequestProcessor;31import com.paypal.selion.grid.servlets.transfer.UploadRequestProcessor.AbstractUploadRequestProcessor;32import com.paypal.selion.grid.servlets.transfer.UploadRequestProcessor.ApplicationUploadRequestProcessor;33import com.paypal.selion.grid.servlets.transfer.UploadRequestProcessor.MultipartUploadRequestProcessor;34import com.paypal.selion.grid.servlets.transfer.UploadResponder;35import com.paypal.selion.grid.servlets.transfer.UploadResponder.AbstractUploadResponder;36import com.paypal.selion.grid.servlets.transfer.UploadResponder.AcceptHeaderEnum;37import com.paypal.selion.grid.servlets.transfer.UploadResponder.JsonUploadResponder;38import com.paypal.selion.logging.SeLionGridLogger;39/**40 * <code>TransferServlet</code> is used for processing HTTP POST upload requests to SeLion grid. The artifacts are41 * uploaded using POST HTTP method call. The response of the POST HTTP method call contains the necessary HTTP GET url42 * used for downloading the artifact.43 */44public class TransferServlet extends HttpServlet {45 private static final long serialVersionUID = -4598713481663637719L;46 private static final SeLionGridLogger LOGGER = SeLionGridLogger.getLogger(TransferServlet.class);47 public void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)48 throws ServletException, IOException {49 LOGGER.entering((Object)new Object[] { httpServletRequest, httpServletResponse });50 try {51 TransferContext transferContext = new TransferContext(httpServletRequest, httpServletResponse);52 UploadRequestProcessor requestProcessor = getUploadRequestProcessor(transferContext);53 transferContext.setUploadRequestProcessor(requestProcessor);54 UploadResponder uploadResponder = getUploadResponder(transferContext);55 uploadResponder.respond();56 } catch (ArtifactUploadException exe) {57 /*58 * Catching RuntimeException because UploadResponder some times throws IOException wrapped in59 * ArtifactUploadException and this IOException should be thrown back as IOException defined by the Servlet60 * API.61 */62 handleExceptions(exe);63 }64 LOGGER.exiting();65 }66 public void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)67 throws ServletException, IOException {68 LOGGER.entering((Object)new Object[] { httpServletRequest, httpServletResponse });69 try {70 TransferContext transferContext = new TransferContext(httpServletRequest, httpServletResponse);71 DownloadRequestProcessor downloadRequestProcessor = new DownloadRequestProcessor();72 transferContext.setDownloadRequestProcessor(downloadRequestProcessor);73 DownloadResponder downloadResponder = new DownloadResponder(transferContext);74 downloadResponder.respond();75 } catch (ArtifactDownloadException exe) {76 /*77 * Catching RuntimeException because DownloadResponder some times throws IOException wrapped in78 * ArtifactDownloadException and this IOException should be thrown back as IOException defined by the79 * Servlet API.80 */81 handleExceptions(exe);82 }83 LOGGER.exiting();84 }85 private void handleExceptions(Exception exe) throws IOException, ServletException {86 if (exe.getCause() instanceof IOException) {87 throw (IOException) exe.getCause();88 } else {89 throw new ServletException(exe.getMessage());90 }91 }92 /**93 * Returns a {@link AbstractUploadRequestProcessor} for {@link DefaultManagedArtifact}94 * 95 * @param transferContext96 * Instance of {@link TransferContext}97 * @return Instance of {@link UploadRequestProcessor}.98 */99 private UploadRequestProcessor getUploadRequestProcessor(TransferContext transferContext) {100 LOGGER.entering(transferContext);101 String contentType = transferContext.getHttpServletRequest().getContentType() != null ? transferContext102 .getHttpServletRequest().getContentType().toLowerCase() : "unknown";103 if (contentType.contains(AbstractUploadRequestProcessor.MULTIPART_CONTENT_TYPE)) {104 // Return a Multipart request processor105 UploadRequestProcessor uploadRequestProcessor = new MultipartUploadRequestProcessor(106 transferContext);107 LOGGER.exiting(uploadRequestProcessor);...
DefaultManagedArtifact
Using AI Code Generation
1import org.openqa.selenium.WebDriver;2import org.openqa.selenium.remote.DesiredCapabilities;3import org.openqa.selenium.remote.RemoteWebDriver;4import org.testng.annotations.Test;5import com.paypal.selion.grid.servlets.transfer.DefaultManagedArtifact;6public class DefaultManagedArtifactTest {7 public void testDefaultManagedArtifact() throws Exception {8 DesiredCapabilities cap = new DesiredCapabilities();9 cap.setCapability(DefaultManagedArtifact.DEFAULT_MANAGED_ARTIFACT_CAPABILITY, artifact);10 WebDriver driver = new RemoteWebDriver(cap);11 driver.get(artifact.getArtifactLocation());12 driver.quit();13 }14}15import java.io.File;16import java.net.URL;17import java.util.concurrent.TimeUnit;18import org.openqa.selenium.By;19import org.openqa.selenium.WebDriver;20import org.openqa.selenium.remote.DesiredCapabilities;21import org.openqa.selenium.remote.RemoteWebDriver;22import org.testng.annotations.Test;23import com.paypal.selion.grid.servlets.transfer.DefaultManagedArtifact;24public class DefaultManagedArtifactTest {25 public void testDefaultManagedArtifact() throws Exception {26 DesiredCapabilities cap = new DesiredCapabilities();27 cap.setCapability(DefaultManagedArtifact.DEFAULT_MANAGED_ARTIFACT_CAPABILITY, artifact);28 driver.get(artifact.getArtifactLocation());29 driver.findElement(By.name("q")).sendKeys("Selenium");30 driver.findElement(By.name("q")).submit();31 driver.quit();32 }33}34import java.io.File;35import java.net.URL;36import java.util.concurrent.TimeUnit;37import org.openqa.selenium.By;38import org.openqa.selenium.WebDriver;39import org.openqa.selenium.remote.DesiredCapabilities;40import org.openqa.selenium.remote.RemoteWebDriver;41import org.testng.annotations.Test;42import com.paypal.selion.grid.servlets.transfer.DefaultManagedArtifact;43public class DefaultManagedArtifactTest {44 public void testDefaultManagedArtifact() throws Exception {
DefaultManagedArtifact
Using AI Code Generation
1package com.paypal.selion.grid.servlets.transfer;2import com.paypal.selion.grid.servlets.transfer.DefaultManagedArtifact;3public class TestDefaultManagedArtifact {4 public static void main(String[] args) {5 DefaultManagedArtifact artifact = new DefaultManagedArtifact("test", "test");6 System.out.println(artifact.getArtifactId());7 System.out.println(artifact.getArtifactName());8 }9}10package com.paypal.selion.grid.servlets.transfer;11import com.paypal.selion.grid.servlets.transfer.DefaultManagedArtifact;12public class TestDefaultManagedArtifact {13 public static void main(String[] args) {14 DefaultManagedArtifact artifact = new DefaultManagedArtifact("test", "test");15 System.out.println(artifact.getArtifactId());16 System.out.println(artifact.getArtifactName());17 }18}19package com.paypal.selion.grid.servlets.transfer;20import com.paypal.selion.grid.servlets.transfer.DefaultManagedArtifact;21public class TestDefaultManagedArtifact {22 public static void main(String[] args) {23 DefaultManagedArtifact artifact = new DefaultManagedArtifact("test", "test");24 System.out.println(artifact.getArtifactId());25 System.out.println(artifact.getArtifactName());26 }27}28package com.paypal.selion.grid.servlets.transfer;29import com.paypal.selion.grid.servlets.transfer.DefaultManagedArtifact;30public class TestDefaultManagedArtifact {31 public static void main(String[] args) {32 DefaultManagedArtifact artifact = new DefaultManagedArtifact("test", "test");33 System.out.println(artifact.getArtifactId());34 System.out.println(artifact.getArtifactName());35 }36}37package com.paypal.selion.grid.servlets.transfer;38import com.paypal.selion.grid.servlets.transfer.DefaultManagedArtifact;39public class TestDefaultManagedArtifact {40 public static void main(String[] args) {
DefaultManagedArtifact
Using AI Code Generation
1package com.paypal.selion.grid.servlets.transfer;2import java.io.File;3import java.io.IOException;4import java.io.InputStream;5import java.io.OutputStream;6import java.io.PrintWriter;7import java.io.StringWriter;8import java.util.List;9import javax.servlet.ServletException;10import javax.servlet.http.HttpServlet;11import javax.servlet.http.HttpServletRequest;12import javax.servlet.http.HttpServletResponse;13import org.apache.commons.io.FileUtils;14import org.apache.commons.io.IOUtils;15import org.apache.commons.lang.StringUtils;16import org.openqa.grid.internal.Registry;17import org.openqa.grid.internal.RemoteProxy;18import org.openqa.grid.internal.TestSlot;19import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;20import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;21import org.openqa.grid.web.Hub;22import org.openqa.grid.web.servlet.RegistryBasedServlet;23import com.google.gson.Gson;24import com.google.gson.JsonArray;25import com.google.gson.JsonObject;26import com.paypal.selion.grid.servlets.transfer.DefaultManagedArtifact;27import com.paypal.selion.grid.servlets.transfer.DefaultTransferManager;28import com.paypal.selion.grid.servlets.transfer.TransferManager;29import com.paypal.selion.logging.SeLionGridLogger;30import com.paypal.selion.pojos.SeLionGridConstants;31public class DefaultArtifactManagerServlet extends RegistryBasedServlet {32 private static final long serialVersionUID = 1L;33 private static final SeLionGridLogger LOGGER = SeLionGridLogger.getLogger(DefaultArtifactManagerServlet.class);34 public DefaultArtifactManagerServlet() {35 this(null);36 }37 public DefaultArtifactManagerServlet(Registry registry) {38 super(registry);39 }40 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {41 response.setContentType("text/html");42 response.setStatus(HttpServletResponse.SC_OK);43 response.getWriter().println("DefaultArtifactManagerServlet is up and running");44 }45 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,46 IOException {47 response.setContentType("application/json");48 response.setStatus(HttpServletResponse.SC_OK);49 JsonObject resp = new JsonObject();50 PrintWriter writer = response.getWriter();51 try {52 String action = request.getParameter(SeLionGridConstants.ACTION);53 if (action == null) {54 throw new IllegalArgumentException("Missing action parameter");55 }56 if (action.equals(SeLionGridConstants.ACTION_LIST)) {57 resp.add(SeLionGridConstants.ARTIFACTS, listArtifacts());58 } else if (action
DefaultManagedArtifact
Using AI Code Generation
1package com.paypal.selion.grid.servlets.transfer;2import java.io.File;3import java.io.IOException;4import org.apache.commons.io.FileUtils;5import org.openqa.grid.internal.Registry;6import org.openqa.grid.internal.RemoteProxy;7import org.openqa.grid.internal.TestSession;8import org.openqa.grid.selenium.proxy.DefaultRemoteProxy;9import org.openqa.grid.web.servlet.RegistryBasedServlet;10import org.openqa.selenium.remote.DesiredCapabilities;11import com.paypal.selion.grid.servlets.transfer.DefaultManagedArtifact.ArtifactType;12public class UploadServlet extends RegistryBasedServlet {13 public UploadServlet() {14 this(null);15 }16 public UploadServlet(Registry registry) {17 super(registry);18 }19 protected void doPost(javax.servlet.http.HttpServletRequest request,20 javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {21 String path = request.getParameter("path");22 String type = request.getParameter("type");23 String content = request.getParameter("content");24 if (path == null || type == null || content == null) {25 response.setStatus(400);26 response.getWriter().println("Invalid request");27 return;28 }29 if (path.startsWith("/") || path.startsWith("\\")) {30 response.setStatus(400);31 response.getWriter().println("Invalid path");32 return;33 }34 ArtifactType artifactType = ArtifactType.valueOf(type);35 File file = new File(path);36 switch (artifactType) {37 FileUtils.writeStringToFile(file, content);38 break;39 FileUtils.forceMkdir(file);40 break;41 response.setStatus(400);42 response.getWriter().println("Invalid type");43 return;44 }45 Registry registry = getRegistry();46 for (RemoteProxy proxy : registry.getAllProxies()) {47 if (proxy instanceof DefaultRemoteProxy) {48 TestSession session = ((DefaultRemoteProxy) proxy).getNewSession(new DesiredCapabilities());49 if (session != null) {50 session.getSlot().getProxy().addArtifact(new DefaultManagedArtifact(file, artifactType));51 }52 }53 }54 response.setStatus(200);55 response.getWriter().println("OK");56 }57}58package com.paypal.selion.grid.servlets.transfer;59import
DefaultManagedArtifact
Using AI Code Generation
1import com.paypal.selion.grid.servlets.transfer.DefaultManagedArtifact;2DefaultManagedArtifact artifact = new DefaultManagedArtifact(“com.paypal.selion”, “Selion-Grid”, “jar”, “1.0.0”);3String url = artifact.getUrl();4String fileName = artifact.getFileName();5String filePath = artifact.getFilePath();6import com.paypal.selion.grid.servlets.transfer.ManagedArtifactFactory;7ManagedArtifact artifact = ManagedArtifactFactory.getManagedArtifact(“selion-grid.jar”);8String url = artifact.getUrl();9String fileName = artifact.getFileName();10String filePath = artifact.getFilePath();11import com.paypal.selion.grid.servlets.transfer.ManagedArtifactFactory;12ManagedArtifact artifact = ManagedArtifactFactory.getManagedArtifact(“selion-grid.jar”);13String url = artifact.getUrl();14String fileName = artifact.getFileName();15String filePath = artifact.getFilePath();16import com.paypal.selion.grid.servlets.transfer.ManagedArtifactFactory;17ManagedArtifact artifact = ManagedArtifactFactory.getManagedArtifact(“selion-grid.jar”);18String url = artifact.getUrl();19String fileName = artifact.getFileName();20String filePath = artifact.getFilePath();21import com.paypal.selion.grid.servlets.transfer.ManagedArtifactFactory;
DefaultManagedArtifact
Using AI Code Generation
1package com.paypal.selion.grid.servlets.transfer;2import java.io.File;3import java.io.IOException;4import java.net.URL;5import java.util.ArrayList;6import java.util.List;7import org.apache.commons.io.FileUtils;8import org.openqa.grid.internal.Registry;9import org.openqa.grid.internal.RemoteProxy;10import org.openqa.grid.internal.TestSlot;11import org.openqa.grid.internal.listeners.Prioritizer;12import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;13import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;14import org.openqa.grid.selenium.proxy.DefaultRemoteProxy;15import org.openqa.grid.web.Hub;16import org.openqa.grid.web.servlet.handler.RequestHandler;17import org.openqa.selenium.remote.CapabilityType;18public class DefaultManagedArtifactPrioritizer implements Prioritizer {19 private GridNodeConfiguration nodeConfig;20 private GridHubConfiguration hubConfig;21 public DefaultManagedArtifactPrioritizer(GridNodeConfiguration nodeConfig, GridHubConfiguration hubConfig) {22 this.nodeConfig = nodeConfig;23 this.hubConfig = hubConfig;24 }25 public void beforeSession(RequestHandler request, Registry registry) {26 RemoteProxy proxy = request.getTestSlot().getProxy();27 if (proxy instanceof DefaultRemoteProxy) {28 return;29 }30 List<DefaultManagedArtifact> artifacts = new ArrayList<DefaultManagedArtifact>();31 if (nodeConfig.custom.containsKey("artifact")) {32 artifacts.add(new DefaultManagedArtifact(nodeConfig.custom.get("artifact")));33 }34 if (nodeConfig.custom.containsKey("artifacts")) {35 for (String artifact : nodeConfig.custom.get("artifacts").split(",")) {36 artifacts.add(new DefaultManagedArtifact(artifact));37 }38 }39 if (artifacts.isEmpty()) {40 return;41 }42 File tmpDir = new File(System.getProperty("java.io.tmpdir"), "seLionGridArtifact");43 if (tmpDir.exists()) {44 try {45 FileUtils.deleteDirectory(tmpDir);46 } catch (IOException e) {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!