Best SeLion code snippet using com.paypal.selion.grid.servlets.transfer.UploadResponder.getAcceptHeader
Source:UploadResponder.java
...43 AcceptHeaderEnum(String acceptHeader, Class<? extends UploadResponder> uploadResponder) {44 this.acceptHeader = acceptHeader;45 this.uploadResponder = uploadResponder;46 }47 public String getAcceptHeader() {48 return acceptHeader;49 }50 public Class<? extends UploadResponder> getUploadResponder() {51 return uploadResponder;52 }53 /**54 * Returns {@link AcceptHeaderEnum} for the given HTTP Accept header, or throws {@link ArtifactUploadException}55 * if there is no implementation for the Accept header or if the Accept header is null56 * 57 * @param acceptHeader58 * Accept header of the HTTP method59 * @return Instance of {@link AcceptHeaderEnum}60 */61 public static AcceptHeaderEnum getAcceptHeaderEnum(String acceptHeader) {62 if (acceptHeader != null) {63 for (AcceptHeaderEnum acceptHeaderEnum : AcceptHeaderEnum.values()) {64 if (acceptHeader.contains(acceptHeaderEnum.getAcceptHeader())) {65 if (LOGGER.isLoggable(Level.FINE)) {66 LOGGER.log(Level.FINE, "Returning: " + acceptHeaderEnum.getClass().getSimpleName()67 + " for accept header: " + acceptHeader);68 }69 return acceptHeaderEnum;70 }71 }72 }73 throw new ArtifactUploadException("No 'UploadResponder' found for AcceptHeader: " + acceptHeader);74 }75 }76 /**77 * <code>AbstractUploadResponder</code> is abstract super class for concrete implementations that work on types of78 * {@link ManagedArtifact}.79 */80 abstract class AbstractUploadResponder implements UploadResponder {81 protected final TransferContext transferContext;82 protected final String requestUrl;83 protected final Map<String, String> headersMap;84 protected final List<ManagedArtifact> managedArtifactList;85 protected ManagedArtifact managedArtifactUnderProcess;86 public AbstractUploadResponder(TransferContext transferContext) {87 super();88 this.transferContext = transferContext;89 this.requestUrl = this.transferContext.getHttpServletRequest().getRequestURL().toString();90 this.managedArtifactList = this.transferContext.getUploadRequestProcessor().getUploadedData();91 this.headersMap = this.transferContext.getHeadersMap();92 }93 public void respond() {94 if (transferContext.getUploadRequestProcessor().getUploadedData().size() <= 0) {95 throw new ArtifactUploadException("No files processed by request processor");96 }97 respondFromRequestProcessor();98 }99 protected void addArtifactPath(StringBuffer url) {100 File repoFolder = ManagedArtifactRepository.getInstance().getRepositoryFolder();101 String relPath = managedArtifactUnderProcess.getAbsolutePath().replace(repoFolder.getAbsolutePath(), "");102 relPath = FilenameUtils.normalize(relPath);103 relPath = FilenameUtils.separatorsToUnix(relPath);104 url.append(relPath);105 }106 protected abstract void respondFromRequestProcessor();107 }108 /**109 * <code>JsonUploadResponder</code> for {@link AbstractUploadResponder} which sends out application/json responses110 * to {@link HttpServletResponse}111 */112 final class JsonUploadResponder extends AbstractUploadResponder {113 public static final String CONTENT_TYPE_VALUE = AcceptHeaderEnum.APPLICATION_JSON.getAcceptHeader();114 private final Gson gson;115 private final JsonObject jsonResponse;116 private final JsonArray files;117 public JsonUploadResponder(TransferContext transferContext) {118 super(transferContext);119 gson = new GsonBuilder().disableHtmlEscaping().create();120 jsonResponse = new JsonObject();121 files = new JsonArray();122 }123 protected void respondFromRequestProcessor() {124 SeLionGridLogger.getLogger(JsonUploadResponder.class).entering();125 PrintWriter out;126 transferContext.getHttpServletResponse().setContentType(CONTENT_TYPE_VALUE);127 try {128 out = transferContext.getHttpServletResponse().getWriter();129 jsonResponse.add("files", files);130 for (ManagedArtifact managedArtifact : managedArtifactList) {131 managedArtifactUnderProcess = managedArtifact;132 processArtifact();133 }134 out.println(gson.toJson(jsonResponse));135 SeLionGridLogger.getLogger(JsonUploadResponder.class).exiting();136 } catch (IOException e) {137 throw new ArtifactUploadException("IOException in retrieving HttpServletResponse's Writer", e);138 }139 }140 private void processArtifact() {141 JsonObject file = new JsonObject();142 StringBuffer url = new StringBuffer(requestUrl);143 addArtifactPath(url);144 file.addProperty(ManagedArtifact.ARTIFACT_FILE_NAME, managedArtifactUnderProcess.getArtifactName());145 file.addProperty("url", url.toString());146 files.add(file);147 }148 }149 /**150 * <code>TextPlainUploadResponder</code> for {@link AbstractUploadResponder} which sends out text/plain responses to151 * {@link HttpServletResponse}152 */153 final class TextPlainUploadResponder extends AbstractUploadResponder {154 public static final String CONTENT_TYPE_VALUE = AcceptHeaderEnum.TEXT_PLAIN.getAcceptHeader();155 private static final SeLionGridLogger LOGGER = SeLionGridLogger.getLogger(TextPlainUploadResponder.class);156 private final StringBuffer textResponse;157 public TextPlainUploadResponder(TransferContext transferContext) {158 super(transferContext);159 textResponse = new StringBuffer();160 }161 protected void respondFromRequestProcessor() {162 LOGGER.entering();163 PrintWriter out;164 transferContext.getHttpServletResponse().setContentType(CONTENT_TYPE_VALUE);165 try {166 out = transferContext.getHttpServletResponse().getWriter();167 for (ManagedArtifact managedArtifact : managedArtifactList) {168 managedArtifactUnderProcess = managedArtifact;...
getAcceptHeader
Using AI Code Generation
1String acceptHeader = getAcceptHeader(request);2response.setContentType(acceptHeader);3response.setStatus(HttpServletResponse.SC_OK);4response.flushBuffer();5String contentType = response.getContentType();6assertEquals(contentType, "application/json");7import static org.testng.Assert.assertEquals;8import java.io.File;9import java.io.IOException;10import java.io.InputStream;11import java.io.OutputStream;12import java.net.HttpURLConnection;13import java.net.URL;14import java.util.zip.ZipEntry;15import java.util.zip.ZipInputStream;16import org.apache.commons.io.FileUtils;17import org.apache.commons.io.IOUtils;18import org.apache.commons.lang.StringUtils;19import com.paypal.selion.grid.servlets.transfer.UploadResponder;20import com.paypal.selion.pojos.SeLionGridConstants;21public class UploadResponderTest {22 public static void main(String[] args) throws IOException {23 HttpURLConnection connection = (HttpURLConnection) url.openConnection();24 connection.setDoOutput(true);25 connection.setDoInput(true);26 connection.setUseCaches(false);27 connection.setRequestMethod("POST");28 connection.setRequestProperty("Content-Type", "application/zip");29 connection.setRequestProperty("Accept", "application/json");30 OutputStream outputStream = connection.getOutputStream();31 File file = new File("/Users/username/Downloads/zipfile.zip");32 IOUtils.copy(FileUtils.openInputStream(file), outputStream);33 outputStream.close();34 connection.connect();35 String contentType = connection.getContentType();36 System.out.println(contentType);37 assertEquals(contentType, "application/json");38 InputStream inputStream = connection.getInputStream();39 String responseBody = StringUtils.join(IOUtils.readLines(inputStream), "\r40");
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!!