App Engine Python SDK version 1.8.4
[gae.git] / java / src / main / com / google / apphosting / utils / config / DosXml.java
blob78ea7a0c88f5db1be782a2ec7f4e64240c137d6d
1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com.google.apphosting.utils.config;
5 import com.google.net.base.CidrAddressBlock;
7 import java.util.ArrayList;
8 import java.util.List;
10 /**
11 * Parsed dos.xml file.
13 * Any additions to this class should also be made to the YAML
14 * version in DosYamlReader.java.
17 public class DosXml {
19 /**
20 * Describes a single blacklist entry.
22 public static class BlacklistEntry {
24 String subnet;
25 String desc;
27 /** Create an empty blacklist entry. */
28 public BlacklistEntry() {
29 desc = "";
30 subnet = null;
33 /** Records the human-readable description of this blacklist entry. */
34 public void setDescription(String description) {
35 this.desc = description.replace('\n', ' ');
38 /** Records the subnet of this blacklist entry. */
39 public void setSubnet(String subnet) {
40 try {
41 CidrAddressBlock parsedSubnet = CidrAddressBlock.create(subnet);
42 this.subnet = subnet;
43 } catch (IllegalArgumentException iae) {
44 this.subnet = null;
45 throw new AppEngineConfigException("subnet " + subnet + " failed to parse",
46 iae.getCause());
50 public String getSubnet() {
51 return subnet;
54 public String getDescription() {
55 return desc;
59 private List<BlacklistEntry> blacklistEntries;
61 /** Create an empty configuration object. */
62 public DosXml() {
63 blacklistEntries = new ArrayList<BlacklistEntry>();
66 /**
67 * Puts a new blacklist entry into the list defined by the config file.
69 * @throws AppEngineConfigException if the previously-last blacklist entry is
70 * still incomplete.
71 * @return the new entry
73 public BlacklistEntry addNewBlacklistEntry() {
74 validateLastEntry();
75 BlacklistEntry entry = new BlacklistEntry();
76 blacklistEntries.add(entry);
77 return entry;
80 public void addBlacklistEntry(BlacklistEntry entry) {
81 validateLastEntry();
82 blacklistEntries.add(entry);
83 validateLastEntry();
86 /**
87 * Check that the last blacklist entry defined is complete.
88 * @throws AppEngineConfigException if it is not.
90 public void validateLastEntry() {
91 if (blacklistEntries.size() == 0) {
92 return;
94 BlacklistEntry last = blacklistEntries.get(blacklistEntries.size() - 1);
95 if (last.getSubnet() == null) {
96 throw new AppEngineConfigException("no subnet for blacklist");
101 * Get the YAML equivalent of this dos.xml file.
103 * @return contents of an equivalent {@code dos.yaml} file.
105 public String toYaml() {
106 StringBuilder builder = new StringBuilder("blacklist:\n");
107 for (BlacklistEntry ent : blacklistEntries) {
108 builder.append("- subnet: " + ent.getSubnet() + "\n");
109 if (!ent.getDescription().equals("")) {
110 builder.append(" description: " + ent.getDescription() + "\n");
113 return builder.toString();