Best SeLion code snippet using com.paypal.selion.grid.servlets.TransferServlet.getUploadResponder
Source:TransferServlet.java
...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);108 return uploadRequestProcessor;109 }110 if (contentType.contains(AbstractUploadRequestProcessor.APPLICATION_URLENCODED_CONTENT_TYPE)) {111 // Return normal Urlencoded request processor112 UploadRequestProcessor uploadRequestProcessor = new ApplicationUploadRequestProcessor(113 transferContext);114 LOGGER.exiting(uploadRequestProcessor);115 return uploadRequestProcessor;116 }117 throw new ArtifactUploadException("Content-Type should be either: "118 + AbstractUploadRequestProcessor.MULTIPART_CONTENT_TYPE + " or: "119 + AbstractUploadRequestProcessor.APPLICATION_URLENCODED_CONTENT_TYPE + " for file uploads");120 }121 /**122 * Returns a {@link AbstractUploadResponder} depending on the Accept header received in {@link HttpServletRequest}.123 * If there is no matching {@link AbstractUploadResponder} then return {@link JsonUploadResponder} as the default124 * implementation.125 * 126 * @param transferContext127 * Instance of {@link TransferContext}128 * @return Instance of {@link AbstractUploadResponder}.129 */130 private UploadResponder getUploadResponder(TransferContext transferContext) {131 LOGGER.entering(transferContext);132 UploadResponder uploadResponder;133 Class<? extends UploadResponder> uploadResponderClass = getResponderClass(transferContext134 .getHttpServletRequest().getHeader("accept"));135 try {136 uploadResponder = (UploadResponder) uploadResponderClass.getConstructor(new Class[] { TransferContext.class }).newInstance(137 new Object[] { transferContext });138 LOGGER.exiting(uploadResponder);139 return uploadResponder;140 } catch (Exception e) {141 // We cannot do any meaningful operation to handle this; catching exception and returning142 // default responder143 uploadResponder = new JsonUploadResponder(transferContext);144 LOGGER.exiting(uploadResponder);145 return uploadResponder;146 }147 }148 private Class<? extends UploadResponder> getResponderClass(String headerString) {149 String[] headers = headerString.split(";");150 List<String> headerPrecedenceList = Arrays.asList(headers);151 Collections.reverse(headerPrecedenceList);152 for (String header : headerPrecedenceList) {153 try {154 AcceptHeaderEnum acceptHeaderEnum = AcceptHeaderEnum.getAcceptHeaderEnum(header);155 return acceptHeaderEnum.getUploadResponder();156 } catch (ArtifactUploadException exe) {157 // Exception is thrown if there is no implementation; just ignore and iterate until success158 }159 }160 return JsonUploadResponder.class;161 }162}...
getUploadResponder
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.HashMap;6import java.util.Map;7import org.apache.commons.io.FileUtils;8import org.apache.commons.io.FilenameUtils;9import org.apache.http.client.ClientProtocolException;10import org.openqa.grid.common.RegistrationRequest;11import org.openqa.grid.common.SeleniumProtocol;12import org.openqa.grid.common.exception.GridException;13import org.openqa.grid.internal.utils.configuration.GridHubConfiguration;14import org.openqa.grid.internal.utils.configuration.GridNodeConfiguration;15import org.openqa.grid.selenium.GridLauncherV3;16import org.openqa.grid.web.Hub;17import org.openqa.selenium.remote.DesiredCapabilities;18import com.paypal.selion.grid.servlets.transfer.UploadRequest;19import com.paypal.selion.grid.servlets.transfer.UploadResponse;20import com.paypal.selion.utils.ConfigParser;21public class TransferServletTest {22 public static void main(String[] args) throws ClientProtocolException, IOException {23 GridHubConfiguration hubConfig = new GridHubConfiguration();24 hubConfig.setPort(4444);25 Hub hub = GridLauncherV3.launchHub(hubConfig);26 GridNodeConfiguration nodeConfig = new GridNodeConfiguration();27 nodeConfig.setHub(hubConfig);28 nodeConfig.setPort(5555);29 nodeConfig.setCapabilities(new DesiredCapabilities("firefox", "1", SeleniumProtocol.WebDriver));30 RegistrationRequest nodeRequest = new RegistrationRequest(nodeConfig);31 nodeRequest.setConfiguration(nodeConfig);32 nodeRequest.getConfiguration().capabilities.add(nodeConfig.capabilities);33 GridLauncherV3.launchNode(nodeRequest);34 GridNodeConfiguration nodeConfig1 = new GridNodeConfiguration();35 nodeConfig1.setHub(hubConfig);36 nodeConfig1.setPort(5556);37 nodeConfig1.setCapabilities(new DesiredCapabilities("firefox", "2", SeleniumProtocol.WebDriver));38 RegistrationRequest nodeRequest1 = new RegistrationRequest(nodeConfig1);39 nodeRequest1.setConfiguration(nodeConfig1);40 nodeRequest1.getConfiguration().capabilities.add(nodeConfig1.capabilities);41 GridLauncherV3.launchNode(nodeRequest1);42 GridNodeConfiguration nodeConfig2 = new GridNodeConfiguration();
getUploadResponder
Using AI Code Generation
1DesiredCapabilities capabilities = new DesiredCapabilities();2capabilities.setBrowserName("firefox");3capabilities.setVersion("3.6");4capabilities.setCapability("platform", "WINDOWS");5capabilities.setCapability("name", "Upload file test");6String fileURL = driver.getUploadResponder().uploadFile(new File("file.txt"));7driver.get(fileURL);8driver.close();9driver.quit();
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!!